Magic Number Identifier EA

 

Hi, I am a newbie in mql4 programming. I was trying to code a simple EA to identify the magic numbers of open orders. 

Why this code is not working? On compiling, it didn't give any errors but I can not see the comment on chart.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
for(int i=OrdersTotal(); i>=0; i--) 
{
   if ( OrderSymbol() == Symbol() &&  OrderType() == OP_BUY)
         {
         
         int number = OrderMagicNumber();
         int ticket = OrderTicket();
         Comment ( "OrderTicket is ", " " , ticket , " and its magic number is " , " ",  number );
         
  }
  }
  }
//+------------------------------------------------------------------+
 
Hatem Abou Ouf:

Hi, I am a newbie in mql4 programming. I was trying to code a simple EA to identify the magic numbers of open orders. 

Why this code is not working? On compiling, it didn't give any errors but I can not see the comment on chart.

You have to select the ticket before calling any methods on it. If you are iterating over the tickets in the pool by position then you need to select it by position. Also, just some general tips, learn how to format your code using an indention style. MQ uses GNU indentation by default and you can automatically format your code using the styler in the editor, however, most (c++/java) programmers prefer the Allman style. Finally, you have to pay close attention that you are not creating array out of range bugs in your for loops. 

void MagicNumbersToChart(const int magic_number)
{
   string result = "";
   for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == _Symbol && OrderMagicNumber() == magic_number)
      {
         result+=StringFormat("orderTicket: %d, magicNumber: %d\n", OrderTicket(), OrderMagicNumber());
      }
   }
   Comment(result);
}
Reason: