Strategy Loss Firewall
Running several strategies on one account under different magic numbers means a single one going wrong can otherwise burn through a shared daily loss limit and take every other strategy down with it. This EA firewalls exactly ONE magic number per instance - attach one copy per strategy you want isolated - and tracks that magic's own daily loss against its own budget, independent of whatever the rest of the account is doing. On breach, it closes only that magic's own open orders (pending orders included) and sets a GlobalVariable named StrategyLossFirewall_Paused_<magic> to 1. That flag is the coordination contract: a strategy EA sharing the same magic number can check GlobalVariableGet() for that name before sending a new order and skip the entry while it reads 1. No MQL4 program can reach into another EA's OnTick and block an OrderSend call directly - there is no such hook on either platform - so the pause flag is advisory to the strategy EA even though the position-closing action taken here is real, not advisory. The daily budget resets automatically at the next trading day unless InpAutoResetNextDay is turned off, in which case the pause persists until InpManualResetRequest is used to clear it. Notes and limitations - The day's realised P/L component is read from account balance, which is an account-wide number in MQL4, not a per-magic one. On an account running several firewalled magics side by side, each instance's day P/L figure therefore includes every magic's realised trades, not only its own - only the floating P/L half is genuinely scoped to this instance's magic. Treat the daily figure as a reasonable approximation, not an exact per-magic ledger, when more than one magic trades the same account. - The pause flag only pauses NEW entries if the strategy EA itself checks it - this firewall cannot force another program to read a GlobalVariable, it can only publish one and close what it can see. - Flattening requires AutoTrading to be enabled; if it is off when the budget is breached, the EA alerts loudly but cannot act, and says so.
Expert advisor · MT4 · v1.0 · Free
About Strategy Loss Firewall
Strategy Loss Firewall is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 10 settings, such as Magic number, Limit is percent and Daily loss limit. MQL source code included.
Files in the download: StrategyLossFirewall.ex4, StrategyLossFirewall.mq4, StrategyLossFirewall.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic number | 45200 | The single magic number this instance firewalls |
| Limit is percent | true | true = InpDailyLossLimit is % of day-open balance, false = account currency |
| Daily loss limit | 3.0 | Loss allowed for this magic in one day before pausing it |
| Close on breach | true | Close this magic's own open orders when the budget is breached |
| Auto reset next day | true | Clear the pause automatically at the next daily rollover |
| Manual reset request | false | Set true and re-attach (or update inputs) to clear the pause immediately |
| Dry run | false | true = alert only when breached, never actually closes anything |
| Timer interval sec | 15 | How often the budget is checked |
| Max retries | 5 | Attempts per order action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT4 Expert Advisor · MQL4 Source Code