Margin Level Floor Guard
A margin call or stop out is the moment a strategy's bug, a runaway grid, or simple bad luck turns into a forced, broker-chosen liquidation at the worst possible prices. This EA sits underneath everything else running on the account as a last-resort circuit breaker: when margin level drops to or below a configured floor percentage, it identifies the open position with the largest floating loss - net of swap and commission, matching an optional magic number filter - and closes it, then re-reads the fresh margin level and repeats if still needed. The floor and the restore level are deliberately two different numbers, not one shared threshold. Closing positions only until the account exactly reaches the floor again would leave it one tick from tripping the guard on the very next price update, closing another position, and another, in a tight and unnecessary cascade. The gap between floor and restore is hysteresis: the same reason a thermostat does not switch off the instant it reaches the target temperature. A per-tick close ceiling further limits how many positions can be liquidated in a single price update, so a fast V-shaped spike does not trigger an unnecessarily large single-tick wipe-out. This is a guard against catastrophe, not a trading strategy - it never opens a position, only ever closes existing ones, and only when the account is already in genuine danger. Notes and limitations Margin level is undefined when there is no used margin (no open positions); the EA correctly treats that as safe rather than as a false emergency. It reads equity and margin directly, the same numbers the broker's own stop-out logic uses, but it cannot guarantee it always beats a sudden gap or a very fast broker-side stop out - it is a second line of defence, not a replacement for sensible position sizing. It closes the worst performer by floating result only; it has no concept of which position is strategically most important to keep open.
Expert advisor · MT4 · v1.0 · Free
About Margin Level Floor Guard
Margin Level Floor Guard is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Floor percent, Restore percent and Magic filter. MQL source code included.
Files in the download: MarginLevelFloorGuard.ex4, MarginLevelFloorGuard.mq4, MarginLevelFloorGuard.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Floor percent | 150.0 | Margin level percent at or below which closing begins |
| Restore percent | 200.0 | Margin level percent that must be reached before closing stops (hysteresis) |
| Magic filter | -1 | Magic number filter, -1 = every magic number in the account |
| Max closes per tick | 1 | Positions closed per tick while the guard is active, throttles a closing cascade |
| Dry run | false | true = alert only, never closes - use to observe the trigger level first |
| Max retries | 5 | Attempts per close 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: Grid · Alerts · MT4 Expert Advisor · MQL4 Source Code