Loading…

Basket Unwind Sequencer

Closing a whole basket by looping blindly over open tickets spends the calmest, cheapest exits on whatever ticket happened to come first, while the position that is actually threatening the account waits its turn. If something goes wrong partway through - a rejection, a disconnect - the positions still open afterward are whichever ones the loop had not reached yet, not the ones it would have been safest to leave for last. This script computes the full closing order up front: every open position is scored by its stop-implied risk in account currency, using the broker's tick value and tick size so nothing depends on a hardcoded contract size, and sorted largest first. Positions without a usable stop fall back to a margin-based size proxy so they are still ranked rather than silently skipped. Ties are broken by spread - the tighter-spread position goes first, since it is the cheaper one to exit and there is no reason to leave an easy close for later. The full plan is always printed before anything happens. Execution only starts when InpConfirmExecute is true AND InpConfirmPhrase is typed exactly as UNWIND - the same double-gate pattern used across this catalogue's other real-order scripts, so the safety behaviour is consistent wherever a confirmation phrase is asked for. Once running, closes are strictly sequential: each ticket is confirmed closed, or given up on after its retry budget, before the next one is attempted. A mid-run failure leaves a known, fully printed state - which positions closed, which did not and why - rather than an ambiguous partial batch. Notes and limitations - Closes the whole matched basket, in full, one ticket at a time - it is not a partial or gradual unwind tool. - Ranking is a snapshot taken once at the start of the run. Prices and spreads move while the sequence executes; a position ranked second may by the time it is reached have a different real-world risk than when it was scored. - A position with no stop loss is ranked by margin used, not by a true risk figure - it is included so it is never silently ignored, but the ranking for it is a size proxy, not a measured risk. - If a close in the middle of the sequence fails after exhausting its retries, the script logs it and continues with the rest of the plan rather than stopping - by design, so one stuck ticket does not leave the whole basket open. - Retries only cover the broker's ordinary temporary refusals. A hard rejection is reported per ticket, not retried indefinitely.

Script · MT5 · v1.0 · Free

About Basket Unwind Sequencer

Basket Unwind Sequencer is an expert advisor (trading robot) for MetaTrader 5. It has 6 settings, such as Current symbol only, Magic filter and Confirm execute. MQL source code included.

Files in the download: BasketUnwindSequencer.ex5, BasketUnwindSequencer.mq5, BasketUnwindSequencer.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 to this chart's symbol
Magic filter-1Only this magic (-1 means every magic)
Confirm executefalseMust be true to actually close positions
Confirm phraseMust equal UNWIND exactly to execute
Max retries5Attempts per position before giving up and moving on
Retry wait ms500Pause between attempts, milliseconds

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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria