Loading…

Stale Pending Order Cleaner

Grid systems, breakout straddles and news-event setups all tend to leave pending orders behind once the setup they were placed for has expired - either price never reached them, or too much time passed for the original idea to still be valid. Left alone, those orders accumulate and eventually trigger on a level that no longer means anything. This EA scans the pending-order pool once per new bar and deletes anything that fails either of two independent checks: older than an age limit in hours, or further than a distance limit in pips from the current price. Either check can be disabled on its own by setting its input to zero, so the EA can run as an age-only cleaner, a distance-only cleaner, or both together. The scan collects every stale ticket and its reason before deleting anything. The pending pool re-indexes the instant an order leaves it, so deleting while iterating by position risks skipping an order that shifted into the slot just vacated - collecting first and acting on tickets afterward avoids that entirely. Each deletion is logged with its ticket number and which check it failed, so the journal reads as an audit trail rather than a bare "deleted" line. Checking once per new bar rather than every tick is a deliberate choice, not a shortcut: an order's age in hours and its distance in pips do not change meaningfully tick to tick, and scanning the whole pool that often would be wasted work for a number that only needs a periodic check. Notes and limitations - Market positions are never touched. Only the four pending types - buy limit, sell limit, buy stop, sell stop - are considered. - At least one of InpMaxAgeHours or InpMaxDistancePips must be greater than zero, or OnInit refuses to start and says why. - The distance check compares against the current bid for the order's own symbol, not the chart's symbol, so a single instance can safely manage pending orders across multiple symbols when the magic number filter matches them. - Deletion is retried a small, fixed number of times only on the classic temporary broker/server errors. A permanent rejection is logged and left for the next scan rather than retried indefinitely. - Runs once immediately on attach in addition to every new bar after that, so a book that is already stale when the EA is attached is cleaned right away rather than waiting for the next bar. - On MQL5, a pending order that triggers between scans leaves the order pool under a brand-new position, not this ticket - this EA has nothing further to do with it once that happens, which is the expected, harmless case when OrderSelect() on the old ticket returns false during a delete pass.

Expert advisor · MT4 · v1.0 · Free

About Stale Pending Order Cleaner

Stale Pending Order Cleaner is a script for MetaTrader 4. It has 3 settings, such as Magic number, Max age hours and Max distance pips. MQL source code included.

Files in the download: PendingCleaner.ex4, PendingCleaner.mq4, PendingCleaner.txt.

Settings

SettingDefaultWhat it does
Magic number-1Magic number filter, -1 = all magic numbers
Max age hours48Delete pending orders older than this, hours (0 = ignore age)
Max distance pips300Delete pending orders further than this from price, pips (0 = ignore distance)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Grid · Breakout · News Trading · Pending Orders · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria