Last updated: May 17, 2026
Foundations

Magic Numbers in MT4: The EA's Internal ID

Trade-Charts IntelUpdate 2026.03

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.

Foundation Key

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.

Frequently Asked Questions

Can I use '0' as a Magic Number?

No. In MetaTrader, 0 is the default Magic Number reserved for manual orders. If you set your EA to use Magic Number 0, it will treat every trade you open manually as its own and will try to manage or close those trades. Always use a number greater than 1.

What if I use the same ID on two MT4 accounts?

That is perfectly fine. Magic Numbers are only used locally by the individual MetaTrader terminal to manage its internal order pool. There is no conflict between different accounts or different brokers even if they use the same Magic Numbers.

Recommended Reading