Select different order types

 

Hi all I would like to know what function I need to use to select orders.
Example
I have 10 orders: 6 open, and 4 pending.
1 ==> OP_BUY
2 ==> OP_BUY
3 ==> OP_BUY
4 ==> OP_BUY
5 ==> OP_SELL
6 ==> OP_SELL
7 ==> OP_BUYSTOP
8 ==> OP_BUYSTOP
9 ==> OP_SELLSTOP
10 ==> OP_SELLSTOP

My questions are how to select the last OP_BUY, the last OP_SELL, the last OP_BUYSTOP and the last OP_SELLSTOP

T‌hank for your futures replies 

H‌urryc 

 

You should in cycle find the latest one (based on OrderOpenTime()) for each type, and then get its Ticket Number.

‌for each type:

datetime last_date=-1;‌ int last=0;

if(OrderOpenTime()>last_date)
{
               last=OrderMagicNumber();
               last_date=OrderOpenTime();‌

}‌

return last;‌
 
Foed:

You should in cycle find the latest one (based on OrderOpenTime()) for each type, and then get its Ticket Number.

‌for each type:

datetime last_date=-1;‌ int last=0;

if(OrderOpenTime()>last_date)
{
               last=OrderMagicNumber();
               last_date=OrderOpenTime();‌

}‌

return last;‌


Don't forget to OrderSelect() first. 

bool Select_Latest_Order(int order_type)
  {
   datetime last_time = 0;
   int ticket = -1;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      if(OrderSymbol() != _Symbol)      continue; // optional
      if(OrderMagicNumber() != magic)   continue; // optional
      if(OrderType() != order_type)     continue;
      datetime open_time = OrderOpenTime();
      if(open_time > last_time)
        {
         ticket = OrderTicket();
         last_time = open_time;
        }      
     }
   return(OrderSelect(ticket,SELECT_BY_TICKET));  
  }

 

Please use this function:

//+------------------------------------------------------------------+
//| Check Last Trade function                                        |
//+------------------------------------------------------------------+
int CheckLastTrade(int Type)
{
   datetime OpenTime = 0;
   int Ticket = 0;
   for( int cnt = OrdersTotal()-1; cnt >= 0; cnt-- )
      if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))
         if ( OrderType() == Type )
            if ( OrderOpenTime() > OpenTime )
               Ticket = OrderTicket();
   return (Ticket);
}

‌If you want to check the last OP_BUY, you can use :

int LastBuy = CheckLastTrade(OP_BUY);

‌The result is the ticket number of last  buy‌

 
Thank you all for your quick replies 
 

Hi , all i come back again , when i use this code 

int CheckLastTrade()

{

   datetime OpenTime = 0;

   int Ticket = 0;

   for( int cnt = OrdersTotal()-1; cnt >= 0; cnt-- )

      if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))

         if ( OrderType() == OP_SELLSTOP )

            if ( OrderOpenTime() > OpenTime )

               Ticket = OrderTicket();Alert ("Ticket=",Ticket);

   return (Ticket);‌

T‌he  result gives me the ticket n°89565499,whereas I want to have the ticket N°89565547 see more on the pics 

t‌hank for your help 

Files:
LastOrder1.PNG  64 kb
lastOrder2.PNG  15 kb
 
hurryc: T‌he  result gives me the ticket n°89565499,whereas I want to have the ticket N°89565547
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Don't post a link to or attach a image, just insert the image Use the image button
  3.  if ( OrderOpenTime() > OpenTime )
    Where do you update OpenTime so that you find the latest one?
 
Hi,I am learning the trading, and I started in expert advisors programmingThanks for the tips I would apply them.i use this code 
int CheckLastTrade()

{

   datetime OpenTime = 0;

   int Ticket = 0;

   for( int cnt = OrdersTotal()-1; cnt >= 0; cnt-- )

      if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))

         if ( OrderType() == OP_SELLSTOP )

            if ( OrderOpenTime() > OpenTime )

               Ticket = OrderTicket();Alert ("Ticket=",Ticket);

   return (Ticket);‌

my orders 

‌‌‌‌‌

Last Order of

i‌ used Alert to see the résult 

Result

B‌ut i wanted use the ticket number 89565547(the last OP_SELLSTOP) for the part of the  code)  ( i wish that it s clear for you

 
void deletingPendingOrdersSell()
                
 {if (Bid-LastPenedingOrderSellStop<1000*Point){ // the LastPenedingOrderSellStop must be the ticket n°89565547, the number 1000 is only for the tests when the code will be good then i ll change it 
   
   // Iterate orders
        for(int i = OrdersTotal()-1; i >= 0; i--)
        {
                OrderSelect(i, SELECT_BY_POS, MODE_TRADES); Type = OrderType();
                if(OrderSymbol() == Symbol())
                { 
             if(Type == OP_BUYSTOP  || Type == OP_BUYLIMIT )  
             if(Type == OP_SELLLIMIT  || Type == OP_SELLSTOP|| Type == OP_BUYSTOP||Type == OP_BUYLIMIT )
            {
            if(!OrderDelete(OrderTicket()))
               Print(ShortName +" (OrderDelete Error) "+ ErrorDescription(GetLastError()));
         }
      }
   }
        } 
 
hurryc:
Hi,I am learning the trading, and I started in expert advisors programmingThanks for the tips I would apply them.i use this code 

my orders 

‌‌‌‌‌

i‌ used Alert to see the résult 

B‌ut i wanted use the ticket number 89565547(the last OP_SELLSTOP) for the part of the  code)  ( i wish that it s clear for you


How do you open each OP_SELLSTOP order? different magic number?
better to assign different magic number, as it is easier to select from order pool.

void DeletePendingOrders(int magic, int type)
{
   for(int order = 0; order <= OrdersTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS);
      
      if(select && OrderMagicNumber()==magic && OrderType()==type)
      {
         bool Deleted = OrderDelete(OrderTicket(),clrRed);
         if(Deleted)
         {
            order--;
            Print("Order # ", OrderTicket(), " deleted");
         }
      }
   }
}

 
hurrycI would apply them.i use this code
  1. Why did you post the same broken CheckLastTrade? We asked you to edit your previous post with SRC.
  2. I asked you a question that should have lead you to the problem and solution, but you ignored it. (#6/#2)
 
hi Mohamad Zulhairi Baba

 thank for your reply , as i told you im not a programmer , and i try to make my EA , and a good strategy .At this moment work with a magic number is not important as my code is not correct , but later it will be an obligation .your code is good but my probleme is the condition, to be more simple i need to call the procedure  DeletePendingOrders(int magic, int type) only if (Bid-the last OP_SELLSTOP>140*Point)
Reason: