How to Use Magic Number in MT4

A magic number is an identifier that you can assign to an order in MetaTrader 4 using the MQL4 language.

What Is Magic Number?

When working with scripts, indicators, and expert advisors in MetaTrader 4, you may arrive to a stage where you manage many orders at the same time.

It is important to be able to manage the different orders running under different strategies and that is why you need a magic number!

A magic number is an identifier for the orders. Basically, when you open an order in MetaTrader 4 using MQL4 code, you can associate the order to a number.

The same magic number can be used in many orders. Of course, you could generate a different magic number for each order, but this wouldn't be helpful as you will see in the next section.

Why Use Magic Numbers?

At the start of your trading journey, you probably deal with only one or few orders at a time. As your experience grows, you may increase the number of open orders or strategies that you are using at the same time.

When you automate your trading through MQL4, using a magic number is necessary to select which orders to work with.

Each EA Uses Its Own Magic Number

A magic number can be use to:

  • Separate orders originating from different strategies.
  • Separate orders for the same instrument but based on different timeframes.
  • Separate orders opened by different expert advisors.

To mention an example, assume you have two expert advisors and you want to run all of them on the EUR/USD currency pair. One expert advisor is a scalper working better on a 15-minute chart, while the other one is more suitable for a 1-hour chart. If the expert advisors are coded properly, you will be able to assign different magic numbers to the orders opened by the two expert advisors. Using two different magic numbers will allow the expert advisors to work only on their own trades.

How to Use Magic Numbers in MT4?

MetaTrader 4 does not allow to work with magic numbers via its normal interface for manual trading.

You can work with magic numbers when coding your own tools in MQL4 like explained further in this guide. However, not everyone is capable of coding or has enough time to spend on it.

It is for this reason, that we created a tool that can help you significantly with this aspect of trading.

One-Click Trade Pro for MT4 is a free order management panel that allows you to submit orders associating them to a magic number. It can also show you magic numbers of your open orders.

How to Use Magic Numbers in MQL4?

Working with magic numbers in MQL4 language is quite easy.

MQL4 has native functions to assign and read a magic number.

When you open an order with the OrderSend() function, one of the optional parameters is a magic number. So, with this function, you associate a magic number with the order you are opening.

When you select an order with OrderSelect(), you can retrieve the magic number for the selected order using the function OrderMagicNumber().

When you code an expert advisor or a script that opens and updates orders, I would suggest to always use a magic number.

The example below shows you some code that you should include in your programs to allow you (or other traders) to specify a magic number for the code running.

// The Magic Number is asked before the code starts running.
extern int MagicNumber=11223344;       // Magic Number to use for the orders.

// When you submit the order, you specify the Magic Number.
OrderSend(Symbol(),Command,PositionSize,OpenPrice,Slip,SLPrice,TPPrice,"",MagicNumber,0,Green);

// When you scan the open orders, you can select to perform an action only to the orders matching the Magic Number.
for (int i = 0; i < OrdersTotal(); i++) {
   if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
      Print("ERROR - Unable to select the order - ",GetLastError());
      break;
   } 
   // If the Magic Number of the order matches the Magic Number entered at the beginning.
   if(OrderMagicNumber()==MagicNumber){
      // Action to execute.
   }
}

All our expert advisors are capable of using magic numbers for assigning to orders, for filtering orders, or for both.

Conclusion

A magic number is an order parameter that helps MetaTrader 4 and MQL4 identify orders. A magic number is necessary especially when running different expert advisors and different strategies in the same trading account. MQL4 makes it very easy to work with magic numbers.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.