Skip to content

Commit 39684ac

Browse files
author
EarnForex
authored
1.10
Warning! The MT5 version now uses UTF-8 settings file format. After you update the Account Protector to version 1.10, it will likely cause a failure to load your old settings file correctly. 1. Added an option to calculate only realized profit/loss in the daily profit/loss conditions (CountFloatingInDailyPL). 2. Added a trigger delay option (ConditionDelay). 3. Added an option to load your custom settings file with panel fields' values (Settings file). 4. Added four new conditions based on the number of positions and pending orders. 5. Added a global ON/OFF state for the panel. 6. Added a new filter to the Filters tab to target long and short trades separately. 7. Changed how you can control the way the positions are sorted before closing. 8. Fixed the file encoding used for the panel's settings file in MT5. It is now UTF-8. 9. Fixed a bug in the MT4 version of the EA that resulted in deposits and withdrawals being counted in the daily profit/loss conditions. 10. Fixed decimal places normalization in some condition fields. 11. Fixed the placement of fields on the Filters tab to avoid overlapping problems in MT4. 12. Optimized the EA initialization and deinitialization phases to significantly improve the loading speed when switching the chart symbol. 13. Removed the PanelOnTopOfChart input parameter. Use the 'Chart on foreground' checkbox in the chart's settings instead.
1 parent bfc91b7 commit 39684ac

7 files changed

Lines changed: 935 additions & 149 deletions

File tree

MQL4/Experts/Account Protector/Account Protector.mq4

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//+------------------------------------------------------------------+
66
#property copyright "EarnForex.com"
77
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
8-
#property version "1.091"
9-
string Version = "1.091";
8+
#property version "1.10"
9+
string Version = "1.10";
1010
#property strict
1111

1212
#property description "Protects account balance by applying given actions when set conditions trigger."
@@ -15,15 +15,15 @@ string Version = "1.091";
1515

1616
#include "Account Protector.mqh";
1717

18-
input int Slippage = 2; // Slippage
19-
input string LogFileName = "log.txt"; // Log file name
18+
input string ____Main = "";
2019
input bool EnableEmergencyButton = false; // Enable emergency button
21-
input bool PanelOnTopOfChart = true; // PanelOnTopOfChart: Draw chart as background?
2220
input bool DoNotDisableConditions = false; // DoNotDisableConditions: Don't disable conditions on trigger?
2321
input bool DoNotDisableActions = false; // DoNotDisableActions: Don't disable actions on trigger?
2422
input bool DoNotDisableEquityTS = false; // DoNotDisableEquityTS: Don't disable equity TS on trigger?
2523
input bool DoNotDisableTimer = false; // DoNotDisableTimer: Don't disable timer on trigger?
26-
input bool AlertOnEquityTS = false; // AlertOnEquityTS: Alert when equity trailing stop triggers?
24+
input int ConditionDelay = 0; // ConditionDelay: How long should condition be active to trigger?
25+
input bool CountFloatingInDailyPL = true; // CountFloatingInDailyPL: Count floating P/L in daily P/L?
26+
input string ____Conditions = "";
2727
input bool DisableFloatLossRisePerc = false; // Disable floating loss rises % condition.
2828
input bool DisableFloatLossFallPerc = true; // Disable floating loss falls % condition.
2929
input bool DisableFloatLossRiseCurr = false; // Disable floating loss rises currency units condition.
@@ -50,25 +50,49 @@ input bool DisableDailyProfitLossPointsGE = true; // Disable daily profit/loss g
5050
input bool DisableDailyProfitLossPointsLE = true; // Disable daily profit/loss level less or equal points condition.
5151
input bool DisableDailyProfitLossPercGE = true; // Disable daily profit/loss greater or equal percentage condition.
5252
input bool DisableDailyProfitLossPercLE = true; // Disable daily profit/loss level less or equal percentage condition.
53+
input bool DisableNumberOfPositionsGE = true; // Disable number of positions greater or equal condition.
54+
input bool DisableNumberOfOrdersGE = true; // Disable number of pending orders greater or equal condition.
55+
input bool DisableNumberOfPositionsLE = true; // Disable number of positions less or equal condition.
56+
input bool DisableNumberOfOrdersLE = true; // Disable number of pending orders less or equal condition.
57+
input string ____Trading = "";
5358
input int DelayOrderClose = 0; // DelayOrderClose: Delay in milliseconds.
5459
input bool UseTotalVolume = false; // UseTotalVolume: enable if trading with many small trades and partial position closing.
60+
input ENUM_CLOSE_TRADES CloseFirst = ENUM_CLOSE_TRADES_DEFAULT; // CloseFirst: Close which trades first?
61+
input bool BreakEvenProfitInCurrencyUnits = false; // BreakEvenProfitInCurrencyUnits: currency instead of points.
62+
input string ____Miscellaneous = "";
63+
input bool AlertOnEquityTS = false; // AlertOnEquityTS: Alert when equity trailing stop triggers?
5564
input double AdditionalFunds = 0; // AdditionalFunds: Added to balance, equity, and free margin.
5665
input string Instruments = ""; // Instruments: Default list of trading instruments for order filtering.
57-
input bool CloseMostDistantFirst = false; // CloseMostDistantFirst: Close most distant trades first?
58-
input bool BreakEvenProfitInCurrencyUnits = false; // BreakEvenProfitInCurrencyUnits: currency instead of points.
5966
input bool GlobalSnapshots = false; // GlobalSnapshots: AP instances share equity & margin snapshots.
67+
input int Slippage = 2; // Slippage
68+
input string LogFileName = "log.txt"; // Log file name
69+
input string SettingsFileName = ""; // Settings file: Load custom panel settings from \Files\ folder.
6070

6171
CAccountProtector ExtDialog;
6272

73+
int DeinitializationReason = -1;
74+
6375
//+------------------------------------------------------------------+
6476
//| Initialization function |
6577
//+------------------------------------------------------------------+
6678
int OnInit()
6779
{
80+
if (DeinitializationReason == REASON_CHARTCHANGE)
81+
{
82+
EventSetTimer(1);
83+
return INIT_SUCCEEDED;
84+
}
85+
6886
MathSrand(GetTickCount() + 2202051901); // Used by CreateInstanceId() in Dialog.mqh (standard library). Keep the second number unique across other panel indicators/EAs.
6987

88+
if (SettingsFileName != "") // Load a custom settings file if given via input parameters.
89+
{
90+
ExtDialog.SetFileName(SettingsFileName);
91+
}
92+
7093
if (!ExtDialog.LoadSettingsFromDisk())
7194
{
95+
sets.OnOff = false;
7296
sets.CountCommSwaps = true;
7397
sets.UseTimer = false;
7498
sets.Timer = TimeToString(TimeCurrent() - 7200, TIME_MINUTES);
@@ -98,6 +122,7 @@ int OnInit()
98122
}
99123
sets.OrderCommentary = "";
100124
sets.intOrderCommentaryCondition = 0;
125+
sets.intOrderDirection = 0;
101126
sets.MagicNumbers = "";
102127
sets.boolExcludeMagics = false;
103128
sets.intInstrumentFilter = 0;
@@ -138,6 +163,10 @@ int OnInit()
138163
sets.boolDailyProfitLossPointsLE = false;
139164
sets.boolDailyProfitLossPercGE = false;
140165
sets.boolDailyProfitLossPercLE = false;
166+
sets.boolNumberOfPositionsGE = false;
167+
sets.boolNumberOfOrdersGE = false;
168+
sets.boolNumberOfPositionsLE = false;
169+
sets.boolNumberOfOrdersLE = false;
141170
sets.doubleLossPerBalance = 0;
142171
sets.doubleLossQuanUnits = 0;
143172
sets.intLossPoints = 0;
@@ -172,6 +201,10 @@ int OnInit()
172201
sets.intDailyProfitLossPointsLE = 0;
173202
sets.doubleDailyProfitLossPercGE= 0;
174203
sets.doubleDailyProfitLossPercLE = 0;
204+
sets.intNumberOfPositionsGE = 0;
205+
sets.intNumberOfOrdersGE = 0;
206+
sets.intNumberOfPositionsLE = 0;
207+
sets.intNumberOfOrdersLE = 0;
175208
sets.ClosePos = true;
176209
sets.doubleClosePercentage = 100;
177210
sets.CloseWhichPositions = All;
@@ -230,7 +263,6 @@ int OnInit()
230263
sets.boolBreakEvenExtra = false;
231264
}
232265

233-
EventSetTimer(1);
234266
if (!ExtDialog.Create(0, Symbol() + " Account Protector (ver. " + Version + ")", 0, 20, 20)) return(-1);
235267
ExtDialog.Run();
236268
ExtDialog.IniFileLoad();
@@ -242,19 +274,21 @@ int OnInit()
242274
ExtDialog.RefreshPanelControls();
243275
ExtDialog.RefreshValues();
244276

245-
ChartSetInteger(0, CHART_FOREGROUND, !PanelOnTopOfChart);
277+
EventSetTimer(1);
246278

247-
return(INIT_SUCCEEDED);
279+
return INIT_SUCCEEDED;
248280
}
249281

250282
//+------------------------------------------------------------------+
251283
//| Deinitialization function |
252284
//+------------------------------------------------------------------+
253285
void OnDeinit(const int reason)
254286
{
287+
DeinitializationReason = reason; // Remember reason to avoid recreating the panel in the OnInit() if it is not deleted here.
288+
EventKillTimer();
255289
if ((reason == REASON_REMOVE) || (reason == REASON_CHARTCLOSE) || (reason == REASON_PROGRAM))
256290
{
257-
ExtDialog.DeleteSettingsFile();
291+
if (SettingsFileName == "") ExtDialog.DeleteSettingsFile(); // Only delete settings file if no custom file name is given.
258292
Print("Trying to delete ini file.");
259293
if (!FileIsExist(ExtDialog.IniFileName() + ".dat")) Print("File doesn't exist.");
260294
else if (!FileDelete(ExtDialog.IniFileName() + ".dat")) Print("Failed to delete file: " + ExtDialog.IniFileName() + ".dat. Error: " + IntegerToString(GetLastError()));
@@ -266,14 +300,13 @@ void OnDeinit(const int reason)
266300
ExtDialog.SilentLogging = false;
267301
ExtDialog.Logging_Current_Settings();
268302
}
269-
else
303+
else if (reason != REASON_CHARTCHANGE)
270304
{
271305
if (reason == REASON_PARAMETERS) GlobalVariableSet("AP-" + IntegerToString(ChartID()) + "-Parameters", 1);
272306
ExtDialog.SaveSettingsOnDisk();
273307
ExtDialog.IniFileSave();
274308
}
275-
ExtDialog.Destroy();
276-
EventKillTimer();
309+
if (reason != REASON_CHARTCHANGE) ExtDialog.Destroy();
277310
}
278311

279312
//+------------------------------------------------------------------+
@@ -303,6 +336,7 @@ void OnChartEvent(const int id,
303336
void OnTick()
304337
{
305338
ExtDialog.RefreshValues();
339+
if (!sets.OnOff) return;
306340
ExtDialog.Trailing();
307341
ExtDialog.EquityTrailing();
308342
ExtDialog.MoveToBreakEven();
@@ -316,6 +350,7 @@ void OnTick()
316350
void OnTimer()
317351
{
318352
ExtDialog.RefreshValues();
353+
if (!sets.OnOff) return;
319354
ExtDialog.Trailing();
320355
ExtDialog.EquityTrailing();
321356
ExtDialog.MoveToBreakEven();

0 commit comments

Comments
 (0)