5
5
//+------------------------------------------------------------------+
6
6
#property copyright " Copyright 2015-2021, EarnForex.com"
7
7
#property link " https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/#Trading_script"
8
- #property version " 1.11 "
8
+ #property version " 1.12 "
9
9
#include < Trade/Trade.mqh>
10
10
11
11
/*
@@ -374,10 +374,14 @@ void OnStart()
374
374
return ;
375
375
}
376
376
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 --)
379
384
{
380
- tp = NormalizeDouble (ScriptTPValue [j ], _Digits );
381
385
double position_size = PositionSize * ScriptTPShareValue [j ] / 100.0 ;
382
386
383
387
if (position_size < MinLot )
@@ -397,6 +401,25 @@ void OnStart()
397
401
position_size = MathFloor (steps ) * LotStep ;
398
402
Print (" Adjusting position size to the broker's Lot Step parameter." );
399
403
}
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 );
400
423
401
424
if (DoNotApplyStopLoss ) sl = 0 ;
402
425
if (DoNotApplyTakeProfit ) tp = 0 ;
@@ -447,11 +470,14 @@ void OnStart()
447
470
return ;
448
471
}
449
472
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 --)
452
480
{
453
- double order_sl = sl ;
454
- double order_tp = NormalizeDouble (ScriptTPValue [j ], _Digits );
455
481
double position_size = PositionSize * ScriptTPShareValue [j ] / 100.0 ;
456
482
457
483
if (position_size < MinLot )
@@ -471,7 +497,27 @@ void OnStart()
471
497
position_size = MathFloor (steps ) * LotStep ;
472
498
Print (" Adjusting position size to the broker's Lot Step parameter." );
473
499
}
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
+
475
521
// Market execution mode - preparation.
476
522
if ((Execution_Mode == SYMBOL_TRADE_EXECUTION_MARKET ) && (entry_type == Instant ))
477
523
{
0 commit comments