Minimal-Action Position Reconciler
Strategy code usually tracks "what should be open" as a running mental model and issues one trade request per signal. That model drifts from the account constantly - a stop loss fires unattended, a partial fill happens, a manual trade gets placed, a restart loses in-memory state - and nothing built into the platform closes the gap between what the strategy believes is open and what the broker actually shows. This header inverts the relationship. You declare a single number per symbol: the desired NET volume, positive for net long, negative for net short, zero for flat. Reconcile() reads the real position pool, computes the actual net for every watched symbol, and returns an action list that closes the difference using the fewest possible actions. The ordering is deliberate, not arbitrary. Closing an existing opposing ticket does two things in one action: it reduces exposure toward the target AND it removes a position from the book. Opening new volume only adds exposure. So the planner always spends available opposing tickets first - largest first, to converge in the fewest steps - and only proposes a fresh open for whatever remains after every usable opposing ticket has been spent. Net exposure is something you have to compute, not something every platform hands you directly. GetActualNet() does that computation by scanning open tickets, and Reconcile() only acts on symbols explicitly registered with SetDesired - a manual trade on a symbol never handed to the reconciler is left alone rather than closed as a surprise side effect. This library never sends, closes or modifies a trade itself. It hands back a plan; the caller decides how and whether to execute it, keeping every trading side effect visible in code the caller owns. Notes and limitations - Reconcile() only plans; it is safe to call every tick. Nothing on the account changes until the caller executes the returned actions. - Because the header never executes anything, it cannot know whether an action it proposed actually succeeded. Call Reconcile() again after executing a plan to confirm convergence rather than assuming the plan completed. - Partial closes are floored to the broker's lot step, never rounded up, so a plan never proposes closing more than the gap requires. A small leftover fragment below one lot step is left in place and will show up again on the next Reconcile() call once enough gap accumulates to cross a full step. - Opening a fresh position when the remaining gap is below the broker's minimum lot is reported as a zero-volume action with an explanatory note rather than silently rounded up into a larger trade than requested. - A symbol never registered with SetDesired is completely invisible to this library - it will never propose closing positions on it. - On MQL5, a close action's note says whether the caller must use a full PositionClose() instead of a partial close - read it before executing the plan rather than always calling PositionClosePartial().
Script · MT5 · v1.0 · Free
About Minimal-Action Position Reconciler
Minimal-Action Position Reconciler is an expert advisor (trading robot) for MetaTrader 5. It has 2 settings, such as Desired net chart symbol and Magic filter. MQL source code included.
Files in the download: PositionReconciler.mq5.mqh, PositionReconciler.txt, PositionReconcilerDemo.ex5, PositionReconcilerDemo.mq5.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Desired net chart symbol | 0.30 | Desired net volume (+long/-short) on the chart symbol |
| Magic filter | -1 | Magic number to match, -1 = match every ticket |
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: Buy/Sell Arrows · MT5 Script · MQL5 Source Code