Stop Out Sequence Simulator
Most traders only find out what their broker's stop out actually does the first time it happens to them, live, with real money already gone. This script runs the same cascade as a projection over the book exactly as it stands right now, so the mechanics can be understood ahead of time. The projection uses the overwhelmingly common broker convention: close the position carrying the largest floating loss first, one at a time, then re-check the margin level, repeating until it recovers above the stop-out threshold or nothing is left. Each position's margin contribution is estimated from MODE_MARGINREQUIRED, which already folds in that symbol's real contract size and leverage - never a hand-rolled contract-size formula, which would be quietly wrong on indices, metals and other CFDs. Because the sum of per-position estimates rarely matches the broker's real total margin exactly (rounding, cross-margining, hedged-position netting on some brokers), every estimate is scaled by one ratio so the running total agrees with the account's real AccountMargin() figure, while the ORDER of closure still follows worst-loss-first. The one limitation that matters most: MetaTrader exposes no API for "which position does this broker's dealing engine actually pick first" - that decision runs entirely server-side and is broker-specific. A broker that closes the whole book in one action, or picks by margin usage rather than floating loss, will play out differently from this narration. This script is a same-account approximation of the common convention, not a guarantee of what a specific broker will do. Notes and limitations - Report-only. No order is ever opened, closed or modified. - The closure order assumed (largest floating loss first) is the common convention, not a guarantee - confirm this broker's actual stop-out policy in its terms. - Per-position margin is an estimate built from MODE_MARGINREQUIRED, scaled to match the account total; it can still diverge from the broker's exact internal figure on hedged or cross-margined books. - If the account currently sits above the stop-out threshold, the printed sequence is a stress test of "what would happen if it were triggered right now", not a forecast that it will be. - Requires at least one open market position and nonzero AccountMargin() to produce a sequence; both are checked and reported plainly if missing.
Script · MT5 · v1.0 · Free
About Stop Out Sequence Simulator
Stop Out Sequence Simulator is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 1 setting, such as Use estimated margin only. MQL source code included.
Files in the download: StopOutSequenceSimulator.ex5, StopOutSequenceSimulator.mq5, StopOutSequenceSimulator.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Use estimated margin only | false | If true, scale estimated per-position margin so totals match ACCOUNT_MARGIN exactly |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 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 · MT5 Script · MQL5 Source Code