Know when a trade closes

 

I know I have asked this before, but I could not find it in my past threads. I also did a search for something like it and could not find a reference.

I know that I need to basically do a command where I do something like the following:

      int CntClosed = 0;
      for(int i = 0; i <= OrdersHistoryTotal(); i++) 
      {
          OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbols) 
         {
            CntClosed += 1;
         }  
     }
      int CntClosed = 0;
      for(int i = 0; i <= OrdersHistoryTotal(); i++) 
      {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbols) 
         {
            // ???? 
         }
      }

In the section where the ??? are, what do I need to be able to get the most recently closed trade?

My idea is to use the above code to first maintain a count value of closed trades (this part I can handle). If the count is different then I want to use the above code to figure out which trade closed (this is the part I need help with). This way I can grab the ticket number of the most recently closed trade.

 
      datetime last=0; 
      int retval;     
      for(int i = 0; i <= OrdersTotal(); i++) 
      {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) 
         {
            // ?????
            if(OrderCloseTime()>last){
             last=OrderCloseTime(); retval=OrderTicket();
            }

         }
      }
//z
 

I corrected my code snippet, glad you understood it. FYI, the OrdersTotal should be changed to OrdersHistoryTotal for those who wish to use this code.

thank you for the help here. I will put it to good use ;-)

Reason: