Magic Number (EA Identifier)
Definition
A magic number is an integer identifier attached to every order placed by an EA, allowing the EA to distinguish its own positions from other trades (other EAs, manual trades). Each EA should use a unique magic number; running multiple EAs on the same account requires careful magic-number assignment to prevent conflicts. Standard MQL5 best practice.
In-depth: Magic Number (EA Identifier)
The magic number is one of the most important configuration concepts in MQL5 trading. Every trade request in MQL5 (`MqlTradeRequest.magic` field) includes an integer identifier that becomes part of the position's metadata. The EA later filters positions by this identifier to distinguish its own trades from others.
Why magic numbers matter:
1. Multi-EA coexistence: traders often run multiple EAs on the same account — e.g. Scalperology on EURUSD M5, Trendopedia on GBPUSD H1, manual swing trades. Each EA must manage only its own positions; without magic numbers, all EAs would see all positions and could conflict.
2. Manual trade isolation: when a trader places manual trades on the same account as an EA, the EA should not modify or close manual trades. Filtering by magic number (where manual trades have magic=0 by default) achieves this isolation.
3. Multi-pair single-EA: even one EA running on multiple charts (one per pair) typically uses different magic numbers per chart instance, allowing each chart's instance to manage its own pair's positions independently.
Implementation pattern: ```mql5 // EA input parameter input int MagicNumber = 12345;
// When placing a trade MqlTradeRequest req = {}; req.magic = MagicNumber; // ... other request fields OrderSend(req, result);
// When iterating positions to manage int total = PositionsTotal(); for(int i = 0; i < total; i++) { ulong ticket = PositionGetTicket(i); if(PositionGetInteger(POSITION_MAGIC) == MagicNumber) { // This is my position — manage it } } ```
Magic number assignment best practices:
1. Unique per EA instance: a 4-8 digit integer unique across all EAs on the account. Common pattern: combine an EA family ID with a chart-instance ID, e.g. 10001, 10002, 10003 for three instances of EA family 1; 20001, 20002 for two instances of EA family 2.
2. Documented: maintain a spreadsheet or text file of magic numbers in use, especially for accounts running many EAs. Conflicts produce subtle bugs that are hard to diagnose.
3. EA-input parameter: expose magic number as an EA input so users can change it without recompiling. Avoid hardcoding.
4. Avoid zero: magic=0 is the default for manual trades. Using 0 in an EA mixes EA trades with manual trades and breaks isolation.
5. Consistent across instances: if running the same EA on multiple pairs, use distinct magic numbers per pair to prevent the EA from modifying positions on the wrong pair.
Debugging magic-number issues: - Symptoms: EA closing positions it didn't open; EA failing to manage positions it should; positions appearing to be 'orphaned' (not managed by any active EA) - Diagnosis: log the magic number of every position the EA touches; compare against EA's configured magic number - Common errors: typo in magic number assignment (12345 vs 21345); multiple EAs accidentally configured with the same magic number; magic number changed after positions were opened (the EA no longer recognises its own positions)
For vendors distributing commercial EAs: include the magic number as a documented EA input parameter with a default value chosen to avoid conflicts with common other EAs (e.g. 8-digit numbers reduce collision probability with simpler default magic numbers).