Written for MT4 and MT5 (MQL4/MQL5). Verify specific field names and behavior against current MetaQuotes documentation, since implementation details can shift between platform builds.
A magic number in forex is a numeric identifier that an Expert Advisor (EA) assigns to the orders or trades it opens. The EA can later search for that number to tell its own trades apart from manual orders or trades placed by other EAs on the same account. A magic number is separate from the order ticket and separate from the order comment, and it only works correctly when the EA is actually coded to assign and check it consistently. It isn’t automatic just because an EA exists.
That last part matters more than most guides let on, so let’s get into it properly.
Magic Number vs. Ticket vs. Comment
These three get mixed up constantly, and honestly, I don’t think that’s surprising. They’re all “numbers or text attached to a trade,” so on the surface they feel interchangeable. They’re not.
| Identifier | Assigned by | Main purpose |
| Magic number | EA or trading code | Identify which strategy opened a trade |
| Ticket number | The trading platform | Uniquely identify a specific order, deal, or position record |
| Order comment | EA, script, or trader | Attach optional readable text |
| Symbol | Market or broker | Identify which instrument is being traded |
| Account number | Broker | Identify the trading account itself |
A magic number is not the same thing as a comment, even though earlier versions of guides on this topic have said exactly that. A comment is free text, it can be edited, truncated by the platform, or left blank entirely. A magic number is a defined numeric field the EA sets programmatically. They serve different purposes, and treating a comment as a reliable identifier is asking for trouble, since comments aren’t guaranteed to be unique or even present.

How an EA Assigns a Magic Number
When an EA opens a trade, its code can attach a specific magic number value to that order as part of the trade request. That value then travels with the order, and later, the EA (or a script, or an external reporting tool) can filter trades by checking which magic number each one carries.
Here’s the part that’s easy to assume incorrectly: not every EA automatically has one, and not every EA uses it well. For a magic number to actually do its job, the code needs to:
- Accept or define a magic-number value somewhere in its settings
- Assign that value every time it submits an order
- Filter existing orders or positions using it
- Avoid interfering with trades that carry a different number
Some EAs skip this logic entirely, or fall back on a default value that isn’t unique. In that case, the magic number exists as a setting, but it isn’t really protecting anything.
What a Magic Number Does (and Doesn’t) Do
I want to be direct about this, because it’s the single biggest misconception floating around this topic: a magic number does not calculate performance. It doesn’t track profit, loss, drawdown, win rate, or risk-adjusted return on its own. All it does is tag a trade so that compatible tools, the EA itself, a script, or an external analytics platform, can group trades belonging to the same strategy.
If you’re running three EAs on one account and want to see how each one performed individually, the magic number is what lets a reporting tool separate that data. But the actual performance math happens elsewhere. The number itself is just a label.
How an EA Finds Its Own Trades
Say EA A trades EUR/USD using magic number 1001, and EA B trades the same account using magic number 2001. When EA A scans its open trades, it filters specifically for EUR/USD positions tagged 1001, and it ignores anything tagged 2001.
That sounds clean in theory. In practice, filtering by magic number alone can be risky. A more robust approach usually checks several fields together:
- Account
- Symbol
- Magic number
- Order or position type
- Strategy version
- Direction
- Ticket, where relevant
- Comment, as a secondary check where useful
Why bother with all that? Because two EAs can accidentally end up using the same magic number, especially if they’re built from a similar template or copied from an online example without changing the default. If EA A and EA B both use 1001, they may start reading, closing, or modifying each other’s trades without either one “knowing” it’s happening.
Why Every Strategy Needs a Unique Identifier
This is worth its own section, because the consequences of getting it wrong aren’t small. If two independently managed EAs share a magic number:
- One EA may close the other’s trades, thinking they’re its own
- It may modify stop-loss or take-profit levels that belong to a different strategy
- It may count the wrong exposure when calculating position size
- It may block a valid entry because it thinks a position is already open
- Reporting and statistics for both strategies can become misleading
None of that is hypothetical. It’s a fairly common mistake, particularly when traders copy an EA template and forget to change the magic-number setting before running a second strategy alongside it.
A reasonable, repeatable numbering convention helps avoid this. Here’s one example structure, though the exact format is entirely a personal or team convention, not something MetaTrader requires:
| Digits | Meaning |
| First 2 | Strategy family |
| Next 2 | Strategy version |
| Next 2 | Symbol group |
| Last 2 | Timeframe or variant |
Under that scheme, a magic number like 10030702 might mean strategy family 10, version 03, symbol group 07, timeframe or variant 02. It’s worth keeping a simple registry alongside your EAs so you (or anyone else managing the account) can see what’s running at a glance:
| Magic number | EA | Symbol | Timeframe | Version |
| 10030702 | Trend EA | EUR/USD | H1 | 2 |
| 10040701 | Breakout EA | GBP/USD | M15 | 1 |
Magic Numbers in MT4 vs. MT5
This is a distinction I don’t see covered often enough, and it genuinely changes how magic numbers behave in practice.
MT4 commonly manages trades as tickets, with each order carrying its own identity. MT5 works differently: it distinguishes between orders, deals, and positions as separate concepts, and accounts can run in either a hedging or a netting model.
In a netting account specifically, multiple strategies trading the same symbol may end up contributing to a single net position rather than staying as separate, individually tracked trades. That means position-level attribution can get more complicated than just assigning different magic numbers and calling it solved. Magic-number behavior genuinely depends on the platform and account model you’re running, so it’s worth confirming exactly how your account handles this, hedging or netting, before assuming your strategies are cleanly separated.
What Actually Happens After a Restart
Here’s a claim that shows up in a lot of beginner content, and it needs correcting: a magic number alone does not guarantee that an EA will recognize its open trade after a restart and avoid duplicating it. That recovery behavior depends entirely on how the EA is coded.
After a disconnection, computer restart, or platform relaunch, a well-built EA can search for matching trades by checking magic number, symbol, and other relevant fields. But “can” is doing real work in that sentence. To recover safely, the EA generally needs to:
- Scan currently open orders or positions
- Filter them correctly using its own identifiers
- Reconstruct whatever internal state it needs (like entry price or trailing-stop progress)
- Check that it’s looking at the right symbol and account
- Handle partial closes correctly
- Account for pending orders, not just open positions
- Avoid re-entering under conditions that would create a duplicate trade
If any of that logic is missing, a restart can absolutely result in duplicate orders, or an EA that loses track of a position it should still be managing. The magic number is a piece of the puzzle here, not the whole solution.
Similarly, changing an EA’s magic number while it still has open trades is a real risk. If an EA opens a position using one number and gets restarted with a different one, it may simply stop recognizing that existing position altogether. Don’t change a live EA’s magic number setting unless the code includes a specific, tested migration process for handling it.
Manual Trades and Magic Numbers
Manual trades typically don’t carry a magic number in the same deliberate way an EA-assigned one does, though the exact convention can vary by platform, script, or copy-trading tool. An EA has to explicitly decide how it treats these:
- Ignore manual trades entirely and only manage its own
- Include manual trades in some way, if the logic calls for it
- Manage strictly its own tagged trades and leave everything else alone
- Respect existing exposure created by other systems on the same account
Assuming an EA will automatically “know” which trades are manual and which aren’t is a mistake. That behavior has to be built in.
Common Mistakes Worth Avoiding
- Reusing the same magic number across unrelated EAs: This is probably the most common issue, and it can cause two strategies to interfere with each other’s trades without either one flagging an error.
- Changing the magic number while trades are open: The EA may simply stop recognizing its own existing positions.
- Filtering by magic number but not symbol: An EA can end up modifying or counting trades on the wrong instrument if it isn’t checking both.
- Assuming the magic number prevents duplicate entries: It doesn’t, on its own. Duplicate protection needs separate entry-state logic in the code.
- Assuming every reporting tool imports magic-number data automatically: Some analytics or reporting tools may not expose or preserve it, so check before relying on it.
- Treating order comments as reliable identifiers: Comments can be truncated, edited, or left blank, none of which applies to a properly assigned magic number.
EA Logic Checklist Before Deployment
Worth running through this before putting any EA on a live account:
- Does it assign a deliberate, non-default magic number?
- Does it filter by both magic number and symbol, not just one?
- Does it distinguish between market orders and pending orders?
- Does it handle partial closes correctly?
- Can it reconstruct its state properly after a restart?
- Does it avoid duplicate entries under the same conditions?
- Does it intentionally ignore or include manual trades, rather than behaving unpredictably around them?
- Does it handle changed input settings safely, without breaking existing logic?
- Does it log errors and trade actions somewhere you can actually review?
- Does it work correctly with your account’s hedging or netting model?
A Note on Building EAs Without Coding Experience
Traditionally, turning a manual strategy into an EA meant hiring a developer, which tends to be slow and expensive, and often involves several rounds of back-and-forth fixing bugs before the code actually matches what you intended. Strategy builder software has made this considerably more accessible for traders without a coding background, letting you generate an EA’s logic without writing MQL yourself.
That said, automation doesn’t automatically mean secure or error-free. Even a generated EA still carries real operational risk:
- Coding errors, whether human-written or generated
- Broker disconnections
- VPS or hosting outages
- Duplicate orders from incomplete recovery logic
- Incorrect lot sizing
- Platform updates changing expected behavior
- Corrupted internal state after a crash
- Strategy failure in changing market conditions
- Credential theft, if account access isn’t properly secured
- Malicious or poorly vetted third-party EAs
A strategy builder can meaningfully cut down the coding effort involved, but the EA it produces still needs code review, real testing, risk controls, and someone actually keeping an eye on it. It also needs the platform running, algorithmic trading enabled, an active broker connection, and a functioning computer or VPS to actually monitor the market. It doesn’t trade “all the time” on its own; it trades only while all of that infrastructure stays up.
One more thing worth correcting here: there’s no fixed limit like “up to 99 EAs” on one account. Practical capacity depends on your CPU, available memory, platform and broker limits, how many charts you’re running, tick frequency, how efficient the code is, network stability, and how much exposure you’re comfortable carrying at once. Anyone quoting a specific number without tying it to a documented broker or platform limit is likely guessing.
Frequently Asked Questions
Is a magic number required for every Expert Advisor?
No. A magic number is a setting the EA’s code has to define and use deliberately; it isn’t a mandatory platform feature that exists automatically on every EA. Some simple or single-strategy setups might not bother with one at all. It becomes genuinely important once you’re running more than one EA on the same account, since without a consistent identifier, separating each strategy’s trades gets difficult fast.
Can I set my own magic number, or does the EA choose it automatically?
In most EAs, the magic number is an adjustable input you can set yourself, usually visible in the EA’s properties or input settings panel. Some EAs ship with a default value that traders never bother changing, which is exactly how magic-number collisions happen between unrelated strategies. It’s worth deliberately choosing and recording your own value for each EA rather than accepting whatever default the code shipped with.
What happens if two EAs on the same account use the same magic number by accident?
They can start interfering with each other’s trades. One EA may close, modify, or count the other’s positions as if they belonged to its own strategy, since both are technically tagged the same way. This can produce incorrect exposure calculations, blocked entries, or misleading performance statistics for both strategies. Assigning each EA a genuinely unique magic number, and checking it, prevents this specific problem.
Does a magic number work the same way in MT5 as it does in MT4?
Not entirely. MT4 generally organizes trades around individual tickets. MT5 separates orders, deals, and positions, and accounts can run in either a hedging or netting model. In a netting account, several strategies trading the same symbol may end up contributing to one combined net position rather than staying fully separate, so magic-number-based attribution alone may not tell the full story on MT5 the way it more directly does on MT4.
Can I use an order’s magic number to recover an EA’s state after a power outage or disconnection?
Partially, and only if the EA is coded for it. A well-built EA can scan open orders and positions after reconnecting, filter by magic number and symbol, and rebuild whatever internal state it needs to keep managing the trade correctly. But this isn’t automatic just because a magic number exists. If that recovery logic wasn’t written into the EA, a restart can still result in a duplicate order or a position the EA no longer tracks properly.
Final Summary
A magic number is a tag, not a guarantee. It’s genuinely useful, arguably essential once more than one automated strategy shares an account, but it only does what the surrounding code tells it to do. It doesn’t calculate performance by itself, doesn’t automatically restore an EA’s state after a restart, and doesn’t prevent duplicate trades unless separate logic handles that. Understanding where the magic number’s job actually ends, and where the EA’s broader code needs to pick up the slack, is really the whole point of this topic.
This article is for educational purposes and does not constitute financial or trading advice. Automated trading carries operational and financial risk, including the possibility of coding errors, connectivity issues, and unexpected losses.
Further Reading
- How To Trade Forex From Home
- How To Trade Forex For Beginners
- Best Forex Strategies
- Forex Indicators
- Best Currency Pairs To Trade
- How To Create A Forex Trading Without Coding
- Best Forex Brokers
- Best Forex Robots For Trading
- What Does FX Stand For
- Best Forex VPS For MetaTrader
- What Is Forex Algorithmic Trading
- How To Become A Successful Forex Trader

Petko Aleksandrov



