How to Use Magic Number in MT4 (2024)

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.

How to Use Magic Number in MT4 (1)

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.

How to Use Magic Number in MT4 (2024)
Top Articles
E*TRADE Online Trading | Active Trading
Crypto firm sues SEC to fend off oversight of Ethereum
My E Chart Elliot
Craigslist Mpls Mn Apartments
Voordelige mode in topkwaliteit shoppen
Crocodile Tears - Quest
How To Get Free Credits On Smartjailmail
Pickswise the Free Sports Handicapping Service 2023
According To The Wall Street Journal Weegy
Anki Fsrs
William Spencer Funeral Home Portland Indiana
Find The Eagle Hunter High To The East
Craigslist Heavy Equipment Knoxville Tennessee
What Is A Good Estimate For 380 Of 60
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
Studentvue Columbia Heights
How to find cash from balance sheet?
Midlife Crisis F95Zone
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Google Flights Missoula
Erica Banks Net Worth | Boyfriend
The Pretty Kitty Tanglewood
Tinker Repo
Sulfur - Element information, properties and uses
Laveen Modern Dentistry And Orthodontics Laveen Village Az
R. Kelly Net Worth 2024: The King Of R&B's Rise And Fall
Rochester Ny Missed Connections
Walgreens Bunce Rd
Mals Crazy Crab
Fiona Shaw on Ireland: ‘It is one of the most successful countries in the world. It wasn’t when I left it’
Egusd Lunch Menu
Section 408 Allegiant Stadium
Jeep Cherokee For Sale By Owner Craigslist
Little Caesars Saul Kleinfeld
Max 80 Orl
Ourhotwifes
Serenity Of Lathrop - Manteca Photos
How to Destroy Rule 34
Viewfinder Mangabuddy
Cherry Spa Madison
Philadelphia Inquirer Obituaries This Week
Updates on removal of DePaul encampment | Press Releases | News | Newsroom
manhattan cars & trucks - by owner - craigslist
Myrtle Beach Craigs List
8776725837
Petra Gorski Obituary (2024)
The Great Brian Last
Rheumatoid Arthritis Statpearls
Www.homedepot .Com
Wera13X
Cars & Trucks near Old Forge, PA - craigslist
Strawberry Lake Nd Cabins For Sale
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6020

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.