last trade

 
nead help, new to mql4. i do i call the last trade that was excicuted;
 
int last_trade=HistoryTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
     {
      Print("Last trade ticket ",OrderTicket(),"   op ",OrderTicket(), "   time ",TimeToStr(OrderCloseTime()),"   price ",OrderClosePrice());
     }
  }
 
stringo:
int last_trade=HistoryTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
     {
      Print("Last trade ticket ",OrderTicket(),"   op ",OrderTicket(), "   time ",TimeToStr(OrderCloseTime()),"   price ",OrderClosePrice());
     }
  }

I found that the HistoryTotal list is sorted by ticket number when EA or script launches. So the formula above won't work at initialisation. Besides that the 'last trade' term might include last open position. The cheapest way is to keep the lass accessed ticket number in a global variable. Although the most reliable way is to compare OrderOpenTime and OrderCloseTime in both MODE_HISTORY and MODE_TRADE lists to find the ticket with the latest time recorded.

There is another condition missed from the first post - whether or not 'last trade' should take into account pending orders executed automatically or positions closed by stop loss or take profit on server side. In this case you need to analyse OrderOpenPrice and OrderClosePrice as well.

Regards,

mqlexpert {AT} gmail {DOT} com

PS
Slawa, there is a typo in the example you provided. Should be '... " op ", OrderType(), ' I suppose.