Orderselect - by ticket.

Back to topics list To post a new topic, please log in or register
avatar
4
richy562 2015.02.12 02:07
 

Hi all,

Trying to make some code that uses OrderSelect() to get the ticket number of the 2nd last order  that has been opened by my EA on the current pair. (EA has a magic number).

Looked around but everyone is just selecting the last order.

Thanks for any input.


Cheers

Richard


avatar
Moderator
3427
GumRai 2015.02.12 03:43 #
 

I don't know whether this is the simplest way, but it should work. I have not tested it.

//Function to find 2nd last order
 int Find2ndOrder()
    {
    int as=0;
    int ot=OrdersTotal();
    int ticketarray[];
    ArrayResize(ticketarray,as,ot);
    datetime datearray[];
    ArrayResize(datearray,as,ot);
    for(int x=ot-1;x>=0;x--)
       {
       if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
          if(OrderMagicNumber()==MagicNumber)
             {
             as++;
             ArrayResize(ticketarray,as);
             ticketarray[as-1]=OrderTicket();
             ArrayResize(datearray,as);
             datearray[as-1]=OrderOpenTime();
             }
       }
    if(as<2)
       return(0);
    int index=ArrayMaximum(datearray,WHOLE_ARRAY,0);
    datearray[index]=0;
    index=ArrayMaximum(datearray,WHOLE_ARRAY,0);
    return(ticketarray[index]);
    }

 But at least it may give you something to think about. This obviously doesn't work with closed orders, you will need to modify if you need to include closed orders.

avatar
4
richy562 2015.02.12 04:24 #
 

Hi GumRai,


Thanks very much - it appears to be working correctly on backtester. I'll test on demo to be certain.

Your a legend, exactly what I was looking for.


Cheers

Richard

avatar
21
hji 2015.02.12 09:04 #
 
I have been looking for the same,for some time now. Did it work out well for you richy562? 
avatar
4
richy562 2015.02.12 13:40 #
 
Yes it all seems good
avatar
13012
WHRoeder 2015.02.12 14:40 #
 
GumRai: I don't know whether this is the simplest way, but it should work.
Simpler
Not compiled, not tested
//Function to find nTh order
 int Find2ndOrder(int nTh=0){ // 0 last
    int ticketarray[][2];  int count=0;
    for(int x=OrdersTotal()-1;x>=0;x--) if(
       OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
    && OrderMagicNumber()==MagicNumber
    ){
       ArrayResize(ticketarray,count+1);
       ticketarray[count][0] = OrderOpenTime();
       ticketarray[count][1] = OrderTicket();
       ++count;
    }
    if(count<nTh)
       return(0);
    ArraySort(ticketarray, WHOLE_ARRAY, 0, MODE_DESCEND);
    return ticketArray[nTh][1];
}
Not compiled, not tested
See also Could EA Really Live By Order_History Alone? - MQL4 forum
Back to topics list  

To add comments, please log in or register