Hi folks,
I've decided to set aside an article for the Magic Number mystery because of the huge quantity of questions from the novices in the forex-tsd forum.
It's an article about a daily question!
What's the Magic Number and What's the Magic Number for?
This is one of the question we have got everyday in the forum:
"Hi,
I would like to learn how to alter a basic EA (say, the Simple MACD) to allow me to place multiple, concurrent orders for a given account. For example, if I want to be able to place an order for each different currency pair and not have any conflicts in terms of the order placing or order closing process.
I believe this has something to do with the OrderSend function and the "magic" parameter, but that's as far as I've gotten.
Any help on this would be great.
Thanks,
Ian"
Well Ian, you have given a hint of the what's the Magic Number for.
The Magic Number is one of the parameters (optional parameter) 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)
OrderSend() is the function you use in MQL4 to open a new order (instant or pending order) and the magic parameter (the ninth parameter ) is an integer value you set as a magic number of the opened order!
So, The answer of the first question is "The Magic Number is one of the OrderSend parameters which you can set when you send your order".
What's the Magic Number for? is the one on the negotiation table right now.
The Magic Number is a unique number you assign to your order(s) as a reference enables you to distinguish between the orders that opened by your expert advisor and the orders that opened by another expert advisors or manually.
To know how much important to use the Magic Number we have a simple code:
Magic Number Usage sample:
Our sample is an Expert Advisor that opens 4 buying order ( no conditions because it's just a sample) and close them all when the total profit of them reaches 100 Pips or the total loss is -100 Pips.
With the aid of using Magic Number our Expert Advisor is smart enough to know is the order has been opened by itself or it has been opened by another expert advisor or manually.
Here's the code:
int MagicNumber = 101030;
extern double Profit = 100;
extern double Loss = -100;
//+------------------------------------------------------------------
int init()
{
return(0);
}
int deinit()
{
return(0);
}
int start()
{
int cnt, ticket, total,n;
if(Bars<100) {Print("bars less than 100"); return(0);}
total = OrdersTotal();
if(total < 1)
{
OrderSend(Symbol(),OP_BUY,1,Ask,5,0,Ask+100*Point,"MagicNumber demo",MagicNumber,0,Green);
OrderSend(Symbol(),OP_BUY,1,Ask,5,0,Ask+100*Point,"MagicNumber demo",MagicNumber,0,Green);
OrderSend(Symbol(),OP_BUY,1,Ask,5,0,Ask+100*Point,"MagicNumber demo",MagicNumber,0,Green);
OrderSend(Symbol(),OP_BUY,1,Ask,5,0,Ask+100*Point,"MagicNumber demo",MagicNumber,0,Green);
return(0);
}
ProfitLossMonitor();
return(0);
}
void ProfitLossMonitor()
{
int total = OrdersTotal();
double MyCurrentProfit=0;
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber)
MyCurrentProfit += OrderProfit();
}
if(MyCurrentProfit>=Profit)
CloseAll();
if(MyCurrentProfit<=Loss)
CloseAll();
}
void CloseAll()
{
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
}
}
Code study:
The expert advisor at the first place has defined (declared) the integer variable MagicNumber to use it later in the code.
int MagicNumber = 101030;
The expert advisor opens 4 buy orders and use the function ProfitLossMonitor() to monitor the sum of profit or loss. But how can the Expert advisor know that current opened order has been opened by him or by another expert advisor?
The expert advisor checks the MagicNumber of selected order returned by OrderMagicNumber() against the MagicNumber variable to know is it our order or not:
if (OrderMagicNumber() == MagicNumber)
When the total of profit is equal to 100 or the total of the loss equal to -100 the Expert advisor will call the function CloseAll() to close all the opened orders have been opened by the expert advisors. And before closing those orders the Expert Advisor checks again the MagicNumber of selected order returned by OrderMagicNumber() against the MagicNumber variable.
Without using Magic Number could you imagine the operation of these kinds of expert advisors? How could they calculate the profit and loss of their orders? How could they close only their orders?
That's why the Magic Number is essential!
Life cycle of MQL4 program