Paul Wakhungu
Paul Wakhungu

Reputation: 332

How are MQL4 Orders ordered by default?

When using OrderSelect() in mql4, are the orders ordered according to the ticket number by default? My intention is to use OrderModify() on orders starting from the first that was opened to the most recent.

Upvotes: 2

Views: 2262

Answers (3)

Oliver
Oliver

Reputation: 76

`enter code here`
int Magic = 12345;
// This reads the orders LIFO (last in...)
// the index is an integer and is incremented from 0 
// as the orders are placed. It is NOT 
// the ticket number. No sorting necessary.
for(int i=OrdersTotal()-1;i>=0;i--) 
    if(_Symbol=OrderSymbol() && Magic = OrderMagicNumber())
        if(OrderSelect(i, SELECT_BY_POS) {
            Print("Order Index: ",i,", Ticket: ",OrderTicket());
   

Upvotes: 2

nicholishen
nicholishen

Reputation: 3012

Never assume anything in MQL unless it's explicitly specified in the documentation. That said, you'll need to sort your ticket numbers before iterating them in order.

   CArrayInt tickets;
   for(int i=0; OrderSelect(i, SELECT_BY_POS); i++)
      tickets.Add(OrderTicket());
   tickets.Sort();
   for(int i=0; i<tickets.Total(); i++)
      if(OrderSelect(tickets[i], SELECT_BY_TICKET))
         ...

Upvotes: 2

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

You cannot call OrderSelect() function without parameters. You must specify id and the way the orders are selected. If you know an ID of the order, as it is seen it MT4 terminal window, you can call OrderSelect( order_id, SELECT_BY_TICKET), if you dont know or in case you loop over history trades you have to apply OrderSelect(i,SELECT_BY_POS) or OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) where i is integer between 0 and OrdersTotal() or OrdersHistoryTotal() respectfully. If you loop over the array of trades with i as an integer, you are strongly advised to loop from the maximum value to zero (and not vice versa), and you can obtain ticket id by calling OrderTicket() function after OrderSelect(*,*[,MODE_HISTORY]) is successful.

Upvotes: 0

Related Questions