Declarative Pending Set Manager
The common way to keep a pending-order ladder in sync with a recalculated strategy is to delete every existing order for the magic number and resend the whole set from scratch on every recalculation. That is wasteful - every order gets a fresh ticket and fresh spread/slippage exposure even when its price has not actually changed - and it is fragile under partial failure: a delete that succeeds followed by a send that fails leaves a real hole in the ladder that the next naive rebuild will not necessarily notice, because it is comparing against nothing rather than against what should be there. This header keeps a small class that holds a caller-declared desired set (symbol, type, price, SL, TP, lots, magic, an optional comment used as the matching key) and a BuildPlan() call that diffs it against the broker's actual pending book for that magic, filtered by symbol. Matching prefers an exact comment match, falling back to the first unmatched order sharing symbol and type. A matched pair within a caller-chosen price/SL/TP tolerance in points produces nothing in the plan at all - the order is already correct and is never touched. Only a genuine mismatch becomes a MODIFY, only a desired leg with nothing to match becomes an ADD, and only a broker order matching no desired leg becomes a DELETE. BuildPlan() sends nothing - it only reads the order pool and returns a plan array the caller can inspect, exactly like the rest of this catalogue's trade-management headers separate computation from action. ApplyPlan() executes a plan, continuing past individual rejections by default so one broker-side refusal does not stall convergence of the rest of the ladder; Converge() is a convenience wrapper that does both in one call. Notes and limitations - BuildPlan() only ever reads the order pool - nothing about it can lose money by itself. ApplyPlan()/Converge() do send real orders once you wire them in; test the plan a BuildPlan() call produces before calling either. - Matching by symbol+type alone (when no comment is given) is order-of- declaration dependent when more than one desired leg shares symbol and type - give every such leg a distinct comment to make matching unambiguous instead of relying on declaration order. - Lots are normalised to the symbol's MODE_LOTSTEP/MODE_MINLOT/MODE_MAXLOT before being sent or compared; price/SL/TP are normalised to the symbol's digit count. Nothing here assumes a fixed contract size or point value. - ApplyPlan() does not retry a failed step itself - it records the error code on that plan entry and, by default, continues to the next step. Wrap it in your own retry loop (or this catalogue's FreezeAwareModifyQueue for the MODIFY leg specifically) if you need automatic retry. - Tolerance is expressed in points, not pips - convert with the same 3/5-digit pip factor used everywhere else in this catalogue if you want to think about it in pips.
Script · MT4 · v1.0 · Free
About Declarative Pending Set Manager
Declarative Pending Set Manager is an expert advisor (trading robot) for MetaTrader 4. It opens a grid of orders at fixed price steps and places pending buy/sell stop or limit orders. It has 4 settings, such as Demo magic, Ladder step pips and Lots. MQL source code included.
Files in the download: PendingSetManager.mqh, PendingSetManager.txt, PendingSetManagerDemo.mq4.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Demo magic | 990211 | Magic number the demo's synthetic ladder uses |
| Ladder step pips | 20.0 | Distance between successive ladder legs, in pips |
| Lots | 0.01 | Lot size for every declared leg |
| Tolerance pips | 1.0 | Price/SL/TP tolerance before a leg is reported MODIFY |
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 → Experts.
- 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 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: Pending Orders · Grid · MT4 Script · MQL4 Source Code