Skip to content

Commit 9fc5e6e

Browse files
authored
1.03
1. Added a cTrader version of Trailing Stop on Profit. 2. Added chart corner selection for the panel. 3. Added font size selection for the panel. 4. Added automatic scaling for Hi-DPI screens. 5. Fixed a compilation error in newer builds of MT5. 6. Changed how autotrading status is detected and reported. The EA will now be more specific.
1 parent ecadedd commit 9fc5e6e

File tree

4 files changed

+435
-77
lines changed

4 files changed

+435
-77
lines changed

MQL4/Experts/Trailing Stop on Profit.mq4

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#property link "https://www.earnforex.com/metatrader-expert-advisors/Trailing-Stop-on-Profit/"
2-
#property version "1.02"
2+
#property version "1.03"
33
#property strict
4-
#property copyright "EarnForex.com - 2023"
5-
#property description "This Expert Advisor will start trailing the stop-loss after a given profit is reached."
6-
#property description " "
4+
#property copyright "EarnForex.com - 2023-2025"
5+
#property description "This expert advisor will start trailing the stop-loss after a given profit is reached."
6+
#property description ""
77
#property description "WARNING: No warranty. This EA is offered \"as is\". Use at your own risk.\r\n"
88
#property icon "\\Files\\EF-Icon-64x64px.ico"
99

@@ -38,13 +38,25 @@ input bool ShowPanel = true; // Show graphical panel
3838
input string ExpertName = "TSOP"; // Expert name (to name the objects)
3939
input int Xoff = 20; // Horizontal spacing for the control panel
4040
input int Yoff = 20; // Vertical spacing for the control panel
41+
input ENUM_BASE_CORNER ChartCorner = CORNER_LEFT_UPPER; // Chart Corner
42+
input int FontSize = 10; // Font Size
4143

4244
int OrderOpRetry = 5; // Number of order modification attempts.
45+
double DPIScale; // Scaling parameter for the panel based on the screen DPI.
46+
int PanelMovY, PanelLabX, PanelLabY, PanelRecX;
4347
bool EnableTrailing = EnableTrailingParam;
4448

4549
void OnInit()
4650
{
4751
EnableTrailing = EnableTrailingParam;
52+
53+
DPIScale = (double)TerminalInfoInteger(TERMINAL_SCREEN_DPI) / 96.0;
54+
55+
PanelMovY = (int)MathRound(20 * DPIScale);
56+
PanelLabX = (int)MathRound(150 * DPIScale);
57+
PanelLabY = PanelMovY;
58+
PanelRecX = PanelLabX + 4;
59+
4860
if (ShowPanel) DrawPanel();
4961
}
5062

@@ -193,37 +205,42 @@ string PanelBase = ExpertName + "-P-BAS";
193205
string PanelLabel = ExpertName + "-P-LAB";
194206
string PanelEnableDisable = ExpertName + "-P-ENADIS";
195207

196-
int PanelMovX = 50;
197-
int PanelMovY = 20;
198-
int PanelLabX = 150;
199-
int PanelLabY = PanelMovY;
200-
int PanelRecX = PanelLabX + 4;
201-
202208
void DrawPanel()
203209
{
210+
int SignX = 1;
211+
int YAdjustment = 0;
212+
if ((ChartCorner == CORNER_RIGHT_UPPER) || (ChartCorner == CORNER_RIGHT_LOWER))
213+
{
214+
SignX = -1; // Correction for right-side panel position.
215+
}
216+
if ((ChartCorner == CORNER_RIGHT_LOWER) || (ChartCorner == CORNER_LEFT_LOWER))
217+
{
218+
YAdjustment = (PanelMovY + 2) * 2 + 1 - PanelLabY; // Correction for upper side panel position.
219+
}
220+
204221
string PanelText = "TSL on Profit";
205222
string PanelToolTip = "Trailing Stop on Profit by EarnForex";
206223
int Rows = 1;
207224
ObjectCreate(ChartID(), PanelBase, OBJ_RECTANGLE_LABEL, 0, 0, 0);
225+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_CORNER, ChartCorner);
208226
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XDISTANCE, Xoff);
209-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YDISTANCE, Yoff);
227+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YDISTANCE, Yoff + YAdjustment);
210228
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XSIZE, PanelRecX);
211-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 2) * 1 + 2);
229+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 1) * (Rows + 1) + 3);
212230
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_BGCOLOR, clrWhite);
213231
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_BORDER_TYPE, BORDER_FLAT);
214232
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_STATE, false);
215233
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_HIDDEN, true);
216-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_FONTSIZE, 8);
217234
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_SELECTABLE, false);
218235
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_COLOR, clrBlack);
219236

220237
DrawEdit(PanelLabel,
221-
Xoff + 2,
238+
Xoff + 2 * SignX,
222239
Yoff + 2,
223240
PanelLabX,
224241
PanelLabY,
225242
true,
226-
10,
243+
FontSize,
227244
PanelToolTip,
228245
ALIGN_CENTER,
229246
"Consolas",
@@ -232,6 +249,7 @@ void DrawPanel()
232249
clrNavy,
233250
clrKhaki,
234251
clrBlack);
252+
ObjectSetInteger(ChartID(), PanelLabel, OBJPROP_CORNER, ChartCorner);
235253

236254
string EnableDisabledText = "";
237255
color EnableDisabledColor = clrNavy;
@@ -250,12 +268,12 @@ void DrawPanel()
250268
}
251269

252270
DrawEdit(PanelEnableDisable,
253-
Xoff + 2,
254-
Yoff + (PanelMovY + 1)*Rows + 2,
271+
Xoff + 2 * SignX,
272+
Yoff + (PanelMovY + 1) * Rows + 2,
255273
PanelLabX,
256274
PanelLabY,
257275
true,
258-
8,
276+
FontSize,
259277
"Click to enable or disable the trailing stop feature",
260278
ALIGN_CENTER,
261279
"Consolas",
@@ -264,11 +282,7 @@ void DrawPanel()
264282
EnableDisabledColor,
265283
EnableDisabledBack,
266284
clrBlack);
267-
268-
Rows++;
269-
270-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XSIZE, PanelRecX);
271-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 1)*Rows + 3);
285+
ObjectSetInteger(ChartID(), PanelEnableDisable, OBJPROP_CORNER, ChartCorner);
272286
}
273287

274288
void CleanPanel()
@@ -280,11 +294,17 @@ void ChangeTrailingEnabled()
280294
{
281295
if (EnableTrailing == false)
282296
{
283-
if (IsTradeAllowed()) EnableTrailing = true;
284-
else
297+
if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
298+
{
299+
MessageBox("Automated trading is disabled in the platform's options! Please enable it via Tools->Options->Expert Advisors.", "WARNING", MB_OK);
300+
return;
301+
}
302+
if (!MQLInfoInteger(MQL_TRADE_ALLOWED))
285303
{
286-
MessageBox("You need to first enable Autotrading in your MetaTrader options", "WARNING", MB_OK);
304+
MessageBox("Live Trading is disabled in the expert advisors's settings! Please tick the Allow Live Trading checkbox on the Common tab.", "WARNING", MB_OK);
305+
return;
287306
}
307+
EnableTrailing = true;
288308
}
289309
else EnableTrailing = false;
290310
DrawPanel();

MQL4/Include/MQLTA Utils.mqh

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
#property link "https://www.earnforex.com/"
22
#property version "1.01"
33
#property strict
4-
#property copyright "EarnForex.com - 2020-2021"
5-
#property description ""
6-
#property description ""
7-
#property description ""
8-
#property description ""
9-
#property description "Find More on EarnForex.com"
4+
#property copyright "EarnForex.com - 2020-2024"
105

11-
//Draw an edit box with the specified parameters
12-
void DrawEdit( string Name,
13-
int XStart,
14-
int YStart,
15-
int Width,
16-
int Height,
17-
bool ReadOnly,
18-
int EditFontSize,
19-
string Tooltip,
20-
int Align,
21-
string EditFont,
22-
string Text,
23-
bool Selectable,
24-
color TextColor = clrBlack,
25-
color BGColor = clrWhiteSmoke,
26-
color BDColor = clrBlack
6+
// Draw an edit box with the specified parameters.
7+
void DrawEdit(string Name,
8+
int XStart,
9+
int YStart,
10+
int Width,
11+
int Height,
12+
bool ReadOnly,
13+
int EditFontSize,
14+
string Tooltip,
15+
int Align,
16+
string EditFont,
17+
string Text,
18+
bool Selectable,
19+
color TextColor = clrBlack,
20+
color BGColor = clrWhiteSmoke,
21+
color BDColor = clrBlack
2722
)
2823
{
2924

MQL5/Experts/Trailing Stop on Profit.mq5

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#property link "https://www.earnforex.com/metatrader-expert-advisors/Trailing-Stop-on-Profit/"
2-
#property version "1.02"
3-
#property strict
4-
#property copyright "EarnForex.com - 2023"
5-
#property description "This Expert Advisor will start trailing the stop-loss after a given profit is reached."
6-
#property description " "
2+
#property version "1.03"
3+
4+
#property copyright "EarnForex.com - 2023-2025"
5+
#property description "This expert advisor will start trailing the stop-loss after a given profit is reached."
6+
#property description ""
77
#property description "WARNING: No warranty. This EA is offered \"as is\". Use at your own risk.\r\n"
88
#property icon "\\Files\\EF-Icon-64x64px.ico"
99

@@ -39,14 +39,26 @@ input bool ShowPanel = true; // Show graphical panel
3939
input string ExpertName = "TSOP"; // Expert name (to name the objects)
4040
input int Xoff = 20; // Horizontal spacing for the control panel
4141
input int Yoff = 20; // Vertical spacing for the control panel
42+
input ENUM_BASE_CORNER ChartCorner = CORNER_LEFT_UPPER; // Chart Corner
43+
input int FontSize = 10; // Font Size
4244

4345
int OrderOpRetry = 5; // Number of position modification attempts.
46+
double DPIScale; // Scaling parameter for the panel based on the screen DPI.
47+
int PanelMovY, PanelLabX, PanelLabY, PanelRecX;
4448
bool EnableTrailing = EnableTrailingParam;
4549
CTrade *Trade;
4650

4751
void OnInit()
4852
{
4953
EnableTrailing = EnableTrailingParam;
54+
55+
DPIScale = (double)TerminalInfoInteger(TERMINAL_SCREEN_DPI) / 96.0;
56+
57+
PanelMovY = (int)MathRound(20 * DPIScale);
58+
PanelLabX = (int)MathRound(150 * DPIScale);
59+
PanelLabY = PanelMovY;
60+
PanelRecX = PanelLabX + 4;
61+
5062
if (ShowPanel) DrawPanel();
5163
Trade = new CTrade;
5264
}
@@ -59,7 +71,7 @@ void OnDeinit(const int reason)
5971

6072
void OnTick()
6173
{
62-
if (EnableTrailing) TrailingStop();
74+
if (EnableTrailing) DoTrailingStop();
6375
if (ShowPanel) DrawPanel();
6476
}
6577

@@ -87,7 +99,7 @@ void OnChartEvent(const int id,
8799
}
88100
}
89101

90-
void TrailingStop()
102+
void DoTrailingStop()
91103
{
92104
for (int i = PositionsTotal() - 1; i >= 0; i--)
93105
{
@@ -208,37 +220,42 @@ string PanelBase = ExpertName + "-P-BAS";
208220
string PanelLabel = ExpertName + "-P-LAB";
209221
string PanelEnableDisable = ExpertName + "-P-ENADIS";
210222

211-
int PanelMovX = 50;
212-
int PanelMovY = 20;
213-
int PanelLabX = 150;
214-
int PanelLabY = PanelMovY;
215-
int PanelRecX = PanelLabX + 4;
216-
217223
void DrawPanel()
218224
{
225+
int SignX = 1;
226+
int YAdjustment = 0;
227+
if ((ChartCorner == CORNER_RIGHT_UPPER) || (ChartCorner == CORNER_RIGHT_LOWER))
228+
{
229+
SignX = -1; // Correction for right-side panel position.
230+
}
231+
if ((ChartCorner == CORNER_RIGHT_LOWER) || (ChartCorner == CORNER_LEFT_LOWER))
232+
{
233+
YAdjustment = (PanelMovY + 2) * 2 + 1 - PanelLabY; // Correction for upper side panel position.
234+
}
235+
219236
string PanelText = "TSL on Profit";
220237
string PanelToolTip = "Trailing Stop on Profit by EarnForex";
221238
int Rows = 1;
222239
ObjectCreate(ChartID(), PanelBase, OBJ_RECTANGLE_LABEL, 0, 0, 0);
240+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_CORNER, ChartCorner);
223241
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XDISTANCE, Xoff);
224-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YDISTANCE, Yoff);
242+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YDISTANCE, Yoff + YAdjustment);
225243
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XSIZE, PanelRecX);
226-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 2) * 1 + 2);
244+
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 1) * (Rows + 1) + 3);
227245
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_BGCOLOR, clrWhite);
228246
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_BORDER_TYPE, BORDER_FLAT);
229247
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_STATE, false);
230248
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_HIDDEN, true);
231-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_FONTSIZE, 8);
232249
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_SELECTABLE, false);
233250
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_COLOR, clrBlack);
234251

235252
DrawEdit(PanelLabel,
236-
Xoff + 2,
253+
Xoff + 2 * SignX,
237254
Yoff + 2,
238255
PanelLabX,
239256
PanelLabY,
240257
true,
241-
10,
258+
FontSize,
242259
PanelToolTip,
243260
ALIGN_CENTER,
244261
"Consolas",
@@ -247,6 +264,7 @@ void DrawPanel()
247264
clrNavy,
248265
clrKhaki,
249266
clrBlack);
267+
ObjectSetInteger(ChartID(), PanelLabel, OBJPROP_CORNER, ChartCorner);
250268

251269
string EnableDisabledText = "";
252270
color EnableDisabledColor = clrNavy;
@@ -265,12 +283,12 @@ void DrawPanel()
265283
}
266284

267285
DrawEdit(PanelEnableDisable,
268-
Xoff + 2,
269-
Yoff + (PanelMovY + 1)*Rows + 2,
286+
Xoff + 2 * SignX,
287+
Yoff + (PanelMovY + 1) * Rows + 2,
270288
PanelLabX,
271289
PanelLabY,
272290
true,
273-
8,
291+
FontSize,
274292
"Click to enable or disable the trailing stop feature",
275293
ALIGN_CENTER,
276294
"Consolas",
@@ -279,11 +297,7 @@ void DrawPanel()
279297
EnableDisabledColor,
280298
EnableDisabledBack,
281299
clrBlack);
282-
283-
Rows++;
284-
285-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_XSIZE, PanelRecX);
286-
ObjectSetInteger(ChartID(), PanelBase, OBJPROP_YSIZE, (PanelMovY + 1)*Rows + 3);
300+
ObjectSetInteger(ChartID(), PanelEnableDisable, OBJPROP_CORNER, ChartCorner);
287301
ChartRedraw();
288302
}
289303

@@ -296,11 +310,17 @@ void ChangeTrailingEnabled()
296310
{
297311
if (EnableTrailing == false)
298312
{
299-
if (MQLInfoInteger(MQL_TRADE_ALLOWED)) EnableTrailing = true;
300-
else
313+
if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
314+
{
315+
MessageBox("Algorithmic trading is disabled in the platform's options! Please enable it via Tools->Options->Expert Advisors.", "WARNING", MB_OK);
316+
return;
317+
}
318+
if (!MQLInfoInteger(MQL_TRADE_ALLOWED))
301319
{
302-
MessageBox("You need to first enable Autotrading in your MetaTrader options", "WARNING", MB_OK);
320+
MessageBox("Algo Trading is disabled in the Position Sizer's settings! Please tick the Allow Algo Trading checkbox on the Common tab.", "WARNING", MB_OK);
321+
return;
303322
}
323+
EnableTrailing = true;
304324
}
305325
else EnableTrailing = false;
306326
DrawPanel();

0 commit comments

Comments
 (0)