Skip to content

Commit

Permalink
2.36
Browse files Browse the repository at this point in the history
1. Changed how position size is distributed by multiple TP positions. Now the sum position size will equal the calculated total position size. If total position size cannot be divided equally, main TP trade will receive the remainder. Suggested and first code solution provided by @akhater.
2. Fixed deletion of unnecessary TP lines when switching from higher number of TPs to a lower one.
  • Loading branch information
EarnForex authored Apr 6, 2021
1 parent fce45a1 commit 5d613ef
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 25 deletions.
Binary file not shown.
Binary file not shown.
30 changes: 14 additions & 16 deletions MQL4/Scripts/PSC-Trader.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2015-2021, EarnForex.com"
#property link "https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/#Trading_script"
#property version "1.11"
#property version "1.12"
#property strict
#include <stdlib.mqh>

Expand Down Expand Up @@ -394,14 +394,13 @@ void OnStart()
}
}

// Going through a cycle to execute multiple TP trades.
// ak
double AccumulatedPositionSize = 0;
double ArrayPositionSize[];

ArrayResize(ArrayPositionSize, n + 1);
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
double ArrayPositionSize[]; // PS for each trade.
ArrayResize(ArrayPositionSize, n);

for (int j = n; j >= 0; j--)
// Cycle to calculate volume for each partial trade.
// The goal is to use normal rounded down values for additional TPs and then throw the remainder to the main TP.
for (int j = n - 1; j >= 0; j--)
{
double position_size = PositionSize * ScriptTPShareValue[j] / 100.0;

Expand All @@ -424,26 +423,25 @@ void OnStart()
Print("Adjusting position size to the broker's Lot Step parameter.");
}

// ak
if ( j > 0)
// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
if (j > 0)
{
AccumulatedPositionSize += position_size;
}
else
else // For the main TP, use the remaining part of the total PS.
{
position_size = PositionSize - AccumulatedPositionSize;
}

Print(PositionSize, "-----------", position_size);

ArrayPositionSize[j] = position_size;
}

int LotStep_digits = CountDecimalPlaces(LotStep); // Required for proper volume normalization.
// Going through a cycle to execute multiple TP trades.
for (int j = 0; j < n; j++)
{
double order_sl = sl;
double order_tp = NormalizeDouble(ScriptTPValue[j], _Digits);
double position_size = ArrayPositionSize[j];
double position_size = NormalizeDouble(ArrayPositionSize[j], LotStep_digits);


// Market execution mode - preparation.
Expand Down Expand Up @@ -552,4 +550,4 @@ int CountDecimalPlaces(double number)
}
return(-1);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
Binary file not shown.
Binary file not shown.
64 changes: 55 additions & 9 deletions MQL5/Scripts/PSC-Trader.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2015-2021, EarnForex.com"
#property link "https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/#Trading_script"
#property version "1.11"
#property version "1.12"
#include <Trade/Trade.mqh>

/*
Expand Down Expand Up @@ -374,10 +374,14 @@ void OnStart()
return;
}

// Going through a cycle to execute multiple TP trades.
for (int j = 0; j < n; j++)
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
double ArrayPositionSize[]; // PS for each trade.
ArrayResize(ArrayPositionSize, n);

// Cycle to calculate volume for each partial trade.
// The goal is to use normal rounded down values for additional TPs and then throw the remainder to the main TP.
for (int j = n - 1; j >= 0; j--)
{
tp = NormalizeDouble(ScriptTPValue[j], _Digits);
double position_size = PositionSize * ScriptTPShareValue[j] / 100.0;

if (position_size < MinLot)
Expand All @@ -397,6 +401,25 @@ void OnStart()
position_size = MathFloor(steps) * LotStep;
Print("Adjusting position size to the broker's Lot Step parameter.");
}

// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
if (j > 0)
{
AccumulatedPositionSize += position_size;
}
else // For the main TP, use the remaining part of the total PS.
{
position_size = PositionSize - AccumulatedPositionSize;
}
ArrayPositionSize[j] = position_size;
}
int LotStep_digits = CountDecimalPlaces(LotStep); // Required for proper volume normalization.

// Going through a cycle to execute multiple TP trades.
for (int j = 0; j < n; j++)
{
tp = NormalizeDouble(ScriptTPValue[j], _Digits);
double position_size = NormalizeDouble(ArrayPositionSize[j], LotStep_digits);

if (DoNotApplyStopLoss) sl = 0;
if (DoNotApplyTakeProfit) tp = 0;
Expand Down Expand Up @@ -447,11 +470,14 @@ void OnStart()
return;
}

// Going through a cycle to execute multiple TP trades.
for (int j = 0; j < n; j++)
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
double ArrayPositionSize[]; // PS for each trade.
ArrayResize(ArrayPositionSize, n);

// Cycle to calculate volume for each partial trade.
// The goal is to use normal rounded down values for additional TPs and then throw the remainder to the main TP.
for (int j = n - 1; j >= 0; j--)
{
double order_sl = sl;
double order_tp = NormalizeDouble(ScriptTPValue[j], _Digits);
double position_size = PositionSize * ScriptTPShareValue[j] / 100.0;

if (position_size < MinLot)
Expand All @@ -471,7 +497,27 @@ void OnStart()
position_size = MathFloor(steps) * LotStep;
Print("Adjusting position size to the broker's Lot Step parameter.");
}


// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
if (j > 0)
{
AccumulatedPositionSize += position_size;
}
else // For the main TP, use the remaining part of the total PS.
{
position_size = PositionSize - AccumulatedPositionSize;
}
ArrayPositionSize[j] = position_size;
}
int LotStep_digits = CountDecimalPlaces(LotStep); // Required for proper volume normalization.

// Going through a cycle to execute multiple TP trades.
for (int j = 0; j < n; j++)
{
double order_sl = sl;
double order_tp = NormalizeDouble(ScriptTPValue[j], _Digits);
double position_size = NormalizeDouble(ArrayPositionSize[j], LotStep_digits);

// Market execution mode - preparation.
if ((Execution_Mode == SYMBOL_TRADE_EXECUTION_MARKET) && (entry_type == Instant))
{
Expand Down

0 comments on commit 5d613ef

Please sign in to comment.