Selecting last order opened

 

Hi

I am a novice in MQL4

How can I select the last order opened ( in terms of orderopentime() ) when there are multiple orders opened.

Any help will be much appreciated.

Thanks

Hamid

 

In spite of programming MQL4 for quite a long time I never needed to figure out which order the orders in the MODE_TRADES list have.

Therefore, I would just iterate over the list of open orders and would compare the OrderOpenTimes. However, this MQL4 uses normal UNIX timestamps you cannot distignuish between orders which have been opened in the same second.


Just from scratch without much testing I wrote this (just as an inspiration)

int lastOpenTime = 0, needleTicket = 0;
   
   for(int i = (OrdersTotal()-1); i >= 0; i --)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      int curOpenTime = OrderOpenTime();
      
      if(curOpenTime > lastOpenTime)
      {
         lastOpenTime = curOpenTime;
         needleTicket = OrderTicket();
      }
   }
   
   Print("result : T#",needleTicket);
   return(0);
  }
 
tradeigel wrote >>

In spite of programming MQL4 for quite a long time I never needed to figure out which order the orders in the MODE_TRADES list have.

Therefore, I would just iterate over the list of open orders and would compare the OrderOpenTimes. However, this MQL4 uses normal UNIX timestamps you cannot distignuish between orders which have been opened in the same second.


Just from scratch without much testing I wrote this (just as an inspiration)

Thank you for your reply and code, I will go through it to gain some more knowledge.

Much appreciated

Hamid

 
tradeigel wrote >>

In spite of programming MQL4 for quite a long time I never needed to figure out which order the orders in the MODE_TRADES list have.

Therefore, I would just iterate over the list of open orders and would compare the OrderOpenTimes. However, this MQL4 uses normal UNIX timestamps you cannot distignuish between orders which have been opened in the same second.


Just from scratch without much testing I wrote this (just as an inspiration)

hamidraza
wrote
>>

Thank you for your reply and code, I will go through it to gain some more knowledge.

Much appreciated

Hamid

It is working very nice

Very many Thanks

Hamid

 
you're welcome.
 
hamidraza:
How can I select the last order opened ( in terms of orderopentime() ) when there are multiple orders opened.
Last opened order? Or do you mean the last order opened by THIS EA on THIS chart?
int LastOpenTicket(){
    datetime lastTime  = 0;
    int      lastTicket = -1; // None open.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderOpenTime()     >  lastTime
    ){
      lastTime   = OrderOpenTime();
      lastTicket = OrderTicket();
    }
    return(lastTicket);
}
Always test return codes, that includes OrderSelect()
 

Last solution is wrong, because you can get not last order, if many orders opened at one second. For select really last order use this code:

int searchLastOpenedMarketTicket(int magic) {
   int i, ordersCount = OrdersTotal();
   datetime lastTime;

   // Search timestamp of last opened orders.
   for (i = ordersCount - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS)) {
         if (OrderMagicNumber() != magic) {
            continue;
         }

         if (OrderOpenTime() > lastTime) {
            lastTime = OrderOpenTime();
         }
      }
   }   

   for (i = ordersCount - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS)) {
         if (OrderMagicNumber() != magic ||
             OrderOpenTime() != lastTime) {
            continue;
         }

         return (OrderTicket());
      }
   }

   return (0);
}
 
That may not work either. It returns the highest positioned order with the latest timestamp. Since ticket numbers are increasing, for multiple orders in one second try:
int LastOpenTicket(){
    datetime lastTime  = 0;
    int      lastTicket = -1; // None open.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderOpenTime()     >=  lastTime
    &&  OrderTicket()       >   lastTicket
    ){
      lastTime   = OrderOpenTime();
      lastTicket = OrderTicket();
    }
    return(lastTicket);
}
 
int LastOpenTicket(){
    datetime lastTime  = 0;
    int      lastTicket = -1; // None open.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderOpenTime()     >  lastTime
    ){
      lastTime   = OrderOpenTime();
      lastTicket = OrderTicket();
    }
    return(lastTicket);
}

I have an issue with OrderTotal()

Does this code mean that the first order in time is order N° 0

And the last order in time is Order N° ( OrderTotal() -1 )  ?

In this case if one do this, the last order would be selected ?

 OrderSelect( ( OrderTotal() -1 ), SELECT_BY_POS) 
WHRoeder
 

I mean can someone select the last order by his position relative to the OrderTotal() ?

 
ffoorr:

I mean can someone select the last order by his position relative to the OrderTotal() ?

I wrote a small script to test the "ordering"(which is selected when using SELECT_BY_POS) of the function "OrderSelect()", and confirmed that 

OrderSelect( ( OrderTotal() -1 ), SELECT_BY_POS) 

selects the "last" order has JUST been opened.

I also noted (surpringly) that ALL my orders (pending or opening) i placed on ALL symbols are in the "trading pool" and can be selected by OrderSelect()...so can you be sure you get the right order when you run the above command?? --- perhaps another order just having been opened...

So perhaps whroeder1's idea is better.

Reason: