1
1
//+------------------------------------------------------------------+
2
2
//| PositionSizeCalculator.mq4 |
3
- //| Copyright © 2012-2021 , EarnForex.com |
3
+ //| Copyright © 2012-2022 , EarnForex.com |
4
4
//| Based on panel by qubbit.com |
5
5
//| https://www.earnforex.com/ |
6
6
//+------------------------------------------------------------------+
7
7
#property copyright " EarnForex.com"
8
8
#property link " https://www.earnforex.com/metatrader-indicators/Position-Size-Calculator/"
9
- #property version " 2.41 "
10
- string Version = " 2.41 " ;
9
+ #property version " 2.42 "
10
+ string Version = " 2.42 " ;
11
11
#property strict
12
12
#property indicator_chart_window
13
13
#property indicator_plots 0
@@ -23,13 +23,13 @@ string Version = "2.41";
23
23
#include " PositionSizeCalculator.mqh" ;
24
24
25
25
input group " Compactness"
26
- input bool ShowLineLabels = true ; // ShowLineLabels: Show pip distance for TP/SL near lines?
26
+ input bool ShowLineLabels = true ; // ShowLineLabels: Show point distance for TP/SL near lines?
27
27
input bool ShowAdditionalSLLabel = false ; // ShowAdditionalSLLabel: Show SL $/% label?
28
28
input bool ShowAdditionalTPLabel = false ; // ShowAdditionalTPLabel: Show TP $/% + R/R label?
29
29
input bool DrawTextAsBackground = false ; // DrawTextAsBackground: Draw label objects as background?
30
30
input bool PanelOnTopOfChart = true ; // PanelOnTopOfChart: Draw chart as background?
31
31
input bool HideAccSize = false ; // HideAccSize: Hide account size?
32
- input bool ShowPipValue = false ; // ShowPipValue : Show pip value?
32
+ input bool ShowPointValue = false ; // ShowPointValue : Show point value?
33
33
input bool ShowMaxPSButton = false ; // ShowMaxPSButton: Show Max Position Size button?
34
34
input bool StartPanelMinimized = false ; // StartPanelMinimized: Start the panel minimized?
35
35
input group " Fonts"
@@ -49,8 +49,8 @@ input uint stoploss_line_width = 1; // Stop-Loss Line Width
49
49
input uint takeprofit_line_width = 1 ; // Take-Profit Line Width
50
50
input group " Defaults"
51
51
input TRADE_DIRECTION DefaultTradeDirection = Long ; // TradeDirection: Default trade direction.
52
- input int DefaultSL = 0 ; // SL: Default stop-loss value, in broker's pips .
53
- input int DefaultTP = 0 ; // TP: Default take-profit value, in broker's pips .
52
+ input int DefaultSL = 0 ; // SL: Default stop-loss value, in broker's points .
53
+ input int DefaultTP = 0 ; // TP: Default take-profit value, in broker's points .
54
54
input ENTRY_TYPE DefaultEntryType = Instant ; // EntryType: Instant or Pending.
55
55
input bool DefaultShowLines = true ; // ShowLines: Show the lines by default?
56
56
input bool DefaultLinesSelected = true ; // LinesSelected: SL/TP (Entry in Pending) lines selected.
@@ -68,6 +68,7 @@ input bool DefaultIgnoreOtherSymbols = false; // IgnoreOtherSymbols: Ignore othe
68
68
input int DefaultCustomLeverage = 0 ; // CustomLeverage: Default custom leverage for Margin tab.
69
69
input int DefaultMagicNumber = 0 ; // MagicNumber: Default magic number for Script tab.
70
70
input string DefaultCommentary = " " ; // Commentary: Default order comment for Script tab.
71
+ input bool DefaultScriptCommentAutoSuffix = false ; // AutoSuffix: Automatic suffix for order commentary in Script tab.
71
72
input bool DefaultDisableTradingWhenLinesAreHidden = false ; // DisableTradingWhenLinesAreHidden: for Script tab.
72
73
input int DefaultMaxSlippage = 0 ; // MaxSlippage: Maximum slippage for Script tab.
73
74
input int DefaultMaxSpread = 0 ; // MaxSpread: Maximum spread for Script tab.
@@ -93,7 +94,7 @@ input bool SLDistanceInPoints = false; // SLDistanceInPoints: SL distance in poi
93
94
input bool TPDistanceInPoints = false ; // TPDistanceInPoints: TP distance in points instead of a level.
94
95
input bool ShowATROptions = false ; // ShowATROptions: If true, SL and TP can be set via ATR.
95
96
input CANDLE_NUMBER ATRCandle = Current_Candle ; // ATRCandle: Candle to get ATR value from.
96
- input int ScriptTakePorfitsNumber = 1 ; // ScriptTakePorfitsNumber : More than 1 target for script to split trades.
97
+ input int ScriptTakeProfitsNumber = 1 ; // ScriptTakeProfitsNumber : More than 1 target for script to split trades.
97
98
input bool CalculateUnadjustedPositionSize = false ; // CalculateUnadjustedPositionSize: Ignore broker's restrictions.
98
99
input bool RoundDown = true ; // RoundDown: Position size and potential reward are rounded down.
99
100
input double QuickRisk1 = 0 ; // QuickRisk1: First quick risk button, in percentage points.
@@ -105,6 +106,9 @@ QCPositionSizeCalculator ExtDialog;
105
106
// Global variables:
106
107
bool Dont_Move_the_Panel_to_Default_Corner_X_Y ;
107
108
uint LastRecalculationTime = 0 ;
109
+ bool StopLossLineIsBeingMoved = false ;
110
+ bool TakeProfitLineIsBeingMoved = false ;
111
+ bool NeedToToggleScaleOffOn = false ;
108
112
109
113
//+------------------------------------------------------------------+
110
114
//| Custom indicator initialization function |
@@ -135,15 +139,16 @@ int OnInit()
135
139
}
136
140
137
141
IndicatorSetString (INDICATOR_SHORTNAME , indicator_short_name );
138
- PanelCaption = " Position Size Calculator (ver. " + Version + " )" ;
142
+ if (!ShowSpread ) PanelCaptionBase = " Position Size Calculator (ver. " + Version + " )" ;
143
+ else PanelCaptionBase = " PSC (ver. " + Version + " )" ; // A shorter version for the Spread to fit.
139
144
140
- if (ScriptTakePorfitsNumber > 1 )
145
+ if (ScriptTakeProfitsNumber > 1 )
141
146
{
142
- ArrayResize (sets .ScriptTP , ScriptTakePorfitsNumber );
143
- ArrayResize (sets .ScriptTPShare , ScriptTakePorfitsNumber );
147
+ ArrayResize (sets .ScriptTP , ScriptTakeProfitsNumber );
148
+ ArrayResize (sets .ScriptTPShare , ScriptTakeProfitsNumber );
144
149
ArrayInitialize (sets .ScriptTP , 0 );
145
- ArrayInitialize (sets .ScriptTPShare , 100 / ScriptTakePorfitsNumber );
146
- ArrayResize (sets .WasSelectedAdditionalTakeProfitLine , ScriptTakePorfitsNumber - 1 ); // -1 because the flag for the main TP is saved elsewhere.
150
+ ArrayInitialize (sets .ScriptTPShare , 100 / ScriptTakeProfitsNumber );
151
+ ArrayResize (sets .WasSelectedAdditionalTakeProfitLine , ScriptTakeProfitsNumber - 1 ); // -1 because the flag for the main TP is saved elsewhere.
147
152
}
148
153
149
154
if (!ExtDialog .LoadSettingsFromDisk ())
@@ -174,13 +179,14 @@ int OnInit()
174
179
sets .CustomLeverage = DefaultCustomLeverage ;
175
180
sets .MagicNumber = DefaultMagicNumber ;
176
181
sets .ScriptCommentary = DefaultCommentary ;
182
+ sets .ScriptCommentAutoSuffix = DefaultScriptCommentAutoSuffix ;
177
183
sets .DisableTradingWhenLinesAreHidden = DefaultDisableTradingWhenLinesAreHidden ;
178
- if (ScriptTakePorfitsNumber > 1 )
184
+ if (ScriptTakeProfitsNumber > 1 )
179
185
{
180
- for (int i = 0 ; i < ScriptTakePorfitsNumber ; i ++)
186
+ for (int i = 0 ; i < ScriptTakeProfitsNumber ; i ++)
181
187
{
182
188
sets .ScriptTP [i ] = TakeProfitLevel ;
183
- sets .ScriptTPShare [i ] = 100 / ScriptTakePorfitsNumber ;
189
+ sets .ScriptTPShare [i ] = 100 / ScriptTakeProfitsNumber ;
184
190
}
185
191
}
186
192
sets .MaxSlippage = DefaultMaxSlippage ;
@@ -223,6 +229,8 @@ int OnInit()
223
229
// Brings panel on top of other objects without actual maximization of the panel.
224
230
ExtDialog .HideShowMaximize ();
225
231
232
+ NeedToToggleScaleOffOn = true ;
233
+
226
234
if (!Dont_Move_the_Panel_to_Default_Corner_X_Y )
227
235
{
228
236
int new_x = DefaultPanelPositionX , new_y = DefaultPanelPositionY ;
@@ -281,7 +289,7 @@ void OnDeinit(const int reason)
281
289
ObjectDelete (0 , ObjectPrefix + " TakeProfitLabel" );
282
290
ObjectDelete (0 , ObjectPrefix + " TPAdditionalLabel" );
283
291
ObjectDelete (0 , ObjectPrefix + " SLAdditionalLabel" );
284
- for (int i = 1 ; i < ScriptTakePorfitsNumber ; i ++)
292
+ for (int i = 1 ; i < ScriptTakeProfitsNumber ; i ++)
285
293
{
286
294
ObjectDelete (0 , ObjectPrefix + " TakeProfitLabel" + IntegerToString (i ));
287
295
ObjectDelete (0 , ObjectPrefix + " TPAdditionalLabel" + IntegerToString (i ));
@@ -321,6 +329,15 @@ int OnCalculate(const int rates_total,
321
329
const long &volume [],
322
330
const int &spread [])
323
331
{
332
+ // Toggle price scale off and then on to return it to its original size.
333
+ // This can be useful when switching from symbol with a wide price scale (index, BTC, etc.) to one with a narrow scale (EUR/USD).
334
+ if (NeedToToggleScaleOffOn )
335
+ {
336
+ ChartSetInteger (ChartID (), CHART_SHOW_PRICE_SCALE , false );
337
+ ChartSetInteger (ChartID (), CHART_SHOW_PRICE_SCALE , true );
338
+ NeedToToggleScaleOffOn = false ;
339
+ }
340
+
324
341
ExtDialog .RefreshValues ();
325
342
return rates_total ;
326
343
}
@@ -333,6 +350,39 @@ void OnChartEvent(const int id,
333
350
const double &dparam ,
334
351
const string &sparam )
335
352
{
353
+ // Mouse move while left mouse button is down.
354
+ if ((id == CHARTEVENT_MOUSE_MOVE ) && (((uint )sparam & 1 ) == 1 ))
355
+ {
356
+ if (SLDistanceInPoints )
357
+ {
358
+ double current_line_price = ObjectGetDouble (ChartID (), ObjectPrefix + " StopLossLine" , OBJPROP_PRICE , 0 );
359
+ if (current_line_price != tStopLossLevel ) StopLossLineIsBeingMoved = true ;
360
+ else StopLossLineIsBeingMoved = false ;
361
+ }
362
+ if (TPDistanceInPoints )
363
+ {
364
+ TakeProfitLineIsBeingMoved = false ;
365
+ double current_line_price = ObjectGetDouble (ChartID (), ObjectPrefix + " TakeProfitLine" , OBJPROP_PRICE , 0 );
366
+ if (current_line_price != tTakeProfitLevel ) TakeProfitLineIsBeingMoved = true ;
367
+ // Additional take-profits.
368
+ else if (ScriptTakeProfitsNumber > 1 )
369
+ {
370
+ for (int i = 1 ; i < ScriptTakeProfitsNumber ; i ++)
371
+ {
372
+ if (sets .ScriptTP [i ] != 0 ) // With zero points TP, keep the TP lines at zero level - as with the main TP level.
373
+ {
374
+ current_line_price = ObjectGetDouble (ChartID (), ObjectPrefix + " TakeProfitLine" + IntegerToString (i ), OBJPROP_PRICE , 0 );
375
+ if (current_line_price != sets .ScriptTP [i ])
376
+ {
377
+ TakeProfitLineIsBeingMoved = true ;
378
+ break ;
379
+ }
380
+ }
381
+ }
382
+ }
383
+ }
384
+ }
385
+
336
386
// Remember the panel's location to have the same location for minimized and maximized states.
337
387
if ((id == CHARTEVENT_CUSTOM + ON_DRAG_END ) && (lparam == -1 ))
338
388
{
@@ -341,7 +391,7 @@ void OnChartEvent(const int id,
341
391
}
342
392
343
393
// Catch multiple TP fields.
344
- if (ScriptTakePorfitsNumber > 1 )
394
+ if (ScriptTakeProfitsNumber > 1 )
345
395
{
346
396
if (id == CHARTEVENT_CUSTOM + ON_END_EDIT )
347
397
{
@@ -366,6 +416,14 @@ void OnChartEvent(const int id,
366
416
}
367
417
}
368
418
419
+ if (id == CHARTEVENT_KEYDOWN )
420
+ {
421
+ if (lparam == 9 ) // Tab key to shift from Long to Short and vice versa.
422
+ {
423
+ SwitchEntryDirection ();
424
+ }
425
+ }
426
+
369
427
// Call Panel's event handler only if it is not a CHARTEVENT_CHART_CHANGE - workaround for minimization bug on chart switch.
370
428
if (id != CHARTEVENT_CHART_CHANGE ) ExtDialog .OnEvent (id , lparam , dparam , sparam );
371
429
@@ -378,14 +436,17 @@ void OnChartEvent(const int id,
378
436
{
379
437
if (sparam == ObjectPrefix + " StopLossLine" ) ExtDialog .UpdateFixedSL ();
380
438
else if (sparam == ObjectPrefix + " TakeProfitLine" ) ExtDialog .UpdateFixedTP ();
381
- else if ((ScriptTakePorfitsNumber > 1 ) && (StringFind (sparam , ObjectPrefix + " TakeProfitLine" ) != -1 ))
439
+ else if ((ScriptTakeProfitsNumber > 1 ) && (StringFind (sparam , ObjectPrefix + " TakeProfitLine" ) != -1 ))
382
440
{
383
441
int len = StringLen (ObjectPrefix + " TakeProfitLine" );
384
442
int i = (int )StringToInteger (StringSubstr (sparam , len ));
385
443
ExtDialog .UpdateAdditionalFixedTP (i );
386
444
}
387
445
}
388
446
447
+ if (sparam == ObjectPrefix + " StopLossLine" ) StopLossLineIsBeingMoved = false ; // In any case ending moving state for the stop-loss line.
448
+ if (StringFind (sparam , ObjectPrefix + " TakeProfitLine" ) != -1 ) TakeProfitLineIsBeingMoved = false ; // In any case ending moving state for the take-profit line.
449
+
389
450
if (id != CHARTEVENT_CHART_CHANGE ) ExtDialog .RefreshValues ();
390
451
391
452
static bool prev_chart_on_top = false ;
0 commit comments