Expert Magic Number in MT4 & MQL4


Technological advancement has proven to be one of the contributing factors to the increase in trading. Since everything can be done online, many traders and brokers are giving trading a chance. You can remotely trade from any part of the world. Online trading had simplified the process, to begin with, but the coming up of the MetaTrader platform made trading even better. 

As a platform, the MetaTrader has simplified trading by eliminating emotions. The Expert Advisors allow traders to make rational decisions by focusing on ‘what is’ rather than ‘what can be.’ Along with Expert Advisors, the MetaTrader platform also gives you expert magic numbers. Let’s explore what an expert magic number is in the MetaTrader platform.

 

What is the MT4 Expert Magic Number?

The expert magic number represents the MetaTrader concept to track open positions generated by the Expert Advisor. Using an expert magic number, EA can mark each position by number and distinguish the opened trades versus trades that did not open. The expert magic number can be any unique number; they are license plates for EAs.

expert magic number in mt4

The MT4 platform generates a magic number employed by Expert Advisors (EA) to track open positions. This allows the EA to manage trades in a better way. Is it still confusing? Let’s simplify it – equate a magic number with a driving plate. The latter allows the authority to identify the state from where the vehicle has come from and know about the exact individual who owns it. The expert magic number does a similar job. It helps the EA differentiate between the trades that have been opened by it and the ones that have still not opened. This analysis allows the EA to manage the account in a better way.

As I said, The MT4 Expert Magic Number is a unique identifier used in the MetaTrader 4 (MT4) trading platform, primarily associated with automated trading using Expert Advisors (EAs). Here’s a detailed explanation of its purpose and usage:

  1. Unique Identifier: The Magic Number is a unique numerical value assigned to each trade or order that an EA executes. This identifier distinguishes trades managed by different EAs or strategies within the same EA.
  2. Order Management: In a platform where multiple EAs are running, or if an EA runs multiple strategies, tracking which EA or strategy is responsible for which trade becomes essential. The Magic Number allows the EA to identify and manage its trades without interfering with other trades on the account.
  3. Trade Filtering: When an EA analyzes the current trades, it can filter them based on the Magic Number. This way, it only modifies, closes, or otherwise manages the trades it has opened. This filtering is critical in avoiding unintended interactions with trades opened manually or by other EAs.
  4. Strategy Identification: If an EA runs multiple strategies, each strategy can be assigned a different Magic Number. This allows for performance analysis and optimization of each strategy independently.
  5. Setting the Magic Number: The Magic Number is usually in the EA’s code. It can be a predefined number, or it can be dynamically assigned based on certain conditions. The number choice is arbitrary but must be unique within its context.
  6. No Physical Impact on Trades: It’s important to note that the Magic Number does not affect the physical aspects of a trade (like entry, exit, size, or stop loss). It is purely for identification and management within the EA’s logic.
  7. Backtesting and Optimization: During backtesting, the Magic Number helps segregate trades to analyze the performance of specific strategies or setups.
  8. Troubleshooting and Analysis: When reviewing trading logs or performing post-trade analysis, the Magic Number helps quickly identify which EA or strategy was responsible for a trade, facilitating easier troubleshooting and optimization.

 

Using a MetaTrader Magic Number

Many traders use the same Forex pair to trade across multiple timeframes. If you are one such trader, you wouldn’t want to employ an EA to track one currency pair across different timeframes. For example, consider a situation in which you are trading GBP/USD in two ways, one using an hour chart and the other on a 15-minute chart. You will need two EAs to track these two different time frames. Here, the first EA will be trend following as it tracks the one-hour chart, and the other is scalping as it is chasing a 15-minute chart. It is clear from the example that you should manage trades differently when you are using different timelines.

The above example is a testimony that along with your general stop-loss exits and takes profit, you need to run channel stops for one trade and trailing stops for the other. In such cases, you will end up in a chaotic mess if you use only the currency pair to identify different trades because the currency pair is standard across the channels. It would help if you had something that stands apart.

Your bulletproof savior in the above situation is a MetaTrader magic number. The platform assigns a specific number to each trade as soon as it is opened. The EA will now follow these unique numbers instead of the currency pair to track the trade. For example, the trend-following EA may follow the 214 magic number, while the other EA might chase the magic number 316. 

Thus, we can conclude that you can use these magic numbers to separate orders across different timeframes and strategies, especially when using the same currency pair. It makes EA more operational by allowing it to use its potential.

Finding a Magic Number for an Open Trade

The magic number for the trades opened manually is zero, but the trades opened on the MT4 platform using an EA are assigned unique numbers ranging from 0 to 2147483647. These numbers can help you in finding open trades. Let’s see how.

Finding the Magic Number for open trade in MetaTrader 4 (MT4) involves a few steps, especially using Expert Advisors (EAs) to automate your trading. Here’s a step-by-step guide on how you can find the Magic Number for an open trade:

Adding a Magic Number in MQL4, the programming language used for creating scripts and Expert Advisors (EAs) in MetaTrader 4, is straightforward. The Magic Number is a unique trade helpful identifier when running multiple EAs or strategies. Here’s how you can add a Magic Number to your MQL4 code:

  1. Define the Magic Number: FirDefineMagic Number for your EA. This is typically done at the beginning of the code. You can choose any integer value as your Magic Number if it’s unique to this particular EA or strategy.

    int MagicNumber = 12345; // Example Magic Number


  2. Use the Magic Number in Order Functions: When you place an order using functions, you must include the Magic Number as one of the parameters. Each trade the EA opens will be tagged with this unique identifier.

     

    OrderSend(Symbol(), OP_BUY, 0.1, Ask, 2, 0, 0, "My EA Order", MagicNumber, 0, clrGreen);

     

    In this example, MagicNumber it is passed as one of the arguments to the OrderSend() function. The function parameters are, in order:

    • Symbol: The currency pair or instrument.
    • OP_BUY: The type of order (buy in this case).
    • Lot size: 0.1 in this example.
    • Price: Ask Price for a buy order.
    • Slippage: Allowed slippage in points.
    • Stop loss: 0 (not set in this example).
    • Take profit: 0 (not set in this example).
    • Comment: “My EA Order”.
    • Magic Number: The unique identifier for the EA.
    • Expiry: 0 (not used here).
    • Color: clrGreen.
  3. Manage Orders Using the Magic Number: When you need to manage orders (like closing or modifying them), you can filter them by the Magic Number. This ensures that your EA only manages its trades and not others.Example of checking for the Magic Number in a trade:

    for(int i = 0; i < OrdersTotal(); i++) {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderMagicNumber() == MagicNumber) {
    // Your code to manage the order
    }
    }


  4. Testing and Optimization: After implementing the Magic Number in your EA, test it on a demo account to ensure it’s working as expected. You may also perform strategy tests in MT4 to evaluate its performance.
  5. Adjustment and Fine-Tuning: Depending on your strategy, you might need to adjust the Magic Number or how it’s used in your EA. Ensure it uniquely identifies the trades made by your specific strategy or EA.

Following these steps, you can effectively integrate a Magic Number into your MQL4 EA, allowing for efficient trade management and clear distinction between trades made by different EAs or strategies within the same EA.

The MQL language uses OrderSend to open a trade. OrderSend is a function based on multiple parameters, the magic parameter being one of them. When a trade is opened by an EA using this function, a magic number is automatically assigned to the trade, known as OrderMagicNumber. Doesn’t it sound simple? When you employ this function for the first time, it isn’t a straightforward path, but it certainly gets easier with time. The more you use it, the more familiar you get with it.

All you need to do is to run OrderMagicNumber in an order selection loop. Select a trade by using OrderSelect. This will allow you to run the OrderMagicNumber for your chosen trade. Once your trade has been assigned a magic number, you can use the ‘if-then’ logic to put trade exits. It is a relatively easy technique to understand. For example:

  1. If my magic number is equal to 197, run a trailing stop.
  2. If my magic number is equal to 303, run a channel stop.

MT4 is a user-friendly platform that various brokers offer to their clients. Its codes are not as complicated as compared to other platforms. Yes, it does take some time to get used to it, but such is the case with anything new that you use. Along with EAs and magic numbers, it has various other features to maximize profits.

In summary, the MT4 Expert Magic Number is a crucial feature for automated trading, ensuring efficient and organized trade management, especially when multiple automated strategies or EAs are active simultaneously on a trading platform.

Fxigor

Fxigor

Igor has been a trader since 2007. Currently, Igor works for several prop trading companies. He is an expert in financial niche, long-term trading, and weekly technical levels. The primary field of Igor's research is the application of machine learning in algorithmic trading. Education: Computer Engineering and Ph.D. in machine learning. Igor regularly publishes trading-related videos on the Fxigor Youtube channel. To contact Igor write on: igor@forex.in.rs

Trade gold and silver. Visit the broker's page and start trading high liquidity spot metals - the most traded instruments in the world.

Trade Gold & Silver

GET FREE MEAN REVERSION STRATEGY

Recent Posts