Download MetaTrader 5
To add comments, please log in or register
Register at MQL5.community and leave your comments!
Hamid Raza
23
Hamid Raza 2008.06.14 08:18 

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

Daniel F
363
Daniel F 2008.06.14 12:56  

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);
  }
Hamid Raza
23
Hamid Raza 2008.06.14 18:43  
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

Hamid Raza
23
Hamid Raza 2008.06.14 19:18  
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

Daniel F
363
Daniel F 2008.06.15 20:01  
you're welcome.
whroeder1
13644
whroeder1 2011.08.29 19:18  
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()
Sergey Nevmerzhitsky
106
Sergey Nevmerzhitsky 2013.08.10 17:42  

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);
}
whroeder1
13644
whroeder1 2013.08.10 18:34  
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);
}
ffoorr
985
ffoorr 2014.07.04 13:53  
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
ffoorr
985
ffoorr 2014.07.04 14:14  

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

/
To add comments, please log in or register