A daily loss limit catches a bad day. It does not catch a slow bleed, where an EA loses a little every day for two weeks and never trips the daily rule. For that you want a second guard measured from the highest equity the account has reached, often called a high water mark.
The idea is simple. Track the peak equity, compare current equity to it on every tick, and when the drop passes a set percentage, close everything and stop opening new trades until a human looks at it.
The tricky parts are persistence and restarts. If the peak lives only in a normal variable, a terminal restart resets it to the current equity and the guard quietly forgets the drawdown that already happened. So the peak below is stored in a terminal global variable, which survives restarts.
The guard
input double InpMaxDrawdownPct = 8.0; // max drop from equity peak, percent input long InpMagic = 240117; string PeakKey() { return "DDLOCK_PEAK_" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)); } string LockKey() { return "DDLOCK_FLAG_" + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)); } double PctOf(double value, double pct) { return value / (100.0 / pct); // pct percent of value } bool DrawdownLocked() { return GlobalVariableCheck(LockKey()) && GlobalVariableGet(LockKey()) > 0.5; } // Returns true when trading is allowed. bool CheckDrawdownLock() { if(DrawdownLocked()) return false; double equity = AccountInfoDouble(ACCOUNT_EQUITY); double peak = GlobalVariableCheck(PeakKey()) ? GlobalVariableGet(PeakKey()) : 0.0; if(equity > peak) { GlobalVariableSet(PeakKey(), equity); return true; } double floorLevel = peak - PctOf(peak, InpMaxDrawdownPct); if(equity <= floorLevel) { GlobalVariableSet(LockKey(), 1.0); PrintFormat("Drawdown lock: equity %.2f fell below %.2f (peak %.2f)", equity, floorLevel, peak); CloseAllOwnPositions(); return false; } return true; } CloseAllOwnPositions closes only positions with this EA's magic number:
#include <Trade\Trade.mqh> CTrade trade; void CloseAllOwnPositions() { for(int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket == 0) continue; if(PositionGetInteger(POSITION_MAGIC) != InpMagic) continue; if(!trade.PositionClose(ticket)) PrintFormat("Close failed for %I64u, retcode %u", ticket, trade.ResultRetcode()); } } Using it
int OnInit() { if(InpMaxDrawdownPct <= 0.0 || InpMaxDrawdownPct >= 100.0) return INIT_PARAMETERS_INCORRECT; trade.SetExpertMagicNumber(InpMagic); if(DrawdownLocked()) Print("Drawdown lock is active. Reset it manually after review."); return INIT_SUCCEEDED; } void OnTick() { if(!CheckDrawdownLock()) return; // normal signal and order logic here } Notes from using this in practice
- The lock is deliberately sticky. Unlocking is a manual step: delete the
DDLOCK_FLAG_global variable in the terminal (F3), and decide whether to also reset the peak. - Deposits and withdrawals move equity without any trading. If you withdraw, lower the stored peak by the same amount, or the guard will think you are in drawdown.
- Several EAs on one account can share the same keys if you want an account level guard, or include the magic number in the key names if each EA should have its own.
- In the Strategy Tester global variables are separate from the live terminal, so a backtest will not trip your live lock, but test the guard there first by setting a tiny percentage and checking that the close and the lock both happen.
A daily limit plus a peak based limit covers both the sharp loss and the slow one, and the second one costs about forty lines.

