Getting Last Closed Order

 
total = OrdersHistoryTotal()-1;
if((OrderSelect(total,SELECT_BY_POS,MODE_HISTORY)==true)
&& (OrderSymbol() == Symbol()))
  {Print(" Last Order Ticket = ",OrderTicket());
  }

In previous topics about this, it has been said to get the last closed order it is neccessary to cycle through all the closed orders to find the last one because the OrderSelect() function does not return the orders in the correct sequence by close time. Does anyone have any evidence of this ?

I tested this in strategy tester over hundreds of orders using the code above, and every time an order was closed the code above returned the correct order as the last closed order, so I would really like to know if there really is any evidence to prove this would not always work, because cycling through the whole history to get the last order is time consuming, when it has to be done often it slows tester down considerably so I would choose not to do that if it isnt really neccessary.

 

Yeah, you and I both have been blind followers lol. However some concerns toward it's un-reliability are provided here. I've never put it to the test tho. When I run a back-test, I use codes similar to yours. When I put the finishing touches on for live-real-trading, I use codes similar to here.

Worse case scenario, if a Manual sort of the Trade-Tab and the Account-History-Tabs influences the Sequence of the History-Pool then that'll be the biggest reason for Looping through everything. I've however run your code above on Demo-Testing and Never had a problem. And I'm pretty sure I played around with the sorts on both tabs during those test periods.

 
  1. SDC:
    I tested this in strategy tester over hundreds of orders using the code above, and every time an order was closed the code above returned the correct order as the last closed order, so I would really like to know if there really is any evidence to prove this would not always work,
    total = OrdersHistoryTotal()-1;
    if((OrderSelect(total,SELECT_BY_POS,MODE_HISTORY)==true)
    && (OrderSymbol() == Symbol()))
      {Print(" Last Order Ticket = ",OrderTicket());
      }

    The MOMENT you have multiple charts or manual trading, that code will be not be selecting the EA's order but something else. Likewise if you have deposits and withdrawals

    Since the order of the list is NOT documented, i.e. could change in future versions, the paranoid among us go through all orders.

    If you're not paranoid just find the last order and exit the loop.

        for(int pos=OrdersHistoryTotal(); pos >= 0; pos--) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
        ){
            Print(" Last Order Ticket = ",OrderTicket());
            break;
    }
  2. because cycling through the whole history to get the last order is time consuming, when it has to be done often it slows tester down considerably so I would choose not to do that if it isnt really neccessary.
    Why do you need to do it more than once? Remember the last OrderCount() in a static. If it has changed find the last history and remember what you need.
 

yes I hadnt thought of the manual orders scenario, that would mess things up I guess its back to the loop thanks for replying guys i will implement your suggestion though WHR using a static to check the order count should reduce the frequency of accessing the history also i discovered why it was slowing down the strategy tester so much, in my EA I had the code cycling through the history orders the wrong way, oldest to newest lol

Reason: