How to index order count by SELECT_BY_POS

 

Dear Guys

I just want to know how to SELECT_BY_POS return index. I mean how to open order indexing. is older order to new order. when I open 1st order then order index = 0. then I open 2nd order then that index=1 or 2nd index = 0 and 1st order =1.

 
dralialadin:

Dear Guys

I just want to know how to SELECT_BY_POS return index. I mean how to open order indexing. is older order to new order. when I open 1st order then order index = 0. then I open 2nd order then that index=1 or 2nd index = 0 and 1st order =1.

If you want to be certain you have the oldest order check them all and find the one with the smallest/oldest OrderOpenTime()
 

no I just want to know how indexing start oldest to new order or new to oldest.

I mean 1st order = 0, 2nd order = 1, 3rd order = 2. this way or 1st order = 3, 2nd order = 2, 3rd order = 0.

 
dralialadin:

no I just want to know how indexing start oldest to new order or new to oldest.

I mean 1st order = 0, 2nd order = 1, 3rd order = 2. this way or 1st order = 3, 2nd order = 2, 3rd order = 0.

From what I have read it should be that 0 is the oldest and n is the newest . . . but from what I have also read it isn't guaranteed . . . so if you want the oldest or newest or them sorted then to be certain you should check.
 
dralialadin:

no I just want to know how indexing start oldest to new order or new to oldest.

I mean 1st order = 0, 2nd order = 1, 3rd order = 2. this way or 1st order = 3, 2nd order = 2, 3rd order = 0.


From my experience OrdersHistoryTotal select by position, 0 will return the lowest ticket number, which will usually be the oldest order.

However, if there are triggered pending orders in history, they will be positioned according to their ticket number, not the time that they were triggered.

EDIT: I wrote this back to front originally

 
RaptorUK:
. . . but from what I have also read it isn't guaranteed . . . so if you want the oldest or newest or them sorted then to be certain you should check.

There does seem to be some discussion in the past regarding whether (and how) the order pools are sorted. See OrderSelect() and ticket behavior - ascending/descending? and WHRoeder's code comments.

I haven't actually checked whether the order pools are sorted and how . . . I just assumed they weren't sorted (or weren't reliably sorted) and created a routine to sort them the way I wanted.

 
Thirteen:

There does seem to be some discussion in the past regarding whether (and how) the order pools are sorted. See OrderSelect() and ticket behavior - ascending/descending? and WHRoeder's code comments.

I haven't actually checked whether the order pools are sorted and how . . . I just assumed they weren't sorted (or weren't reliably sorted) and created a routine to sort them the way I wanted.

Exactly . . . better to be safe than sorry.
 

The default sort for the following Pool Position.

  • MODE_TRADES =OrderOpenTime
  • MODE_HISTORY =OrderCloseTime

I've never been able to reproduce the random sort error.

Anyways, if its critical then, produce your own sort.

 
ubzen:

The default sort for the following Pool Position.

  • MODE_TRADES =OrderOpenTime
  • MODE_HISTORY =OrderCloseTime

I've never been able to reproduce the random sort error.

Anyways, if its critical then, produce your own sort.


I put this code in a script

datetime ticket, lastticket;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
   OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
   ticket=OrderCloseTime();
   if(ticket>lastticket)
      Print("Index number=",i,", Ticket number=",OrderTicket(),", Order Close Time is Later");
   if(ticket<lastticket)
      Print("Index number=",i,", Ticket number=",OrderTicket(),", Order Close Time is Earlier");
   lastticket=ticket;
   }

and the result showed that MODE_HISTORY is not ordered by close time

As already said, best to sort it and be safe.

 
The order in history is unknown by test. See Order History sort by closing date - MQL4 forum
Reason: