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

Back to topics list  | 1 2 To post a new topic, please log in or register
hkjonus 2009.03.31 23: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.

hanhtran.qt 2016.06.21 08:18 #

Hi All,


How i can use some magicnumbers into an EA only?


Pls help the way!


gooly 2016.06.21 09: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.

FMIC 2016.06.21 09:30 #
hanhtran.qt: How i can use some magicnumbers into an EA only?

Read the following post for some extra guidance: https://forum.mql4.com/72829#1037293

Back to topics list   | 1 2  

To add comments, please log in or register