Magic Numbers in MT4: The EA's Internal ID
The Logic of the Identifier: What is a Magic Number?
In MetaTrader 4, a Magic Number is a unique numeric identifier assigned to every trade opened by an Expert Advisor (EA). It is a hidden 'Tag' that allows the EA to distinguish between its own trades and those opened by other EAs (or by you manually).
Without a Magic Number, if you ran two different bots on the same currency pair, they would accidentally try to modify or close each other's trades. This would lead to a chaotic 'Fight' between algorithms, resulting in losses and account instability. The Magic Number is the 'Digital DNA' of a trade.
How it Works: OrderSelect and Filter Logic
When an EA wants to check its open positions, it loops through the terminal's order list. It uses the OrderSelect() function and then checks if the OrderMagicNumber() matches the one it was assigned in the settings. This 'Filter' ensures that the EA only 'Sees' its own trades and completely ignores the manual trades you might have opened on your phone.
Choosing a unique Magic Number (e.g., 123456) for each instance of an EA is a critical best practice for any automated trader. If you run the same EA on two different pairs (e.g., EURUSD and GBPUSD), you can usually use the same Magic Number—but for safety, most professionals give each pair a unique ID.
Magic Number Execution Rules
Uniqueness: Assign a different ID to every distinct strategy
Record: Maintain a list of IDs to avoid accidental duplication
Filter: Ensure your MQL code checks OrderMagicNumber() strictly
Safety: Never mix EA IDs with manual trades (which are always 0)
Range: Numbers can range from 1 to 2,147,483,647
Verification: Check the 'Comments' tab in MT4 to see the assigned ID
Managing Multiple EAs on One Account
If you are managing a portfolio of 10 different Expert Advisors, managing your Magic Numbers becomes a vital part of your risk management. You should keep a simple Excel sheet or note where you record which number belongs to which strategy. This allows you to track the performance of each bot individually using third-party tools like MyFxBook or FXBlue, which can filter results by specific Magic Numbers.
The Manual Conflict: EA vs Human
One of the most common mistakes is for a trader to manually 'Move a stop-loss' or 'Partial close' a trade that was opened by an EA. Because manual trades have a Magic Number of 0, the EA will see that its trade has been altered by an 'External source'. Depending on how it was coded, the EA might try to 'Fix' the trade by resetting the stop-loss to its original level. This is why you should never manually intervene in an EA's trade unless you have disabled the bot first.