Delete All Pending Orders
When a setup stops being valid, the pending orders behind it stop being useful and start being dangerous. A breakout ladder left over the weekend, a set of limits below a level that has already broken - these fill when you are not watching, into a market you no longer have a view on. Clearing them from the Trade tab means right-clicking each one, and the number of them is precisely why you want a script. The value here is in what the script refuses to do. Cancelling an order and closing a position are different intentions, and a tool that mixes them cannot do either safely. This one deletes pending orders and never touches live exposure. That distinction is much harder to get right in MQL4 than it looks, because MQL4 keeps positions and pending orders in a single pool. OrdersTotal returns both. Every order in that pool is separated only by its type field: OP_BUY and OP_SELL are live positions, and everything above them is pending. A script that walks the pool without testing the type will happily close your entire book while the log says "deleting pending orders". That one test is the difference between a useful tool and an expensive accident, and it is why this script tests the type twice - once when building the list, and once again on the order actually selected at delete time, because a buy stop can become a live position in the milliseconds between the two. The second trap is the loop direction. The order pool re-indexes the instant an order leaves it. Walking it forwards from zero while deleting means index 3 becomes index 2 while you are still moving to index 4, and roughly half the book survives the run. This script does not iterate the pool while modifying it at all: it collects the tickets first, then deletes them by ticket. That is the only pattern that lets each result be checked and reported individually. The rest is ordinary care that most published versions skip. Every OrderSelect return value is checked, because it fails more often than people expect. Every delete result is checked. Temporary refusals - server busy, off quotes, no connection, trade context held by an EA on another chart - are retried with a pause. Permanent refusals are not retried, because retrying a permanent error just burns time and fills the journal. An order that triggered or expired between the list being built and the delete being sent is reported as skipped rather than counted as a failure, because nothing went wrong. The summary at the end tells you how many were deleted, how many went away by themselves, and how many the broker refused. Notes and limitations - It deletes pending orders. It does not close positions and it does not remove stop losses or take profits, which are attached to positions rather than being orders in this pool. - An order the broker has frozen - typically one sitting inside the freeze level with price approaching it - cannot be deleted while that lasts. The condition usually clears within a second or two as price moves off the level, so a frozen refusal is retried for as many attempts as you allow and only reported as a failure if it survives all of them. - Orders that trigger while the script is running are reported as skipped. The script cannot un-trigger them, and by the time it notices, the fill has already happened. - There is no undo. The confirmation dialog is the only safety net, and turning it off for a hotkey removes it. The symbol filter is the better habit. - Expiry times, comments and attached stop levels are gone with the order. Note anything you intend to re-place before you run this. - On MQL4 the script needs automated trading enabled, exactly like any other trading program. It says so plainly instead of failing silently, which is worth more than it sounds when a hotkey appears to do nothing.
Script · MT5 · v1.0 · Free
About Delete All Pending Orders
Delete All Pending Orders is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders. It has 5 settings, such as Current symbol only, Magic filter and Max retries. MQL source code included.
Files in the download: DeleteAllPendings.ex5, DeleteAllPendings.mq5, DeleteAllPendings.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 |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 deletes every magic) |
| Max retries | 5 | Attempts per order before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before deleting |
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: Breakout · Pending Orders · MT5 Script · MQL5 Source Code