I need to select historical order 1 before the youngest. First order before last one.

 

I need to know if the order before lest one was sell or buy and what was the open price and if there had been a take profit or stop loss used at the end.

 can you help me? 

 
 

Alternative code (because I did it in the meantime). Probably not that good as WHRoeders is!

Print("Orders in history: ", OrdersHistoryTotal());
int ticket,count,i;
int magic=5020060;  
int orderinhistory=2;
datetime orderclosedates[];
//Secure, if Orders are not always ordered in History
for(i=OrdersHistoryTotal()-1,count=1;i>=0;i--){
   if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
     Print("OrderSelect failed. Error: ",GetLastError());
     break;
   }
   Print("magic: ",OrderMagicNumber()," ;OrderType: ",OrderType()); 
   if(magic==OrderMagicNumber()&&Symbol()==OrderSymbol()&&              //only my magic and my symbol
   (OrderType()==OP_BUY||OrderType()==OP_SELL)){                        //no canceled pending orders or 'balance' order
      ArrayResize(orderclosedates,count);
      orderclosedates[count-1]=OrderCloseTime();
      count++;
   }
}
Print("ArraySize (Orders found for symbol ",Symbol()," and magic ",magic,"): ",ArraySize(orderclosedates));
if(ArraySize(orderclosedates)>=orderinhistory){                         
   ArraySort(orderclosedates,WHOLE_ARRAY,0,MODE_DESCEND);
   for(i=OrdersHistoryTotal()-1;i>=0;i--){
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         Print("OrderSelect failed. Error: ",GetLastError());
         break;
      }
      if(OrderCloseTime()==orderclosedates[1]){
         ticket=OrderTicket();
         Print("Orderticket for order ",orderinhistory," in history: ",ticket);
         break;      
      }
   }    
}else{
   Print("Not enough matching orders in history!");
}   
//---------------------------------------------------------------------------------
// BAD CODE BELOW
// 1. Orders must not be sorted in history
// 2. No filter for magic number
// 3. No filter for Symbol
// 4. No filter for order type
// --> Most likely it does return a wrong ticket number! DONT USE IT. 

//Assuming the orders are always ordered
if(OrdersHistoryTotal()>=2){  
   for(i=OrdersHistoryTotal()-1,count=1;i>=0;i--,count++) {
     if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
        Print("OrderSelect failed. Error: ",GetLastError());
        break;
     }
     if(count==2){
        ticket=OrderTicket();
        Print("Orderticket: ",ticket); 
        break;
     }     
   }
}  

 

edit: First example corrected thanks to the advices from WHRoeder. Second example not changed (It is just an example on how you shouldn't do it!)
 
kronin: Alternative code (because I did it in the meantime). Probably not that good as WHRoeders is!
//Assuming the orders are always ordered
The comment in my code points to the post that says history is sometimes out of order. So your if (count==2) will not always work.

Also you are not filtering by magic number and pair, making the Ea incompatible with ever other including itself on other charts and manual trading.

 
WHRoeder:
The comment in my code points to the post that says history is sometimes out of order. So your if (count==2) will not always work.

Also you are not filtering by magic number and pair, making the Ea incompatible with ever other including itself on other charts and manual trading.

Corrected. Thank you.

Reason: