OrderMagicNumber function


for example, forex

Very often a trader requires to identify somehow orders or positions. For example it is useful to know whether this order was open namely by this expert advisor or on what period of the chart the trading signal worked. The developers of MetaTrader 4 have provided for such cases a very useful trading function — OrderMagicNumber().

   int OrderMagicNumber()

The OrderMagicNumber() function returns the integer number which was set for this order by the OrderSend() function in the parameter magic.

Leaping ahead I’ll tell you about the format of the OrderSend() function:

   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)

The OrderSend() function is used to place a pending order or to open a position. In the following article I will write about it in detail.

As I said previously the magic number for an order or a position is passed to the function by means of the parameter magic.

Let’s suppose that several expert advisors each with its own number (ID) are working. Let’s suppose that a situation when one expert advisor works in one and the same instrument but on the different periods of the chart is also possible. All these experts perform transactions and place orders, but to manage further these orders and positions one should be able to determine by what expert and on what period of the chart these positions were open (orders were placed), so that not to close accidentally a position open by another expert advisor or by the same expert advisor but on another period of time.

For such purposes the following format of the magic number will be suitable: the last two digits of the number — ID of the expert, the first digits — the chart’s period.

In MetaQuotes Language there exist the following constants which denote periods of charts:

ConstantValueDescription
PERIOD_M111 minute
PERIOD_M555 minutes
PERIOD_M151515 minutes
PERIOD_M303030 minutes
PERIOD_H1601 hour
PERIOD_H42404 hours
PERIOD_D114401 day
PERIOD_W1100801 week
PERIOD_MN1432001 month
0 (zero)0The period of the current chart

Table. 1. Charts’ periods

On this basis we can describe 3 functions:

   // get the magic number from the chart’s period and expert’s ID
   int GetMagicNumber( int chart_period, int expertID )
     {
      return( chart_period * 100 + expertID );
     }
// get the chart’s period from the magic number int GetPeriod( int magic ) { return( magic / 100 ); }
// get the expert’s ID from the magic number int GetExpertID( int magic ) { return( magic % 100 ); }


Next article: "OrderSend function"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?