Loading…

Magic Number Survival Tester

An EA that tracks its own trades by magic number usually assumes that number is stable across the life of a position. Whether that holds through a partial close is broker-specific and undocumented anywhere except in the behaviour itself, and getting it wrong quietly breaks position tracking on exactly the account where it matters most. The trap most people miss first: a partial close in MQL4 does NOT leave the remainder under the original ticket. The broker closes the requested slice against the original ticket (which then shows up in trade history) and opens a BRAND NEW ticket for whatever volume is left open. Code that assumes its own ticket number survives a partial close has already lost track of the position before the magic number question is even reached. This script does not assume anything about the new ticket's number - it searches the open order pool for it by symbol and magic, then reads the magic number directly off whatever it finds. The test sequence is deliberately small and fully cleaned up: open the smallest tradeable size that still allows a legal partial close (two lot steps), confirm the magic number is correct immediately after open, partially close half of it, locate the new remainder ticket, read its magic number, then close whatever remains regardless of pass or fail. No order is left open in the book at the end of a run, successful or not. Testing whether a magic number survives a genuine stop-out (as opposed to a scripted partial close) is not something a single script run can do deterministically - a stop-out only happens when price actually trades through a level, which a script cannot force or wait for indefinitely within one OnStart() call. The partial-close path tested here uses the same underlying order-closing mechanism the server applies for any close, including a stop-out, so a PASS here is a reasonable proxy for stop-out behaviour too, but it is not identical and the .txt says so plainly rather than overselling it. Notes and limitations - DEMO ACCOUNTS ONLY, and this is not an optional setting. The script checks ACCOUNT_TRADE_MODE unconditionally in OnStart() and there is deliberately no input to override that check - a magic-number survival test has no legitimate reason to run on a funded account, and an override input would only invite firing it on the wrong account by habit. - Every order this script opens is closed again before it returns, whether the test passes, fails, or a step errors out partway through - nothing is left open in the book. - If your broker's minimum lot step does not allow a legal two-step partial close (very rare, but possible on some CFDs with coarse minimums), the script refuses to run on that symbol rather than attempting an order the broker would reject. - This tests the market-buy / partial-close path specifically. It does not test pending order fills, hedged multi-position accounts, or a genuine stop-loss trigger - see the full description above for why the last one cannot be tested deterministically in a single script run. - Uses OP_BUY only. The result should not differ for OP_SELL on the same broker, since both close through the identical server-side mechanism, but this was not independently verified in this test sequence.

Script · MT5 · v1.0 · Free

About Magic Number Survival Tester

Magic Number Survival Tester is an expert advisor (trading robot) for MetaTrader 5. It has 3 settings, such as Magic number, Slippage points and Confirm first. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Magic number990299Magic number stamped on the test order
Slippage points30Slippage tolerance passed to every trade request
Confirm firsttrueAsk for confirmation before sending the real test order

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