Why does my OCO give me an error invalid Ticket for order delete function

 
 //Code to Delete complete OCO
       if( (OCOset == true)&&(OrdersTotal() > 0) )
       {
         OrderSelect(OTicket1, SELECT_BY_TICKET);
         
          if(OrderCloseTime() == 0 && OrderType() == OP_BUY)
          {
           bool Deleted = OrderDelete(OTicket1, Green );
          }
          
          OrderSelect(OTicket2, SELECT_BY_TICKET);
         
          if(OrderCloseTime() == 0 && OrderType() == OP_SELL)
          {
            Deleted = OrderDelete(OTicket2, Red );
          }
       }
 
Read the documentation . . what does OrderDelete do ?
 
 if(OrderCloseTime() == 0 && OrderType() == OP_BUY) <----- OP_BUY? OP_SELL? try ordertype()>1 instead.
 
I place a BUYSTOP order and SELLSTOP order. When one of them turns into a market order, I want to check if it is a OP_BUY or OP_SELL and then delete the other pending order.
 

This is what your code does . . .

OrderSelect(OTicket2, SELECT_BY_TICKET);

This selects OTicket2 for further processing . . . ( https://docs.mql4.com/trading/OrderSelect )

if(OrderCloseTime() == 0 && OrderType() == OP_SELL)

This says . . . if OTicket2 is still open and type OP_SELL then . . .

Deleted = OrderDelete(OTicket2, Red );

You CANNOT Delete an Open order . . .

 

if that is what you want to achieve, maybe you like to consider this simpler ways


bool  isExist_PO1=OrderSelect(ticket1,SELECT_BY_TICKET),
      isExist_PO2=OrderSelect(ticket2,SELECT_BY_TICKET);
 
   if(isExist_PO1 && !isExist_PO2)OrderDelete(ticket1); 
   if(!isExist_PO1 && isExist_PO2)OrderDelete(ticket2);...something like this. 
 
diostar:

if that is what you want to achieve, maybe you like to consider this simpler ways




what this work

//Code to Delete complete OCO
       if( (OCOset == true)&&(OrdersTotal() > 0) )
       {
         OrderSelect(OTicket1, SELECT_BY_TICKET);
         
          if(OrderCloseTime() == 0 && OrderType() == OP_BUY)
          {
           bool Deleted = OrderDelete(OTicket2, Green );
          }
          
          OrderSelect(OTicket2, SELECT_BY_TICKET);
         
          if(OrderCloseTime() == 0 && OrderType() == OP_SELL)
          {
            Deleted = OrderDelete(OTicket1, Red );
          }
       }


I just switched Oticket1 & Oticket2 this way if Oticket1 becomes a market order from pending order, it will delete the pending order Oticket2 and vice versa
 
jeemba2012:

what this work

not a chance it will work properly. see below:

//Code to Delete complete OCO
       if( (OCOset == true)&&(OrdersTotal() > 0) )
       {
         OrderSelect(OTicket1, SELECT_BY_TICKET); <---------- can be true or false (a)
         
          if(OrderCloseTime() == 0 && OrderType() == OP_BUY) <------- from (a) if false, compiler complains orderselect err, prg cease to run.
          {
           bool Deleted = OrderDelete(OTicket2, Green );  
          }
          
          OrderSelect(OTicket2, SELECT_BY_TICKET);
         
          if(OrderCloseTime() == 0 && OrderType() == OP_SELL)
          {
            Deleted = OrderDelete(OTicket1, Red );
          }
       }

I just switched Oticket1 & Oticket2 this way if Oticket1 becomes a market order from pending order, it will delete the pending order Oticket2 and vice versa
---------------------->
reply: Like I mention, I get your point, your objective looks clear. what is not is not being the usual  

-------> if(OrderSelect(OTicket1, SELECT_BY_TICKET)){ .....then only proceed.
 
 
 
jeemba2012:
I place a BUYSTOP order and SELLSTOP order. When one of them turns into a market order, I want to check if it is a OP_BUY or OP_SELL and then delete the other pending order.
Can you guarantee that you will only have a maximum of 2 pending orders open at any one time ?