Skip to content

Commit 5d613ef

Browse files
author
EarnForex
authored
2.36
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.
1 parent fce45a1 commit 5d613ef

File tree

6 files changed

+69
-25
lines changed

6 files changed

+69
-25
lines changed
0 Bytes
Binary file not shown.
2.98 KB
Binary file not shown.

MQL4/Scripts/PSC-Trader.mq4

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//+------------------------------------------------------------------+
66
#property copyright "Copyright 2015-2021, EarnForex.com"
77
#property link "https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/#Trading_script"
8-
#property version "1.11"
8+
#property version "1.12"
99
#property strict
1010
#include <stdlib.mqh>
1111

@@ -394,14 +394,13 @@ void OnStart()
394394
}
395395
}
396396

397-
// Going through a cycle to execute multiple TP trades.
398-
// ak
399-
double AccumulatedPositionSize = 0;
400-
double ArrayPositionSize[];
401-
402-
ArrayResize(ArrayPositionSize, n + 1);
397+
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
398+
double ArrayPositionSize[]; // PS for each trade.
399+
ArrayResize(ArrayPositionSize, n);
403400

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

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

427-
// ak
428-
if ( j > 0)
426+
// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
427+
if (j > 0)
429428
{
430429
AccumulatedPositionSize += position_size;
431430
}
432-
else
431+
else // For the main TP, use the remaining part of the total PS.
433432
{
434433
position_size = PositionSize - AccumulatedPositionSize;
435434
}
436-
437-
Print(PositionSize, "-----------", position_size);
438-
439435
ArrayPositionSize[j] = position_size;
440436
}
441437

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

448446

449447
// Market execution mode - preparation.
@@ -552,4 +550,4 @@ int CountDecimalPlaces(double number)
552550
}
553551
return(-1);
554552
}
555-
//+------------------------------------------------------------------+
553+
//+------------------------------------------------------------------+
0 Bytes
Binary file not shown.
2.98 KB
Binary file not shown.

MQL5/Scripts/PSC-Trader.mq5

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//+------------------------------------------------------------------+
66
#property copyright "Copyright 2015-2021, EarnForex.com"
77
#property link "https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/#Trading_script"
8-
#property version "1.11"
8+
#property version "1.12"
99
#include <Trade/Trade.mqh>
1010

1111
/*
@@ -374,10 +374,14 @@ void OnStart()
374374
return;
375375
}
376376

377-
// Going through a cycle to execute multiple TP trades.
378-
for (int j = 0; j < n; j++)
377+
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
378+
double ArrayPositionSize[]; // PS for each trade.
379+
ArrayResize(ArrayPositionSize, n);
380+
381+
// Cycle to calculate volume for each partial trade.
382+
// The goal is to use normal rounded down values for additional TPs and then throw the remainder to the main TP.
383+
for (int j = n - 1; j >= 0; j--)
379384
{
380-
tp = NormalizeDouble(ScriptTPValue[j], _Digits);
381385
double position_size = PositionSize * ScriptTPShareValue[j] / 100.0;
382386

383387
if (position_size < MinLot)
@@ -397,6 +401,25 @@ void OnStart()
397401
position_size = MathFloor(steps) * LotStep;
398402
Print("Adjusting position size to the broker's Lot Step parameter.");
399403
}
404+
405+
// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
406+
if (j > 0)
407+
{
408+
AccumulatedPositionSize += position_size;
409+
}
410+
else // For the main TP, use the remaining part of the total PS.
411+
{
412+
position_size = PositionSize - AccumulatedPositionSize;
413+
}
414+
ArrayPositionSize[j] = position_size;
415+
}
416+
int LotStep_digits = CountDecimalPlaces(LotStep); // Required for proper volume normalization.
417+
418+
// Going through a cycle to execute multiple TP trades.
419+
for (int j = 0; j < n; j++)
420+
{
421+
tp = NormalizeDouble(ScriptTPValue[j], _Digits);
422+
double position_size = NormalizeDouble(ArrayPositionSize[j], LotStep_digits);
400423

401424
if (DoNotApplyStopLoss) sl = 0;
402425
if (DoNotApplyTakeProfit) tp = 0;
@@ -447,11 +470,14 @@ void OnStart()
447470
return;
448471
}
449472

450-
// Going through a cycle to execute multiple TP trades.
451-
for (int j = 0; j < n; j++)
473+
double AccumulatedPositionSize = 0; // Total PS used by additional TPs.
474+
double ArrayPositionSize[]; // PS for each trade.
475+
ArrayResize(ArrayPositionSize, n);
476+
477+
// Cycle to calculate volume for each partial trade.
478+
// The goal is to use normal rounded down values for additional TPs and then throw the remainder to the main TP.
479+
for (int j = n - 1; j >= 0; j--)
452480
{
453-
double order_sl = sl;
454-
double order_tp = NormalizeDouble(ScriptTPValue[j], _Digits);
455481
double position_size = PositionSize * ScriptTPShareValue[j] / 100.0;
456482

457483
if (position_size < MinLot)
@@ -471,7 +497,27 @@ void OnStart()
471497
position_size = MathFloor(steps) * LotStep;
472498
Print("Adjusting position size to the broker's Lot Step parameter.");
473499
}
474-
500+
501+
// If this is one of the additional TPs, then count its PS towards total PS that will be open for additional TPs.
502+
if (j > 0)
503+
{
504+
AccumulatedPositionSize += position_size;
505+
}
506+
else // For the main TP, use the remaining part of the total PS.
507+
{
508+
position_size = PositionSize - AccumulatedPositionSize;
509+
}
510+
ArrayPositionSize[j] = position_size;
511+
}
512+
int LotStep_digits = CountDecimalPlaces(LotStep); // Required for proper volume normalization.
513+
514+
// Going through a cycle to execute multiple TP trades.
515+
for (int j = 0; j < n; j++)
516+
{
517+
double order_sl = sl;
518+
double order_tp = NormalizeDouble(ScriptTPValue[j], _Digits);
519+
double position_size = NormalizeDouble(ArrayPositionSize[j], LotStep_digits);
520+
475521
// Market execution mode - preparation.
476522
if ((Execution_Mode == SYMBOL_TRADE_EXECUTION_MARKET) && (entry_type == Instant))
477523
{

0 commit comments

Comments
 (0)