I have read much documentation but I'm still confused about the MagicNumber..
Is the MagicNumber stored exclusively by the EA that is performing trades on new orders and also is it stored by the server or just the EA?
In kind regards, Neal
The magic number is "stored" with every Order that is placed by the OrderSend() function. However, the Magic Number is User definable and not predetermined, except for "0" which is assign for manually placed orders.
So, in the EA code, usually in the input variables, you may have something like this:
input int MyMagicNumber = 123456789;
Then, in your EA every time you use OrderSend(), you use "MyMagicNumber" for the Magic Number parameter.
int ticket = OrderSend( _Symbol, type, lots, price, slippage, stoploss, takeprofit, comment, MyMagicNumber, expiration, color );
Similarly, every time you retrieve order information, when you select an order with OrderSelect() function, you should then test its "stored" Magic Number, using the OrderMagicNumber(), by comparing it against the EA's "MyMagicNumber".
for( int i = OrdersTotal() - 1; i >= 0; i-- )
if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
if( OrderMagicNumber() == MyMagicNumber )
if( OrderSymbol() == _Symbol )
// Do Something with the Order
Since you can have many EA's running, as well as manual trading (Magic Number = 0), the Order History contains all those orders. The Magic number thus allows for the EA to be able to filter out ONLY those orders that are associated with itself by using its Magic Number as an ID for selection.
It is up to the user/trader, to make sure that a unique Magic Number is assigned to each EA in use and if any EAs happen to have the same Magic Number by default, then the user must assign a different one via the EAs inputs.
To add comments, please log in or register