Panic Button
A panic button has to be right the first time, because by the time you reach for it you have no attention left to spare on checking its work. This one is built around the two failure modes that make the naive version dangerous. The first is ordering. Most published flatten scripts close positions and then cancel pendings, or do both in one pass through the order pool. Both leave a race: a pending order that triggers while the script is half way through the book hands you a brand new position at the exact moment you were trying to have none. This script deletes every pending first, so the book is sealed before it is emptied, and only then closes positions. The second is what happens behind you. If an expert advisor is running, the account can be flat for a second and then not be. The correct first step is to switch algorithmic trading off before touching anything, and neither MQL4 nor MQL5 will let a script do that. The terminal exposes the state of the button as a read-only property - IsExpertEnabled in MQL4, TERMINAL_TRADE_ALLOWED in MQL5 - and no function that changes it. It can be faked with synthetic keystrokes sent to the terminal window, and this script deliberately does not do that: it is undocumented, it breaks between builds, and it is not something a published tool should be doing to somebody else's terminal. What it does instead is verify. After the first sweep it re-counts both pools. If anything is still live, that is an EA re-opening behind the script, and it says so in the journal by name and count rather than reporting a clean run. It repeats the seal-and-empty cycle for as many verification passes as you allow, then finishes by telling you plainly whether the AutoTrading button is still on and that you need to press it yourself. Everything else follows from the same principle of not lying to you. Tickets are collected before anything is closed, because the order pool re-indexes the instant an order leaves it and a loop that closes while iterating skips roughly half the book. Every close and every delete is checked; temporary refusals such as requotes and off-quotes are retried, permanent ones are given up on and reported. An order that has already gone by the time the script reaches it is counted as done, not as a failure. Slippage is entered in pips and converted to points, so it means the same thing on a 5 digit quote as on a 2 digit one. No filters. No symbol restriction, no magic number, no profit threshold. A panic button that only flattens part of the account is not a panic button, and the one time you need it is the one time you should not be reading input labels. Notes and limitations - It cannot switch AutoTrading off. That is a platform limitation, not an oversight. Read the closing message and press the button. - It cannot close a position while the market for that symbol is closed. Those are reported as failures with the broker's reason, which is the honest answer. - A position frozen by the broker - inside the freeze level, or under a pending close order - will refuse to close and is reported rather than retried forever. - Partial fills are possible. A close that fills partially leaves a remainder, and the verification pass is what catches it. This is another reason not to set InpVerifyPasses to zero. - It does not close positions opened on other accounts, other terminals, or by a copier that is still running. Nothing running inside one terminal can. - Realised profit is not summarised. Deliberate: in an emergency the number you need is how much is still open, not how much you just lost, and the account history has the rest. - The script needs AutoTrading on in order to run at all. If it is already off, the script says so and stops rather than pretending it did something.
Script · MT4 · v1.0 · Free
About Panic Button
Panic Button is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Confirm first, Delete pendings and Slippage pips. MQL source code included.
Files in the download: PanicButton.ex4, PanicButton.mq4, PanicButton.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Confirm first | true | Ask for confirmation before anything is touched |
| Delete pendings | true | Delete pending orders as well as closing positions |
| Slippage pips | 5 | Slippage tolerance when closing, in pips |
| Max retries | 5 | Attempts per order before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Verify passes | 2 | Extra sweeps that catch an EA re-opening behind you |
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: Pending Orders · Alerts · MT4 Script · MQL4 Source Code