Can't get this code to delete a pending order..

 

Hi All, 

Been trying to get this code to delete a pending order. Basically I'm trying to set two pending orders, one buystop and one sellstop, and when one executes i want to close the other one. Trying to do it by naming them ticket1 and ticket2 as below but its not deleting any:

      //if entry signal below is found raise ticket1 and ticket2, when one of those orders execute delete the other..
      if(body<maxsize
      && Open[3]<Low[2]
      //&& Open[2]>Close[2]
      && Open[2]-Low[2]<maxsize
      && High[2]>High[3]
      && High[2]>High[4]
      && Close[1]<Low[2]
      && High[1]<High[2])      
      {
      ticket1 = OrderOpenF(Symbol(),OP_SELLSTOP,selllot,sellPrice,slippage,sellSL,sellTP,NULL,magic,_ExpDate,Blue);
      ticket2 = OrderOpenF(Symbol(),OP_BUYSTOP,selllot,CountersellPrice,slippage,CountersellSL,CountersellTP,NULL,Countermagic,_ExpDate,Blue);
      }
     
  for(int i=0; i<OrdersTotal(); i++)
     {   
  OrderSelect(ticket1,SELECT_BY_TICKET);
   if(OrderType()==OP_SELL)
     {
       // delete pending order
       OrderDelete(ticket2);
     }
     
  OrderSelect(ticket2,SELECT_BY_TICKET);
   if(OrderType()==OP_BUY)
     {
       // delete pending order
       OrderDelete(ticket1);
     } 
     }

Can anyone spot something wrong or suggest a better/easier way of doing this?


Thanks for any help it would be much appreciated!!!

Jowin

 

For sure you can't delete OP_SELL or OP_BUY orders which are not pending orders.

 
Alain Verleyen:

For sure you can't delete OP_SELL or OP_BUY orders which are not pending orders.


oh I thought I was telling the EA that if ticket1 is now a Sell order rather than a sellstop then delete the buystop. Is that not the case?

 
Jowin6789:

oh I thought I was telling the EA that if ticket1 is now a Sell order rather than a sellstop then delete the buystop. Is that not the case?

Ah sorry, read too fast. Yes this logic should work however your code has a lot of issues.

  1. You don't need a loop if you select by ticket.
  2. You need to check the returned value by the functions (OrderSelect(), OrderDelete())
  3. Are you sure your variable ticket1/ticket2 are correct ?
  4. If the code is like that (OrderChecking/Deleting) just after placing an order, your pending will never be triggered.

Print the values, debug your code to find what is happening, what error code is returned.

 


Change this line

  OrderSelect(ticket1,SELECT_BY_TICKET);


to


if(OrderSelect(i,SELECT_POS)==true)

{

..delete ticket


}

Reason: