Magic number
Also called Magic, MagicNumber, Expert ID, EA magic number
A magic number is an ID number that an Expert Advisor stamps on every order it opens. The EA uses it to find, manage and close its own trades and to leave every other trade on the account alone.
The magic number matters most when more than one strategy shares one account. If you set it wrong, one robot can count, move or close trades that belong to another robot, or to you, and that is real money.
How an EA uses its magic number to pick out its own trades
Each time an EA opens a trade, it writes its number into the order. Later the EA checks the open positions to trail a stop, add a grid level or close a basket. It skips every trade whose number does not match its own.
Trades you place by hand usually carry a magic number of 0. To the EA, they are someone else's trades. The picture below shows one account with four open trades and which EA owns each one.
What a shared magic number does to your money
Here is an illustrative example. Your account runs two EAs on EURUSD. EA A is a basket EA: it closes all its trades once their combined profit reaches +$50. Right now its three 0.10-lot buys show +$80 together. EA B holds one 1.00-lot buy that is 30 pips down. On EURUSD, 1.00 lot is worth $10 per pip.
If both EAs use the same magic number, EA A counts EA B's trade as part of its own basket:
EA A should have closed already, but it sees a loss, so it holds. A grid EA might even keep adding trades. When the combined figure finally reaches +$50, EA A closes everything, and that includes EA B's trade, before EA B's own stop or target is hit. Neither strategy now trades the way you tested it.
Where you set and check the magic number
You set it in the EA's input settings, which you see when you attach the EA to a chart. The field is usually called Magic, MagicNumber or Expert ID. The term comes from MetaTrader code. In MQL4 the magic number is a parameter of the OrderSend() function. In MQL5 it is the magic field of the trade request structure (MqlTradeRequest). Use a whole number from 1 up to 2,147,483,647, with no decimals; it works in both MT4 and MT5. If the field is there, choose the number yourself instead of keeping whatever value it came with.
Many platforms hide the number in the main trade list. To check it, look at the EA's own on-chart panel or the trade comment if the EA writes one. If you are comfortable with MetaEditor, you can also write a small script that loops over the open trades and prints each one's magic number to the Experts log.
Choosing magic numbers for each setup
The rule: if two EAs or two copies of one EA could ever trade the same symbol on the same account, give them different numbers.
| Setup | Magic number to use |
|---|---|
| One EA on one chart | Any number nothing else on the account uses |
| Same EA on EURUSD and on GBPUSD | Different numbers (safe even if the EA also filters by symbol) |
| Same EA, same pair, two setting sets | Different numbers |
| Two different EAs | Different numbers |
| Same EA restarted, or moved to a new computer | The exact same number as before, so it picks up its open trades again |
A simple scheme you can copy: the first digits name the EA and the last digits name the pair. For example, use 1101 for EA 1 on EURUSD and 1102 for EA 1 on GBPUSD. Keep the list in your trading journal.
Checks before you let an EA trade
- Every EA and every copy on the account has its own number, and you have written them all down.
- Never use 0 for an EA. Manual trades also carry 0, so an EA set to 0 treats your hand trades as its own and can close or trail them. If an EA's default is 0, change it before it trades.
- Never change the number while the EA has open trades. The picture below shows why.
- On a demo account, open a small trade by hand on the EA's symbol. If the EA moves or closes it, the EA does not filter by magic number. Keep manual trading away from that symbol.
- Be strictest with grid and martingale EAs. Their basket maths only works if they count their own trades and nothing else.
- Separate numbers do not separate risk. All EAs still draw on the same balance and margin.
FAQ
Does the magic number change my spread, fills or profit?
No. The broker prices, fills and charges the trade the same way whatever the number is. It is a label for the software. It only changes which trades an EA acts on.
Can I see the magic number of a trade that has already closed?
Yes. It stays stored with the trade in your account history. A report or tool that reads history can group past results by magic number, which lets you judge each EA on its own record.
Can two different accounts use the same magic number?
Yes. An EA only sees the trades in the account it is logged into, so a number only has to be unique within one account.









