Download MetaTrader 5

Question about Magic Number

To add comments, please log in or register
Neal_Van
331
Neal_Van  

I have read much documentation but I'm still confused about the MagicNumber..

Please help.

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

Fernando Carreiro
2239
Fernando Carreiro  
Neal_Van:

I have read much documentation but I'm still confused about the MagicNumber..

Please help.

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.

tradatore
50
tradatore  
Fernando Carreiro:

The magic number is "stored" with every Order that is placed by the OrderSend() function.


Hi,

the magic number is stored only on the meta trader client right?

Carl Schreiber
6784
Carl Schreiber  
tradatore:


Hi,

the magic number is stored only on the meta trader client right?

No, as part of the order/position it exists on the server side as well, but other than the comment it cannot not be changed by the server.
tradatore
50
tradatore  
Carl Schreiber:
No, as part of the order/position it exists on the server side as well, but other than the comment it cannot not be changed by the server.


So I need to investigate what is wrong.

My EA opens some positions and all is ok. Then I decide to simulate a break out(pc failure, elettricity etc...) 
So I reboot the pc and i Start the mt5 and the EA again but it seems not able to find open positions :|


Carl Schreiber
6784
Carl Schreiber  
Maybe your check of the open position is not in the right place? Try to do an initial check in OnInit()?
To add comments, please log in or register