Howto retreive the details of the last open order

 

Hi Everybody,

I am trying to retrieve the details of the last open order (i.e. not pending) , but the code below does not seem to work.

Any hints?

Thanks

MG


for (int Counter=0; Counter <= OrdersTotal()-1; Counter++)
   {
      OrderSelect(Counter, SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==0||OrderSymbol()==1) break; 
   } 
   
   string LastLiveOrd=OrderComment();
   datetime LastDateLive=OrderOpenTime();
 
 if(OrderSymbol()==0||OrderSymbol()==1) break; 

??????

OrderSymbol() is a string.

 

DOHHHHHH


Thanks Keith, it must be the quarantine;)

 

I have tried this and unfortunately it still does not work 100%

It returns me the GBPCAD order highlighted when I am expecting the last USDCHF


Any reasons?




for (int Counter=OrdersTotal()-1; Counter >= 0; Counter--)
   {
      OrderSelect(Counter, SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==0||OrderType()==1) break; 
   } 
   
   string LastLiveOrd=OrderComment();
   datetime LastDateLive=OrderOpenTime();
   
   


 
MacGiamma:

I have tried this and unfortunately it still does not work 100%

It returns me the GBPCAD order highlighted when I am expecting the last USDCHF


Any reasons?





When you return the list of orders, there's no guarantee of chronological order, so you need to confirm it yourself?

Something like this...

 datetime lastorder=0;
 for(int i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
     if(OrderOpenTime()>lastorder)
      lastorder=OrderOpenTime();
 

It works like a charm!!!

Tx Andrew

 
MacGiamma: It works like a charm!!!
Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
          PositionClose is not working - MQL5 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
Reason: