Skip to content

Commit 65afbd7

Browse files
author
EarnForex
authored
2.42
1. Added a display of the current trade direction (long/short) as an arrow in the top-left corner of the panel (MT5) or as a word in the panel's caption (MT4). 2. Added the TAB key as a keyboard shortcut to switch the trade's direction from long to short and vice versa. 3. Fixed a bug that resulted in a misplaced line label when switching from long-quote symbol to a short-quote one in MT4. 4. Fixed a typo in the ScriptTakeProfitsNumber input parameter. It may reset after you update to a new version — don't forget to change it back to your preferred value if this happens. 5. Fixed SL and TP lines snapping back while dragging them to a new level. 6. Fixed portfolio risk calculation by adding existing swap values to it (MT4 version only). 7. Script: Fixed detection of pending/instant order type by analyzing the panel rather than relying on price levels alone. 8. Script: Added extra information in the order confirmation window. 9. Script: Fixed order opening when stop-loss is too close to the current price. Previously, it could result in a trade without a stop-loss; no trade will be opened now. 10. Script: Added an option to append automatically generated suffix to order commentary. This can be helpful to uniquely mark sets of orders when trading with multiple take-profit levels. A default value input parameter (AutoSuffix) has been added. 11. Script: Improved execution performance due to fewer calls to chart object values. 12. Changed "pips" to "points" everywhere for clarity.
1 parent c203da4 commit 65afbd7

File tree

8 files changed

+733
-499
lines changed

8 files changed

+733
-499
lines changed

MQL4/Indicators/PositionSizeCalculator/Defines.mqh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//+------------------------------------------------------------------+
22
//| Defines.mqh |
3-
//| Copyright © 2012-2021, EarnForex.com |
3+
//| Copyright © 2012-2022, EarnForex.com |
44
//| Based on panel by qubbit.com |
55
//| https://www.earnforex.com/ |
66
//+------------------------------------------------------------------+
@@ -107,6 +107,7 @@ struct Settings
107107
bool DoNotApplyStopLoss;
108108
bool DoNotApplyTakeProfit;
109109
bool AskForConfirmation;
110+
bool ScriptCommentAutoSuffix;
110111
bool IsPanelMinimized;
111112
bool TPLockedOnSL;
112113
} sets;

MQL4/Indicators/PositionSizeCalculator/PositionSizeCalculator.mq4

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//+------------------------------------------------------------------+
22
//| PositionSizeCalculator.mq4 |
3-
//| Copyright © 2012-2021, EarnForex.com |
3+
//| Copyright © 2012-2022, EarnForex.com |
44
//| Based on panel by qubbit.com |
55
//| https://www.earnforex.com/ |
66
//+------------------------------------------------------------------+
77
#property copyright "EarnForex.com"
88
#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";
1111
#property strict
1212
#property indicator_chart_window
1313
#property indicator_plots 0
@@ -23,13 +23,13 @@ string Version = "2.41";
2323
#include "PositionSizeCalculator.mqh";
2424

2525
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?
2727
input bool ShowAdditionalSLLabel = false; // ShowAdditionalSLLabel: Show SL $/% label?
2828
input bool ShowAdditionalTPLabel = false; // ShowAdditionalTPLabel: Show TP $/% + R/R label?
2929
input bool DrawTextAsBackground = false; // DrawTextAsBackground: Draw label objects as background?
3030
input bool PanelOnTopOfChart = true; // PanelOnTopOfChart: Draw chart as background?
3131
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?
3333
input bool ShowMaxPSButton = false; // ShowMaxPSButton: Show Max Position Size button?
3434
input bool StartPanelMinimized = false; // StartPanelMinimized: Start the panel minimized?
3535
input group "Fonts"
@@ -49,8 +49,8 @@ input uint stoploss_line_width = 1; // Stop-Loss Line Width
4949
input uint takeprofit_line_width = 1; // Take-Profit Line Width
5050
input group "Defaults"
5151
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.
5454
input ENTRY_TYPE DefaultEntryType = Instant; // EntryType: Instant or Pending.
5555
input bool DefaultShowLines = true; // ShowLines: Show the lines by default?
5656
input bool DefaultLinesSelected = true; // LinesSelected: SL/TP (Entry in Pending) lines selected.
@@ -68,6 +68,7 @@ input bool DefaultIgnoreOtherSymbols = false; // IgnoreOtherSymbols: Ignore othe
6868
input int DefaultCustomLeverage = 0; // CustomLeverage: Default custom leverage for Margin tab.
6969
input int DefaultMagicNumber = 0; // MagicNumber: Default magic number for Script tab.
7070
input string DefaultCommentary = ""; // Commentary: Default order comment for Script tab.
71+
input bool DefaultScriptCommentAutoSuffix = false; // AutoSuffix: Automatic suffix for order commentary in Script tab.
7172
input bool DefaultDisableTradingWhenLinesAreHidden = false; // DisableTradingWhenLinesAreHidden: for Script tab.
7273
input int DefaultMaxSlippage = 0; // MaxSlippage: Maximum slippage for Script tab.
7374
input int DefaultMaxSpread = 0; // MaxSpread: Maximum spread for Script tab.
@@ -93,7 +94,7 @@ input bool SLDistanceInPoints = false; // SLDistanceInPoints: SL distance in poi
9394
input bool TPDistanceInPoints = false; // TPDistanceInPoints: TP distance in points instead of a level.
9495
input bool ShowATROptions = false; // ShowATROptions: If true, SL and TP can be set via ATR.
9596
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.
9798
input bool CalculateUnadjustedPositionSize = false; // CalculateUnadjustedPositionSize: Ignore broker's restrictions.
9899
input bool RoundDown = true; // RoundDown: Position size and potential reward are rounded down.
99100
input double QuickRisk1 = 0; // QuickRisk1: First quick risk button, in percentage points.
@@ -105,6 +106,9 @@ QCPositionSizeCalculator ExtDialog;
105106
// Global variables:
106107
bool Dont_Move_the_Panel_to_Default_Corner_X_Y;
107108
uint LastRecalculationTime = 0;
109+
bool StopLossLineIsBeingMoved = false;
110+
bool TakeProfitLineIsBeingMoved = false;
111+
bool NeedToToggleScaleOffOn = false;
108112

109113
//+------------------------------------------------------------------+
110114
//| Custom indicator initialization function |
@@ -135,15 +139,16 @@ int OnInit()
135139
}
136140

137141
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.
139144

140-
if (ScriptTakePorfitsNumber > 1)
145+
if (ScriptTakeProfitsNumber > 1)
141146
{
142-
ArrayResize(sets.ScriptTP, ScriptTakePorfitsNumber);
143-
ArrayResize(sets.ScriptTPShare, ScriptTakePorfitsNumber);
147+
ArrayResize(sets.ScriptTP, ScriptTakeProfitsNumber);
148+
ArrayResize(sets.ScriptTPShare, ScriptTakeProfitsNumber);
144149
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.
147152
}
148153

149154
if (!ExtDialog.LoadSettingsFromDisk())
@@ -174,13 +179,14 @@ int OnInit()
174179
sets.CustomLeverage = DefaultCustomLeverage;
175180
sets.MagicNumber = DefaultMagicNumber;
176181
sets.ScriptCommentary = DefaultCommentary;
182+
sets.ScriptCommentAutoSuffix = DefaultScriptCommentAutoSuffix;
177183
sets.DisableTradingWhenLinesAreHidden = DefaultDisableTradingWhenLinesAreHidden;
178-
if (ScriptTakePorfitsNumber > 1)
184+
if (ScriptTakeProfitsNumber > 1)
179185
{
180-
for (int i = 0; i < ScriptTakePorfitsNumber; i++)
186+
for (int i = 0; i < ScriptTakeProfitsNumber; i++)
181187
{
182188
sets.ScriptTP[i] = TakeProfitLevel;
183-
sets.ScriptTPShare[i] = 100 / ScriptTakePorfitsNumber;
189+
sets.ScriptTPShare[i] = 100 / ScriptTakeProfitsNumber;
184190
}
185191
}
186192
sets.MaxSlippage = DefaultMaxSlippage;
@@ -223,6 +229,8 @@ int OnInit()
223229
// Brings panel on top of other objects without actual maximization of the panel.
224230
ExtDialog.HideShowMaximize();
225231

232+
NeedToToggleScaleOffOn = true;
233+
226234
if (!Dont_Move_the_Panel_to_Default_Corner_X_Y)
227235
{
228236
int new_x = DefaultPanelPositionX, new_y = DefaultPanelPositionY;
@@ -281,7 +289,7 @@ void OnDeinit(const int reason)
281289
ObjectDelete(0, ObjectPrefix + "TakeProfitLabel");
282290
ObjectDelete(0, ObjectPrefix + "TPAdditionalLabel");
283291
ObjectDelete(0, ObjectPrefix + "SLAdditionalLabel");
284-
for (int i = 1; i < ScriptTakePorfitsNumber; i++)
292+
for (int i = 1; i < ScriptTakeProfitsNumber; i++)
285293
{
286294
ObjectDelete(0, ObjectPrefix + "TakeProfitLabel" + IntegerToString(i));
287295
ObjectDelete(0, ObjectPrefix + "TPAdditionalLabel" + IntegerToString(i));
@@ -321,6 +329,15 @@ int OnCalculate(const int rates_total,
321329
const long &volume[],
322330
const int &spread[])
323331
{
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+
324341
ExtDialog.RefreshValues();
325342
return rates_total;
326343
}
@@ -333,6 +350,39 @@ void OnChartEvent(const int id,
333350
const double &dparam,
334351
const string &sparam)
335352
{
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+
336386
// Remember the panel's location to have the same location for minimized and maximized states.
337387
if ((id == CHARTEVENT_CUSTOM + ON_DRAG_END) && (lparam == -1))
338388
{
@@ -341,7 +391,7 @@ void OnChartEvent(const int id,
341391
}
342392

343393
// Catch multiple TP fields.
344-
if (ScriptTakePorfitsNumber > 1)
394+
if (ScriptTakeProfitsNumber > 1)
345395
{
346396
if (id == CHARTEVENT_CUSTOM + ON_END_EDIT)
347397
{
@@ -366,6 +416,14 @@ void OnChartEvent(const int id,
366416
}
367417
}
368418

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+
369427
// Call Panel's event handler only if it is not a CHARTEVENT_CHART_CHANGE - workaround for minimization bug on chart switch.
370428
if (id != CHARTEVENT_CHART_CHANGE) ExtDialog.OnEvent(id, lparam, dparam, sparam);
371429

@@ -378,14 +436,17 @@ void OnChartEvent(const int id,
378436
{
379437
if (sparam == ObjectPrefix + "StopLossLine") ExtDialog.UpdateFixedSL();
380438
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))
382440
{
383441
int len = StringLen(ObjectPrefix + "TakeProfitLine");
384442
int i = (int)StringToInteger(StringSubstr(sparam, len));
385443
ExtDialog.UpdateAdditionalFixedTP(i);
386444
}
387445
}
388446

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+
389450
if (id != CHARTEVENT_CHART_CHANGE) ExtDialog.RefreshValues();
390451

391452
static bool prev_chart_on_top = false;

0 commit comments

Comments
 (0)