×

CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 75.00% of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

Authorised and Regulated: FCA UK / GLOBAL

What are MT4 Magic Numbers?

Double exposure of business woman and stock market graph

As if online trading doesn’t seem like magic enough, along came the MetaTrader platform that made it even easier to access the markets. Following that we have the magic of Expert Advisors that can help mitigate issues of human emotion or error. And it doesn’t stop there either, you also have magic numbers. Seems enchanting, doesn’t it? So, how do you use all this magic to your advantage?

A magic number on MT4 is a unique number that is used by Expert Advisors to track open positions. Just as a license plate allows you to track not only the state a vehicle belongs to but even the individual who owns it, an EA uses the magic number to distinguish trades that it has opened, as compared to those it has not. By the magic number, the EA knows which trades it needs to manage.

How MT4 Uses Magic Numbers

Magic numbers are especially useful when you are trading across multiple timeframes, using the same forex pair. For instance, you might wish to use two EAs, one that is tracking the EUR/USD on a one hour chart, while another that is tracking the same pair on a 15 minute chart. So, while one EA is trend following, the other is scalping. Can you imagine one EA trying to track a single trade across different time frames? You wouldn’t want to do that even to an enemy, and an expert advisor is your best friend in forex.

So, while trading across different time lines, you might want to manage the trades differently for each time line. This means that apart from the usual take profit and stop loss exits, you might want to run a channel stop for one trade and a trailing stop for another. In such situations, trying to identify trades using only the currency pair will make it chaotic, given that the currency pair is the same across the different trades, such as the EUR/USD in the example above.

This is where magic numbers come to the rescue. A unique number is assigned to each trade, when it is opened. The EAs now track trades by their magic numbers, simplifying the process. For instance, the trend-following EA might follow the magic number 214, while the scalping one follows 316.

To summarize, magic numbers can be used to separate orders across different strategies, separate orders across different timeframes for the same currency pair, and to separate orders across different EAs.

How to Find the Magic Number for an Open Trade

If a trade is opened manually, its magic number would be 0. However, trades opened by EAs are assigned unique magic numbers on MT4 that range from 0 to 2147483647. So, where can you find this unique number?

The MQL language uses a function called OrderSend to open a trade. This function is based on several parameters, of which one is the magic parameter. So, when an EA opens a trade with the OrderSend function, the magic parameter assigns a unique number to the trade. This function is called OrderMagicNumber. So, when you choose an order using OrderSelect, you can find the magic number for this specific order using the OrderMagicNumber function. Sounds too simple to be true, doesn’t it? Well, the truth is that it isn’t that simple, although as you do it repeatedly, it will become easier.

What you need to do is to run OrderMagicNumber in an order selection loop. Use OrderSelect to choose a trade, following which you can run the OrderMagicNumber function for the chosen trade.

The MQL code for this is:

int total=OrdersTotal();
int mymagic;
for(int pos=0;pos<total;pos++)
{
if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
mymagic = OrderMagicNumber();
}

Once you have the magic number of a trade, you can implement trade exits, using a simple “if-then” logic. For instance:

  • if mymagic is equal to 214
  • run a trailing stop
  • if mymagic is equal to 316
  • run a channel stop

MT4 is actually a very user-friendly platform. And while using its codes might seem overwhelming to a beginner, it really does become much easier as you use it repeatedly. The entire aim of using EAs and magic numbers is to make trading simpler for you, while maximizing the potential to profit.

Disclaimer

If you liked this educational article please consult our Risk Disclosure Notice before starting to trade. Trading leveraged products involves a high level of risk. You may lose more than your invested capital.

Start Trading in 3 easy steps
1
Complete the Application Form
It takes just minutes for us to verify your identity and set up your account.
2
Download MT4 Platform
Download MT5 and trade via your desktop, mobile or tablet. No minimum deposit required.
3
Start Trading
Trade more than 60 products (Forex, CFDs & Commodities)