Spread Aware Panic Close
A plain "close everything now" script fires straight into whatever spread the broker happens to be quoting at that instant. Around rollover, a news release, or a thin session open, that can be five to ten times the normal spread for anywhere from a few seconds to a few minutes. An emergency exit that eats that spread on top of the position loss it was meant to cap makes an already bad moment materially worse, and rollover in particular is entirely predictable in timing - there is usually no reason to pay a rollover-priced spread on an exit that could wait ninety seconds. This script polls the spread on every symbol carrying an open position against a threshold decided one of two ways: a fixed pip ceiling typed in directly, or a multiple of the spread actually observed for that symbol when the script started (the default). Whichever symbol reaches an acceptable spread first is flattened first; a symbol that never calms down is force-closed the moment its own hard deadline passes, so the script never simply waits forever hoping conditions improve. Each symbol carries its own independent deadline, so one wide symbol does not delay the exit of a book that is otherwise fine. The wait is a real wait: the script polls on an interval and sleeps between checks, checking for a user or terminal stop request each pass so it can be interrupted cleanly rather than run to completion no matter what. Notes and limitations - This is a foreground script: it occupies the thread it runs on for as long as any symbol is still waiting, up to InpMaxWaitSeconds. It is not a background monitor and does not survive the chart being closed. - Baseline mode's threshold is only as good as the spread sampled at the single instant the script started. It is not a multi-tick average. - A symbol whose spread never recovers is still closed, at the deadline, at whatever price is available. This script bounds how long you wait for a better price - it does not guarantee one. - Close slippage is set deliberately generous, since the purpose of this exit is certainty once the deadline or the threshold is met, not the best possible fill. - Positions opened on a symbol after the script has already started waiting on it are not picked up - the symbol plan is built once at the start of the run.
Script · MT5 · v1.0 · Free
About Spread Aware Panic Close
Spread Aware Panic Close is an expert advisor (trading robot) for MetaTrader 5. It increases the lot size after a losing trade (martingale). It has 9 settings, such as Spread mode, Max spread pips and Baseline multiplier. MQL source code included.
Files in the download: SpreadAwarePanicClose.ex5, SpreadAwarePanicClose.mq5, SpreadAwarePanicClose.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 |
|---|---|---|
| Spread mode | SPREAD_BASELINE_MULTIPLE | How to judge "abnormal" |
| Max spread pips | 5.0 | Fixed-pips mode: acceptable spread ceiling |
| Baseline multiplier | 2.0 | Baseline mode: multiple of the spread seen at start |
| Max wait seconds | 120 | Hard deadline per symbol before forcing the close |
| Poll interval ms | 1000 | How often to re-check spread while waiting |
| Magic filter | -1 | Magic number (-1 covers every magic) |
| Confirm first | true | Ask for confirmation before starting |
| Max retries | 5 | Attempts per position before giving up |
| Retry wait ms | 500 | Pause between close attempts, milliseconds |
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: Trading Sessions · News Trading · Martingale · MT5 Script · MQL5 Source Code