Download MetaTrader 5

What is and how to set the magic number at EA's ? - page 2

To add comments, please log in or register
Sell products via the Market and track your sales statistics
jonus 2009.03.31 21:13  


The "magic" number can be any number you choose. Simply put it's used within an EA to identify trades that belong (were opened by) that EA. It can be ANY number you choose. The main concern with choosing your magic is that you want it to be unique for each and every implementation of the EA. If you hard code the number in the EA lets say 50000, then each chart you attach the EA to will have the same magic number, and each will not be able to find its own trades. To overcome this, problem I would simply add the charts time frame to it like this:

int thisEA=50000;


But this will not be unique once you start attaching the EA to different currency pairs. Better yet, assign a number to each currency pair you will ever trade for example eurusd=1000, audusd=2000, etc. I dont know if somehwere in MQL each currency pair already has a number assigned to it. If they do then someone else can clarify for us and this code will be much simplified. Assuming you will be trading from multiple EAs on the same Pairs and Time Frames, again I would assign each EA a unique number. Then you would have code that looks something like this:

int thisEA=50000;

if (Symbol() == "EURUSD") {mymagicnum= thisEA+1000+Period();}

if (Symbol() == "AUDUSD") {mymagicnum= thisEA+2000+Period();}


Now you will have a unique magic number for all trades made by each EA for each time period and each currency.

Hanh Tran
Hanh Tran 2016.06.21 06:18  

Hi All,


How i can use some magicnumbers into an EA only?


Pls help the way!

Carl Schreiber
Carl Schreiber 2016.06.21 07:12  

The magic number is your reference of the order you send (the ticket number is the broker's reference).

So set the magic number in OrderSend() and if you loop through the (closed or open) orders you get it by OrderMagicNumber().

It is an integer so you are free to choose -2'147'483'648 to 2'147'483'647.

Common use is that EAs do have individual magic numbers so that they identify their own positions.

Fernando Carreiro
Fernando Carreiro 2016.06.21 07:30  
hanhtran.qt: How i can use some magicnumbers into an EA only?

Read the following post for some extra guidance:

hao nguyen xuan
hao nguyen xuan 2016.12.24 09:02  
thank for your topic. Magic number is same ID of Order Send per 1 pair currency. I think so.
To add comments, please log in or register