Question about Magic Number

 

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

 
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.

 
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?

 
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.
 
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 :|


 
Maybe your check of the open position is not in the right place? Try to do an initial check in OnInit()?
 
Carl Schreiber: Maybe your check of the open position is not in the right place? Try to do an initial check in OnInit()?
Do not check in OnInit because there may not yet be a connection to the server and no trades list yet.
 
whroeder1:
Do not check in OnInit because there may not yet be a connection to the server and no trades list yet.


In the toolbox I can see the orders and the position but I was thinkning maybe the trade list is not available yet  for the EA busy in calculating indicators and other stuff.... I have just tried to put a counter and a PRINT to check the situation. 

See you later for an update.

Reason: