Selecting last order opened - page 2

To add comments, please log in or register
nicholi shen
1655
nicholi shen  
Shui Wang:

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

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.

Don't do it... you'll be sorry... ;) The order pool isn't always ordered (no pun intended). 

Alireza Javadi
16
Alireza Javadi  
I want to find "one to the last" open order that is not pending. I mean I want to know ticket number of "one to the last" buy or sell. I used this codes but it doesn't work. I t returns the last order includes pending orders too! Could you help me?
void OnTick()
{
   Comment("\n\n\n",SecondLastOrderTicket());
   }
   
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int SecondLastOrderTicket()
    {

                datetime SecondTimeMax=0;
                int SecondTicketMax=-5;
                for(int a=0; a<OrdersTotal(); a++)
                    {
                         if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES)==True)
                              {
                                  if(OrderType()<2 && OrderTicket()!=LastOrderTicket())    
                                       {
                                             if(OrderOpenTime()>SecondTimeMax)      
                                                 {
                                                     SecondTimeMax  = OrderOpenTime();
                                                     SecondTicketMax = OrderTicket();
                                                 }
                                             else continue;
                                       }
                                  else continue;
                              } 
                         else continue;
                    }
                return(SecondTicketMax);    
      }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      

int LastOrderTicket()
   {  
    datetime lastOrderTime  = 0;
    int  lastOrderTicket = -4; 
    for(int j = 0 ; j<OrdersTotal() ; j++) 
         {
            if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==True)
                  {
                     if(OrderType()<2)
                        {
                             if(OrderOpenTime()> lastOrderTime)
                                   {
                                       lastOrderTime   = OrderOpenTime();
                                       lastOrderTicket = OrderTicket();
                                   }
                              else     continue;
                         }
                     else   continue;
                  }
             else      continue;
          }
    return(lastOrderTicket);
    }

 
Hector Pacheco
19771
Hector Pacheco  
Alireza Javadi:
I want to find "one to the last" open order that is not pending. I mean I want to know ticket number of "one to the last" buy or sell. I used this codes but it doesn't work. I t returns the last order includes pending orders too! Could you help me?
 


void OnTick()
{
   Comment("\n\n\n",SecondLastOrderTicket());
   }
   
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int SecondLastOrderTicket()
    {

                datetime SecondTimeMax=0;
                int SecondTicketMax=-5;
                int lastOrderTicket = LastOrderTicket();

                for(int a=0; a<OrdersTotal(); a++)
                    {
                         if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES)==True)
                              {
                                  if(OrderType()<2 && OrderTicket()!=lastOrderTicket)    
                                       {
                                             if(OrderOpenTime()>SecondTimeMax)      
                                                 {
                                                     SecondTimeMax  = OrderOpenTime();
                                                     SecondTicketMax = OrderTicket();
                                                 }
                                             else continue;
                                       }
                                  else continue;
                              } 
                         else continue;
                    }
                return(SecondTicketMax);    
      }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      

int LastOrderTicket()
   {  
    datetime lastOrderTime  = 0;
    int  lastOrderTicket = -4; 
    for(int j = 0 ; j<OrdersTotal() ; j++) 
         {
            if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==True)
                  {
                     if(OrderType()<2)
                        {
                             if(OrderOpenTime()> lastOrderTime)
                                   {
                                       lastOrderTime   = OrderOpenTime();
                                       lastOrderTicket = OrderTicket();
                                   }
                              else     continue;
                         }
                     else   continue;
                  }
             else      continue;
          }
    return(lastOrderTicket);
    }
Alireza Javadi
16
Alireza Javadi  
Hector Pacheco:


It works correctly right now!

Thanks a lot Hector!!

Alireza Javadi
16
Alireza Javadi  
Hector Pacheco:


But Hector, I wonder why this is like that.

What is technically difference between our coding?

whroeder1
17980
whroeder1  
Alireza Javadi: What is technically difference between our coding?

In second's select loop, you call LastOrderTicket() and then compare that to OrderTicket. LOT selected a ticket (position zero) which is what will be compared, not the ticket you selected before the call.

I'd simplify buy just returning a list of tickets and you can get any position you want instantly. (Trade pool is sorted, history is not.)

int GetOrderTicket(int& out[]){  
   int count = 0;
   for(int n = OrdersTotal(); n > 0;) if(
      OrderSelect(--n, SELECT_BY_POS)
   && OrderType()        <= OP_SELL
// && OrderMagicNumber() == ...
   && _Symbol            == OrderSymbol()
   ){
      ArrayResize(out, count+1);
      out[count] == OrderTicket;
      ++count;
   }
   return count;
}
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
Matthew Colter
1222
Matthew Colter  
whroeder1:

In second's select loop, you call LastOrderTicket() and then compare that to OrderTicket. LOT selected a ticket (position zero) which is what will be compared, not the ticket you selected before the call.

I'd simplify buy just returning a list of tickets and you can get any position you want instantly. (Trade pool is sorted, history is not.)

Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

Skipping the magic number check just means that you want the program to handle all orders. This is something you would need to do when there are mixed sources for the open orders and you want to close them all, say at some net profit or loss. Also complicating matters is dealing with FIFO rules, you couldn't let multiple experts open and close orders on the same currency pair without taking all of the open orders on that pair into account because inevitably you'll end up trying to close an order that isn't the oldest order for the pair.

12
To add comments, please log in or register