How to know the last closed order info.

 

I want to know the profit for the last order.

The last (newest) closed order number  is

OrdersHistoryTotal() -1 

Is this correct?

So, I coded like; I think I don't need to use FOR function because I just want to know the last (newest order information only).

 var = OrdersHistoryTotal() -1 ;
      OrderSelect(var, SELECT_BY_POS, MODE_HISTORY);
      close_profit=OrderClosePrice()-OrderOpenPrice();//in case of BUY order

It this true?

 
No, you will need to loop through all history orders and check for the one with the latest OrderCloseTime() and get its profit.
 
Keith Watford:
No, you will need to loop through all history orders and check for the one with the latest OrderCloseTime() and get its profit.

Oh, really? Thanks. Need to use loos.

Do I need to use OrderCloseTime? How can I know the newest one?

OrderSelect(var *****
This var number's biggest one is the newest closed order?? Not true?
 
kajironpu:

Oh, really? Thanks. Need to use loos.

Do I need to use OrderCloseTime? How can I know the newest one?

This var number's biggest one is the newest closed order?? Not true?

the one with the latest (largest) OrderCloseTime() 

 

This function give you the last profit/loss. Optionally you can filter by a Magic Number and/or a Symbol.

double LastProfit(const int magicNumber=EMPTY,const string symbol=NULL)
  {
   datetime lastTradeClose=0;
   double lastProfit=0.0;
   int count=OrdersHistoryTotal();
   for(int i=0;i<count;i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if((magicNumber==EMPTY || magicNumber==OrderMagicNumber()) &&
         (symbol==NULL || symbol=="" || StringCompare(symbol,OrderSymbol(),false)==0) &&
         OrderType()<OP_BUYLIMIT &&
         OrderCloseTime()>lastTradeClose)
        {
         lastTradeClose=OrderCloseTime();
         lastProfit=OrderProfit()+OrderCommission()+OrderSwap();
        }
     }
   return(lastProfit);
  }
Reason: