Adopt Orphan Positions Under A Magic
MQL4 has no function that changes a ticket's magic number after it has been opened. OrderMagicNumber() is fixed the instant OrderSend() runs and stays that way for the life of the ticket. This matters whenever a manual trade, or a position left behind by an EA that was removed from the chart, needs to come under the management of a running EA that filters strictly by magic number - there is no in-place fix, only close and reopen. This script does exactly that, and only that: for every ticket matching the source filter, it closes the position at market and immediately opens a new one on the same symbol, same direction, same lot size, under the target magic. Stop loss and take profit prices are carried across by default. The cost is real - one spread crossing per ticket, plus the symbol is briefly flat between the close and the reopen - and this script does not pretend otherwise. It is the honest price of the only mechanism MQL4 actually provides; a submission that implied this was free would be misleading. The riskiest failure mode is a close that succeeds followed by a reopen that fails, since the original exposure is now genuinely gone and needs a human decision, not a silent retry loop that might repeat the mistake under different conditions. That case raises an Alert() as well as a Print(), specifically so it is not missed in a busy Experts tab. By default the source filter targets magic 0, the value most brokers' manual trades and many un-magic'd hobby EAs carry, but any specific magic can be targeted instead, or the filter can be widened to adopt everything except the target magic itself. Notes and limitations - Every adopted ticket pays one round of spread. This is not optional and not hidden - the confirmation prompt states it directly. - The symbol is briefly flat between the close and the reopen. On a fast market this window, though short, is real market exposure risk that a true in-place re-tag (impossible in MQL4) would not have. - If the close succeeds but the reopen fails, the position is gone and not replaced. The script raises an Alert() for this specific case because it is the one outcome that needs a human decision immediately, not a queued retry. - Stop loss and take profit are carried across as absolute prices, not as distances. If the market has moved significantly since the original stop was set, the reopened ticket's stop is still at that same price, which may now be closer to or further from the new entry than it was from the old one. - Comment on the reopened ticket records the original ticket number for an audit trail, but MetaTrader gives no other link between the closed original and its replacement. - The script runs once per invocation. It does not watch for new orphan tickets appearing after it finishes.
Script · MT4 · v1.0 · Free
About Adopt Orphan Positions Under A Magic
Adopt Orphan Positions Under A Magic is an expert advisor (trading robot) for MetaTrader 4. It has 9 settings, such as Source magic filter, Source magic any and Target magic. MQL source code included.
Files in the download: AdoptOrphansToMagic.mq4, AdoptOrphansToMagic.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Source magic filter | 0 | Adopt tickets carrying this magic (0 = typical manual-trade magic) |
| Source magic any | false | Ignore the filter above - adopt every magic except the target |
| Target magic | 100001 | Magic number to assign to the reopened positions |
| Current symbol only | false | Restrict to this chart's symbol |
| Preserve stops | true | Reopen with the same stop loss / take profit prices |
| Slippage pips | 3 | Slippage tolerance on both legs, in pips |
| Max retries | 5 | Attempts per leg before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before adopting |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 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: MT4 Script · MQL4 Source Code