Loading…

CloseBy Pair Optimiser

OrderCloseBy() is spread-free: it nets two opposite tickets against each other at zero cost, closing the smaller ticket's whole volume and the same volume off the larger one, leaving the larger ticket open with the difference. That is genuinely free money compared with closing everything at market and paying the spread twice. The naive way to use it - loop through the order pool and pair whatever buy and sell you find next - still reaches zero net exposure eventually, but it tends to leave one or more small, ragged leftover tickets rather than a single clean one. Every extra leftover ticket that cannot be paired further has to be closed at market, paying exactly the spread cost the whole exercise was meant to avoid. This header plans the pairing sequence before touching a single ticket. BuildCloseByPlan() runs a greedy nearest-volume match: at each step it pairs the still-open buy and sell whose remaining volumes are closest to each other. Pairing near-equal volumes first maximises how many tickets are fully closed at each step and pushes any unavoidable leftover - which only exists if the buy and sell totals are not exactly equal - into a single final ticket instead of scattering a fragment onto every survivor along the way. The matching algorithm (BuildCloseByPlan) is a pure function over a caller-supplied ticket/lots/direction array, deliberately kept separate from order-pool access. CollectSymbolLegs() is the thin wrapper that reads the real MT4 order pool for one symbol (optionally filtered by magic number) and hands it to the planner. ExecuteCloseByPlan() then runs the resulting plan for real, re-checking each ticket is still open immediately before acting, since an earlier step in the same plan (or something outside it entirely - a manual close, an SL/TP hit) can remove a ticket the plan still refers to. Notes and limitations - The planner only ever pairs tickets on the SAME symbol - OrderCloseBy() itself requires this, it is not a library-imposed limitation. - Pending orders are excluded from CollectSymbolLegs() - only OP_BUY and OP_SELL market tickets can be CloseBy'd against each other. - The plan is a recommendation computed at one point in time. If the book changes between BuildCloseByPlan() and ExecuteCloseByPlan() (a new trade opens, one closes on its own SL/TP), the plan can reference a ticket that no longer exists in that state - ExecuteCloseByPlan() detects this per step and skips it rather than aborting the whole batch, but it does not re-plan automatically. Re-run BuildCloseByPlan() against a fresh scan if the book may have moved. - This is a planning and execution tool for an ALREADY hedged book. It does not decide whether hedging is a good idea, and it never opens new tickets itself - only closes existing ones against each other. - The demo does not execute ExecuteCloseByPlan() against real tickets by default; it only prints what a live plan would do, to avoid touching positions unattended.

Script · MT4 · v1.0 · Free

About CloseBy Pair Optimiser

CloseBy Pair Optimiser is a script for MetaTrader 4. It has 1 setting, such as Magic filter. MQL source code included.

Files in the download: CloseByPairOptimiser.mqh, CloseByPairOptimiser.txt, CloseByPairOptimiserDemo.mq4.

Settings

SettingDefaultWhat it does
Magic filter-1Magic number to restrict the live scan to, -1 = any

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: Hedging · Volume · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria