Loading…

Pairwise Close By Opposite Positions

A hedged book often ends up with a buy and a sell open on the same symbol at the same time - a manual hedge, a grid that filled both ways, or two EAs working the same pair. Closing both legs independently at market pays the spread twice, once on each ticket, for a position pair whose net exposure was already flat. OrderCloseBy settles two opposite tickets against each other in a single broker-side operation. The matched volume - the smaller of the two lot sizes - cancels out at the two orders' own open prices rather than at the current market price, so only the unmatched remainder, if any, ever crosses the spread. The saving is real money on a book that hedges often, and it is entirely broker-dependent: not every broker supports close-by, and some disable it per account type. This script checks by attempting it and reports the failure plainly rather than pretending every broker behaves the same way. Pairing is done by symbol and direction: any two open tickets on the same symbol with opposite market direction are eligible. By default both tickets must also share the same magic number, since pairing an EA's hedge against an unrelated manual trade can produce a result the EA does not expect - this can be relaxed with an input for a book that is deliberately managed as one unit regardless of who opened each leg. Every proposed pair is listed in the journal with both ticket numbers, symbol, direction and lot size before anything is touched, and the script asks for confirmation by default. Notes and limitations - Hedging accounts only. Close-by requires two independent opposite tickets on the same symbol, which cannot exist on a netting account - a netting account merges opposing fills into one position before this script ever sees them, so there is nothing for it to pair. - Not every broker supports OrderCloseBy. If the broker refuses it, every attempt fails with the same error code and the script reports it rather than falling back to a plain close silently. - Pairing is greedy: the first eligible opposite ticket found for a given order is the one it is paired with. On a book with several tickets per side and per symbol this may not choose the pairing that minimises the unmatched remainder - it chooses a valid one, not necessarily the optimal one. - Unequal lot sizes leave a remainder on the larger ticket after close-by. That remainder is not automatically closed separately; it stays open under its original ticket for a later run or manual action. - The script runs once. It is not a background monitor that pairs new opposite tickets as they appear.

Script · MT5 · v1.0 · Free

About Pairwise Close By Opposite Positions

Pairwise Close By Opposite Positions is an expert advisor (trading robot) for MetaTrader 5. It has 6 settings, such as Current symbol only, Magic filter and Require same magic. MQL source code included.

Files in the download: PairwiseCloseByOpposite.ex5, PairwiseCloseByOpposite.mq5, PairwiseCloseByOpposite.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict pairing to this chart's symbol
Magic filter-1Magic number (-1 pairs every magic)
Require same magictrueOnly pair tickets sharing the same magic number
Max retries5Attempts per pair before giving up
Retry wait ms500Pause between attempts, milliseconds
Confirm firsttrueAsk for confirmation before closing

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Grid · Hedging · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria