Stop Distance Sanity Guard
A stop entered as 15 instead of 150, or 500 instead of 50, is one of the easiest manual-entry mistakes to make and one of the most expensive to leave unnoticed - either the position gets stopped out almost immediately on ordinary noise, or it carries far more risk than intended because the stop is effectively too far away to matter. A fixed pip threshold cannot catch this reliably across different instruments, since 50 points means something very different on EURUSD than it does on gold or an index CFD. This EA measures every open order's stop distance in multiples of that symbol's own ATR instead of a fixed pip count, so the same sane range works across instruments without separate tuning. Every order is checked the moment it is first seen and again any time its stop-loss value changes, so a fat-fingered edit some time after the trade was opened is caught too, not just the entry. A flagged order can be left alone (alert and log only, the default), closed outright, or have its stop automatically clamped back to a configured ATR multiple - whichever matches how hands-off the trader wants the guard to be. Because MQL4 cannot intercept another program's OrderSend before it fills, this is necessarily an after-the-fact guard: the order is allowed to open and is then acted on, not prevented from opening with the bad stop in the first place. Notes and limitations - After-the-fact guard, not prevention: a bad stop is allowed to exist for at least one tick before this EA reacts to it, since MQL4 cannot intercept another program's OrderSend before it fills. - ATR needs enough history to compute; a symbol whose ATR briefly reads 0 (e.g. just added to Market Watch) is skipped that tick and re-checked once ATR becomes available. - GUARD_CLOSE_ORDER closes the entire order at market, not a partial amount - there is no partial-size trim option. - GUARD_CLAMP_STOP only ever moves the stop; it never touches the take-profit or the position size. - Tracking of "already seen" tickets lives only in memory for the lifetime of this EA instance; removing it from the chart and reattaching re-evaluates every currently open order as if freshly seen.
Expert advisor · MT5 · v1.0 · Free
About Stop Distance Sanity Guard
Stop Distance Sanity Guard is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It has a news filter. It has 8 settings, such as Atr period, Min atr multiple and Max atr multiple. MQL source code included.
Files in the download: StopDistanceSanityGuard.ex5, StopDistanceSanityGuard.mq5, StopDistanceSanityGuard.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Atr period | 14 | ATR period used as the volatility yardstick |
| Min atr multiple | 0.2 | Flag a stop distance below this many ATRs (too tight) |
| Max atr multiple | 15.0 | Flag a stop distance above this many ATRs (too wide) |
| Flag missing stop | false | Also flag positions with no stop-loss set at all |
| Magic filter | -1 | Only guard this magic, -1 = every open position on the account |
| Action | GUARD_ALERT_ONLY | What to do once a stop is flagged |
| Clamp atr multiple | 3.0 | Target ATR multiple used by GUARD_CLAMP_STOP |
| Alert on flag | true | Popup Alert() for every flagged ticket |
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 → Experts.
- 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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: ATR · Alerts · News Trading · MT5 Expert Advisor · MQL5 Source Code