Adding a Magic Number to an EA
October 27, 2006 12:04 amSomeone asked me if there was a manual on how to add a Magic Number to an EA. Since I haven’t seen one, I put together the following basic steps
and thought that others may find it useful too. These steps can also be used to verify that an EA properly uses a Magic Number, as some don’t!
First off, to the completely uninitiated, a Magic Number is simply an arbitrary number that can be added to an EA that is used to uniquely identify trades that are made from that EA with an eye towards differentiating them from trades made by any other EA. Without a Magic Number, a EA that closes all orders will not only close orders that it has made, but it will also close any orders made by a different EA, and it will even close orders that were made manually, as it has no way of telling which orders came from which EA.
This obviously is not an optimal situation. The Magic Number lets you differentiate these orders programatically so that you can run multiple EA’s on multiple charts from within the same MT4 instance and still be able to trade manually as well.
Anyway, now on to the steps:
1) On a global level in the EA add:
int MagicNumber = 233423; // where 233423 is any old number that is unique from any of your other running EA’s
The easiest way to describe declaring on the “global level” of the EA is a variable that is declared outside of the Start, Init and Deinit functions. The easiest way to ensure that you are on the global level of the EA is to put the MagicNumber declaration immediately after the top comments and any #property, #include or #import statements and BEFORE the Init, Deinit and Start functions.
2) Next, make sure that the OrderSend function uses this magic number. OrderSend uses the following form:
int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)
So, you want to make sure that it is called with the MagicNumber in it, e.g:
OrderSend(Symbol(),OP_BUY,1.0,Ask,2,10,20,”My Buy”,MagicNumber,0,Lime)
3) Then, search the EA for each instance of OrderClose, OrderModify or OrderDelete. Usually these are sandwiched by at least one IF statement. Add to that IF statement the following condition: (OrderMagicNumber() == MagicNumber)
So, something like this:
if (OrderSymbol == Symbol())
{
OrderClose(blah blah…);
}
Becomes:
if (OrderSymbol == Symbol() &&
OrderMagicNumber() == MagicNumber)
{
OrderClose(blah blah blah);
}
Voila! Your EA now uses a magic number and should be able to trade with other EAs.
Please note that any money management that an EA uses, like lot optimization based on available margin or equity, etc. will also “interfere” with each other. This is either desirable or undesirable based upon how you want to trade with the specific EA’s.
Hope that helps!


5 Responses to “Adding a Magic Number to an EA”
Hi there cubesteak…. Thank you one more time for clarifying how to add magic numbers to the EA’s…. Thats quite detailed and also clear for even newbies like me…. Talk to you soon!..
My pleasure Emre! Glad to be of service.
Great post! Thanks.
You are most welcome Al… Glad it helped!
thank you for alles ..
Care to comment?