Trying to select ticket number from history??

 
int i;  
                                           
for (i = 5; i >= 0 ; i--)
        {
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

My problem here is; I have for example 12 tickets in my history, i want to select the most recently closed 5 tickects and read there tickect numbers. But my code here is giving me the oldest closed 5 tickets numbers. I need the 5 most recently closed! What am i doing wrong, i tried a few differnt ways but get the same result.

thanks.

 

Thats because if you have 12 orders the oldest ones will be numbers 1-5 you need the recent ones: ( i=12; i>8; i-- ) or better still use OrdersHistoryTotal() something like this:

int total = OrdersHistoryTotal();
   for(int i=total; i>total-5; i--)
    {if((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
     && (OrderSymbol() == Symbol()))
      {print"ticket ",i," = ",OrderTicket();
    } }
thats untested if that doesnt work correctly it might be that the History pool is not in the correct order, I have read somewhere on this forum the History Pool is not guarenteed to be in sequence but when I tested for that I could never find a case where it wasnt7
 
SDC:

Thats because if you have 12 orders the oldest ones will be numbers 1-5 you need the recent ones: ( i=12; i>8; i-- ) or better still use OrdersHistoryTotal() something like this:

thats untested if that doesnt work correctly it might be that the History pool is not in the correct order, I have read somewhere on this forum the History Pool is not guarenteed to be in sequence but when I tested for that I could never find a case where it wasnt

Ok, but i need it to select the most recent 5 tickets all the time, no mater if there are 12 in total or 20 in total.??
 
the code I posted should do that because it uses OrdersHistoryTotal() to find the total number of orders and then total-5 in the loop to get the last 5
 
    for(int iPos=0; iPos < OrdersHistoryTotal(); iPos++) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderCloseTime()    > lastClosePrev             // not yet processed,
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
    ){
        lastClose = OrderCloseTime();
        :
        break; // Last only
    }
 

Reverse your loop direction:

for (int i = 0; i<5 ; i++) <------------
        {
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

HTH.

Reason: