Loading…

Magic Number Manager

A magic number chosen by hand works until it doesn't. Two EAs on the same account both defaulting to magic 12345, or the same EA run twice - once live, once as a forward test - with nobody changing the input, and now both instances are managing each other's trades. The usual fix is "remember to change the magic", which is a process fix for a problem that has a structural one. This header treats a magic number as three fields packed into one integer instead of one opaque number chosen by hand: a strategy id, a symbol code, and a variant (instance index). The layout is fixed and documented in the header comment - 15 bits for strategy, 10 for symbol, 6 for variant, 31 bits total, so the result is always a positive value under 2^31 on every broker: magic = (strategyId << 16) | (symbolCode << 6) | variant EncodeMagicNumber refuses instead of masking when a field is out of range, so a caller that passes a bad value gets an obviously-invalid -1 magic rather than a plausible-looking wrong one caused by silent bit overlap. The symbol field can come from an explicit list you maintain (guaranteed unique) or from MagicSymbolHash, a djb2-style hash of the symbol name (convenient, but two different names can land on the same code - the header is honest about that trade-off rather than hiding it). The practical collision guard is FindFreeVariant. Instead of hardcoding a variant number per chart and hoping nobody duplicates it, an EA calls FindFreeVariant(strategyId, symbolCode) once at OnInit, which scans the account's open trades and returns the first variant slot with nothing open under it. Running the same EA twice on the same symbol then produces two different magics automatically, with no operator step to forget. GetDistinctOpenMagics and MagicToString round out the toolkit for a startup or on-demand collision report: list every magic currently open on the account and decode each one back to its three fields for the journal. Notes and limitations - 1023 symbol codes and 32767 strategy ids are generous but not unlimited. A portfolio running more than 1024 distinct symbols, or wanting more precision than 64 concurrent variants per strategy/symbol pair, needs a wider layout - change the *_BITS constants at the top of the header and everything else follows from them. - MagicSymbolHash can collide between different symbol names (for example a broker's "EURUSD" and "EURUSDpro" suffix variants are different strings that may hash to the same code). When that risk is unacceptable, assign symbol codes explicitly from a fixed list you control instead of hashing. - IsMagicInUse and FindFreeVariant only see currently open trades - they do not check trade history, so a magic used by a fully closed strategy that is not currently trading will read as free, which is the correct and intended behaviour on both platforms. - The scheme assumes every EA on the account uses it. A hand-picked magic from an EA that does not use this header can still collide by coincidence; GetDistinctOpenMagics lets you audit for that after the fact.

Script · MT5 · v1.0 · Free

About Magic Number Manager

Magic Number Manager is a script for MetaTrader 5. It shows several symbols in one panel and is set up for gold (XAUUSD). It has 1 setting, such as Strategy id. MQL source code included.

Files in the download: MagicManager.mq5.mqh, MagicManager.txt, MagicManagerDemo.ex5, MagicManagerDemo.mq5.

Settings

SettingDefaultWhat it does
Strategy id7Strategy id for the free-variant worked example

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Pending Orders · Dashboards & Scanners · Gold (XAUUSD) · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria