How To Use the Magic Number in MetaTrader4 MQL4

The Magic Number is an identifier that you can assign to an order in Metatrader 4 using the MQL4 language.

What is The Magic Number

When working with Scripts, Indicators and Expert Advisors in MetaTrader4 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!

The Magic Number is an identifier for the orders.

Basically when you open an order in MetaTrader4 using MQL4 code you can associate the order to a Number.

The same Magic Number can be used in many orders.

In alternative, with some code you could generate a different Magic Number for each order, however this will not be helpful as you will see in the next section.

Why you need a Magic Number

At the start of your trading journey you probably deal with only one or a few orders at a time.

Becoming more expert you may increase the number of open orders or strategies that you are using.

When you automate your trading through MQL4 using the Magic Number is necessary to select which orders to work with.


The Magic Number can be use to:

  • Separate orders using different strategies
  • Separate orders for the same instrument running on different time frames
  • 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 pair EURUSD.

One expert advisor is a scalper working better on a 15 Minute chart while the other 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 in its graphical interface natively.

You can work with Magic Numbers coding your own tools in MQL4 like I explain further in this article.

However, not everyone is able to code or wants to spend time on it.

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

One Click Trade For MT4 is FREE and allows you to submit orders associating them to a Magic Number.

It can also allow you to see the magic number of your open orders.

How to use the Magic Number with MetaTrader 4 and MQL4 Language

Work with Magic Numbers in MQL4 Language is quite easy.

MQL4 already has native functions to assign and read the Magic Number.

When you open an order with the OrderSend()  function one of the optional parameters is the Magic Number.

So with this function you associate a Magic Number to 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 write 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 the trader) to specify a Magic Number for the code running.

In the download section you can find working examples that are using the magic number.


The Magic Number is a number that helps MetaTrader4 and MQL4 identifying the orders. A Magic Number is necessary especially when running different Expert Advisors and different strategies. MQL4 makes it very easy to work with Magic Numbers.

If you like this article or have any question please comment or Contact Us.

9 thoughts on “How To Use the Magic Number in MetaTrader4 MQL4”

  1. Hello,

    Good Article!!! But I have a doubt, how can I see magic number once trades are closed? in order to perform mentioned analysis (export closed trades to excel to analyze them depending on EA that opened them)

    • Hi Jose,
      Metatrader doesn’t have this function in the interface, you would need to code it. It is possible to retrieve the magic number for closed orders.
      Let me know if it doesn’t make sense.


  2. May I ask What is the different between SELECT_ BY _ POS and TICKET ?
    I couldn’t get the correct ticket number with SELECT _ BY _ TICKET until I change it to BY _ POS.
    Why ?

    • Hi James,
      With SELECT_BY_POS you basically pick the order by position in an array of all orders, imagine a list of the orders and if you go with SELECT_BY_POS you select the order by position in the list, that is why you usually use SELECT_BY_POS in a for or while loop.
      SELECT_BY_TICKET you select the order by ticket number, so you need to know it already.
      Does it make sense?

  3. Thank you Luca !
    One Qns : How to request end user input ? i. e. external key in input.
    Ex. I want the user to input a password for verification b4 proceed.

    • You can either insert it when you start the EA or indicator with an “input” variable or design some sort of interface, but that’s more complex and long to explain.


  4. Scenario – i have 3 EAs which i assigned 111, 222, 333 magic#. So my question is can different pairs orders having the same magic#? Example can eurgbp orders having magic# 111… and audusd orders also having magic# 111 if open by same EA?

    • Hi CJ,
      Technically there is no limitation or rule that says to assign different or same magic number to different pairs.
      It is up to the programmer to design the EA and use the Magic Number as it’s preference.
      In my view I would prefer to use a different Magic Number for different EA and ALSO different Magic Number for different pairs. The reason is that Magic Number can also be used for reporting, say you want to analyse the performance of EA and pairs, you could export all the trades and differentiate by Magic Number to see profitability and other statistics.

Leave a Comment