How to delete pending order if a trade is opened.

 

Hi guys,

I need some help with his code.

EA opens 2 pending order at a specific time. If one of it is triggered the other one closes.

   if(Hour()==StartHour)//MinPipLimit*MyPoint) //v3.0

     {

      ticket=MarketOrderSend(Symbol(),OP_BUYSTOP,Lots,ND(Ask+(MinPipLimit*MyPoint)),10*int(MyPoint/Point()),ND(Bid-StopLoss*MyPoint),ND(Bid+TakeProfit*MyPoint),"Set by SimpleSystem",Magic);

      ticket=MarketOrderSend(Symbol(),OP_SELLSTOP,Lots,ND(Bid-(MinPipLimit*MyPoint)),10*int(MyPoint/Point()),ND(Ask+StopLoss*MyPoint),ND(Ask-TakeProfit*MyPoint),"Set by SimpleSystem",Magic);

      for(int i=0;i<OrdersTotal(); i++)

        {

         if(OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

            if(OrderType()==OP_BUYSTOP && OrderType()==OP_SELLSTOP)

                 {

                  ticket=OrderTicket();

                  if(!OrderDelete(ticket))

                     Print(GetLastError());

                  else

                     i--;

                 }

           }       

     }

  }

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 

this

 if(OrderType()==OP_BUYSTOP && OrderType()==OP_SELLSTOP)

to this

 if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
 
 for(int i=0;i<OrdersTotal(); i++)

        {

         if(OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))

i is an index number, not a ticket number, so you cannot SELECT_BY_TICKET.

NoWhere in your code do you check that the pending order is triggered before attempting to delete it.

 
Sergey Golubev:

Thanks for the correction.

Reason: