Mastering the Magic Number: Your Guide to Order Identification in MetaTrader
If you’re venturing into automated trading with MetaTrader 4 or 5, you’ve likely encountered a crucial but often misunderstood setting: the “Magic Number” (sometimes just called “Magic”). Far from being a mystical figure, this parameter is a fundamental tool for maintaining order and control in your trading account. Whether you’re a novice user running a single Expert Advisor (EA) or an advanced trader managing a portfolio of robots, understanding the Magic Number is essential for efficient and error-free operation.
What is a Magic Number?
At its core, a Magic Number is a unique integer identifier assigned to every order placed by an Expert Advisor or script. Think of it as a digital “nametag” or “ID badge” for each trade. Its primary purpose is to allow an EA to distinguish its own orders from those placed by:
Without this identifier, an EA would see a chaotic list of all open orders and positions on your account, making it impossible to manage its own strategy logically. It could mistakenly close trades from another system or incorrectly calculate its own position size.
The Default: Manual Orders and the Zero Magic
A key point of confusion is the Magic Number for manual trades. By default, any order opened directly from the MetaTrader terminal—by clicking Buy or Sell—receives a Magic Number of 0. This zero value acts as a universal flag, indicating “this trade was not initiated by automated code.” For an EA to recognize and manage manual trades, it must be specifically programmed to look for orders with Magic 0.
Locating and Configuring the Magic Number Setting
Most well-designed commercial and free Expert Advisors expose the Magic Number as a configurable input parameter for the user.
How to Find It:
You can do this by:
Double click on the parameter and set your identifier.
How to Set It:
Within the settings list (usually under the “Inputs” or “Common” tab), look for a parameter named “Magic Number,” “Magic,” or “Identifier.” It is typically found near the top of the list. Simply double-click on the value field and enter your chosen unique number (e.g., 123456, 1001, 888).
Important Note: If you cannot find this option in the settings, it means the EA developer has hard-coded the Magic Number into the program’s logic. This is common in simpler or older EAs. In this case, you cannot change it, and the EA will only manage orders with its predefined identifier.
Practical Configuration Scenarios & Best Practices
How you configure your Magic Number depends entirely on your trading setup:
Running a Single EA: You can often leave the number at its default setting (frequently 0 or a pre-set like 123456). However, setting a unique, non-zero number (e.g., 100001) is a good practice for future-proofing.
Running Multiple EAs on One Account (Portfolio Trading): This is where the Magic Number becomes critical. You must assign a unique, non-overlapping Magic Number to each EA. For example:
This ensures each robot manages only its own trades, preventing catastrophic interference where one EA closes another’s positions.
Using a Recovery Manager, Trade Copier, or Trade Panel: These utility tools are designed to manage orders from a specific EA. For them to function correctly, you must configure their Magic Number filter to match exactly the Magic Number of the EA they are supposed to support. This directs the tool to monitor and act on the correct set of trades.
Enabling an EA to Manage Manual Trades: If you want your EA to apply its management rules (like adding trailing stops or closing conditions) to trades you opened manually, you typically need to set the EA’s Magic Number to 0. This instructs it to look for orders with the manual trade identifier. (Warning: Ensure the EA’s logic is designed for this, as it can be risky.)
Advanced Considerations & Troubleshooting
In summary, the Magic Number is a simple yet powerful concept that brings order to automated trading. By taking a moment to configure it thoughtfully, you create a clean, manageable, and conflict-free trading environment, allowing your EAs to perform their tasks with precision and reliability.










