Download MetaTrader 5

Selecting last order opened

To add comments, please log in or register
Hamid Raza
23
Hamid Raza  

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
347
Daniel F  

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  
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  
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
347
Daniel F  
you're welcome.
whroeder1
14779
whroeder1  
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  

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
14779
whroeder1  
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
1047
ffoorr  
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
1047
ffoorr  

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

To add comments, please log in or register