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 = " " ;
2019input bool EnableEmergencyButton = false ; // Enable emergency button
21- input bool PanelOnTopOfChart = true ; // PanelOnTopOfChart: Draw chart as background?
2220input bool DoNotDisableConditions = false ; // DoNotDisableConditions: Don't disable conditions on trigger?
2321input bool DoNotDisableActions = false ; // DoNotDisableActions: Don't disable actions on trigger?
2422input bool DoNotDisableEquityTS = false ; // DoNotDisableEquityTS: Don't disable equity TS on trigger?
2523input 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 = " " ;
2727input bool DisableFloatLossRisePerc = false ; // Disable floating loss rises % condition.
2828input bool DisableFloatLossFallPerc = true ; // Disable floating loss falls % condition.
2929input bool DisableFloatLossRiseCurr = false ; // Disable floating loss rises currency units condition.
@@ -50,25 +50,49 @@ input bool DisableDailyProfitLossPointsGE = true; // Disable daily profit/loss g
5050input bool DisableDailyProfitLossPointsLE = true ; // Disable daily profit/loss level less or equal points condition.
5151input bool DisableDailyProfitLossPercGE = true ; // Disable daily profit/loss greater or equal percentage condition.
5252input 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 = " " ;
5358input int DelayOrderClose = 0 ; // DelayOrderClose: Delay in milliseconds.
5459input 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?
5564input double AdditionalFunds = 0 ; // AdditionalFunds: Added to balance, equity, and free margin.
5665input 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.
5966input 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
6171CAccountProtector ExtDialog ;
6272
73+ int DeinitializationReason = -1 ;
74+
6375//+------------------------------------------------------------------+
6476//| Initialization function |
6577//+------------------------------------------------------------------+
6678int 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//+------------------------------------------------------------------+
253285void 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,
303336void 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()
316350void OnTimer ()
317351{
318352 ExtDialog .RefreshValues ();
353+ if (!sets .OnOff ) return ;
319354 ExtDialog .Trailing ();
320355 ExtDialog .EquityTrailing ();
321356 ExtDialog .MoveToBreakEven ();
0 commit comments