How to delete pending order if a trade is opened.

To add comments, please log in or register
Benjamin Obafemi
267
Benjamin Obafemi  

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--;

                 }

           }       

     }

  }

Sergey Golubev
Moderator
109906
Sergey Golubev  

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

Marco vd Heijden
Moderator
9532
Marco vd Heijden  

this

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

to this

 if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
Keith Watford
Moderator
16251
Keith Watford  
 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.

Benjamin Obafemi
267
Benjamin Obafemi  
Sergey Golubev:

Thanks for the correction.

To add comments, please log in or register