
A grid or martingale basket on Gold (XAUUSD), forex or indices usually carries one position deep in profit and one deep in loss at the same time. This EA pairs them: it finds the best winner and the worst loser, adds profit, swap and commission, and when the combined result reaches your minimum it closes both, winner first to lock the gain. It never opens a trade.
The filter decides what it manages: the chart symbol or all symbols, one magic number or every position, manual trades included, so it runs next to any grid EA or cleans a basket you built by hand. Two optional basket rules close everything at once: a take profit and a stop loss in account currency. Every close is checked by return code and retried after a pause.
The panel is plain on purpose: positions, floating result, best and worst tickets, the pair sum against your minimum and a strip that turns green when a pair is ready, with buttons to close the pair, close all or pause. It trims exposure trade by trade. It does not turn a losing basket into a winning one by itself.
Attach it to one chart of the symbol whose basket you want to manage. Any timeframe works, the EA runs on every tick and on a one second timer, so the chart period does not matter. Use one instance per symbol, or one instance with the chart symbol filter off to cover the whole account. The single symbol pair logic needs a hedging account, because it closes a buy and a sell, or two trades of the same side, that live together on one symbol. On a netting account use the all symbols mode.
Before running it on a live account test it in the strategy tester in visual mode: open three or four positions by hand, watch the strip turn green when the pair sum passes your minimum and check the log, every close is printed with the ticket numbers and the combined result.
The pair logic is isolated in two functions, XP_FindPair and XP_ClosePair, so you can lift it straight into your own grid or martingale EA. Natural extensions are one winner against two losers, pairing by lot size instead of money, a trailing basket take profit, a session filter or an equity based pause. It works next to XANDER Grid XAUUSD (magic 2001), XANDER Gold Recovery, any martingale EA or your manual scalping on gold, EURUSD, GBPUSD, US30 or NAS100. Keep your own entries, Smart Money Concepts, support and resistance or whatever you trade, and let this one handle the exits of the basket.
Related: XANDER Grid XAUUSD, XANDER Gold Recovery, XPro Trade Panel.
The overlap idea comes from Grid EA Pro by Nguyen Nghiem Duy (MQL5 Code Base, 2020). This code is rewritten from scratch for MT5 as a standalone position manager.
v1.0 (2026-10-25): initial release.
Cointegration: ADF, Engle-Granger and Johansen tests with a pair scanner
Unit root and cointegration tests (ADF, Engle-Granger, Johansen) with MacKinnon p-values, hedge ratio, half-life and z-score of the spread, and a pair scanner.
Kalman: exact diffuse Kalman filter, local level and time-varying hedge ratio
Kalman filter and smoother with an exact diffuse start, missing values and maximum likelihood: local level, local linear trend and a regression whose hedge ratio drifts.
DataSnooping: SPA test, Reality Check, StepM and model confidence set
Is the best of many strategies really better than the benchmark? Hansen's SPA test, White's Reality Check, Romano-Wolf StepM and the model confidence set.
Swap Markup Scanner
Shows the broker's hidden financing markup and the carry for every symbol, as annual % of notional.