Close All Positions
When a trade needs to be out of the market, it needs to be out now. Clicking through the terminal one position at a time is slow, and slow is expensive when price is moving against you. This script flattens the book in a single run. It closes market positions only. Pending orders are deliberately left alone, because closing a position and cancelling an unfilled order are two different intentions and a script that silently does both is a script you cannot trust in a hurry. Three details separate this from a ten-line loop. Tickets are collected into a list before anything is closed. Iterating the pool by index while closing is the single most common bug in scripts of this kind - the pool re-indexes the moment an entry leaves it, so a naive loop silently skips roughly half the book. Both versions here walk the list backwards and build the ticket list first. Every failure is retried, but only when the error deserves it. A requote, an off-quote, a busy trade context or a dropped connection are all temporary and worth another attempt. An invalid volume or a disabled account is permanent, and retrying it just burns time and fills the journal with noise. Slippage is entered in pips and converted to points internally, so the value means the same thing on a 3 or 5 digit broker as it does on a 4 digit one. The MQL5 version does that conversion per position rather than once, because a basket can hold a 5 digit currency pair and a 2 digit index at the same time and a single global deviation would be wrong for one of them. At the end you get a summary of how many closed, how many failed, and the total result. Notes and limitations - Market positions only. Pending orders are not deleted. Use Delete All Pending Orders for those. - A position that hits its own stop or target mid-run is treated as already closed and counted as a success, not a failure. It genuinely is closed. - The MQL4 trade context is shared across the terminal. If an EA is mid-order the script waits for it rather than failing, which can add a short delay. - Nothing here can close a position while the market for that symbol is shut. Those are reported as failures with the reason given. - The script does not touch stop loss or take profit levels, and does not cancel any EA that might immediately open a new position. Stop your EAs first, or they will simply trade back in. - A FIFO account can refuse to close positions out of order. The refusal comes from the server and is reported as-is.
Script · MT4 · v1.0 · Free
About Close All Positions
Close All Positions 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 Current symbol only, Magic filter and Slippage pips. MQL source code included.
Files in the download: CloseAllPositions.ex4, CloseAllPositions.mq4, CloseAllPositions.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 closes every magic) |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per order before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before closing |
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