Open pending orders!

 
for(int i=OrdersTotal()-1; i>=0; i--) {
      if(OrderSelect(2,SELECT_BY_POS,MODE_TRADES)) {
         if(OrderType() ==OP_SELL){ 
            OrderSend (_Symbol,OP_BUYSTOP,0.02,Bid+50*_Point,1,0,0,NULL,0,0,Green);
         }
         if(OrderType()==OP_BUY){
            OrderSend(_Symbol,OP_SELLSTOP,0.02,Bid-50*_Point,1,0,0,NULL,0,0,Red); 
         }
      }    
   }

Hi everyone,

I am trying to create simple EA that opens a buy limit when a sell order or buy order is triggered. But that EA opens thousands of buy stop orders at same time.

Can anyone help me to fix the code to open just one stop order.

Thanks in advance.

 
  1. You are selecting the third order every time.
  2. Go through the loop and count open and pending orders. Only after, can you decide if you need to open a new pending.
 
William Roeder #:
  1. You are selecting the third order every time.
  2. Go through the loop and count open and pending orders. Only after, can you decide if you need to open a new pending.

Thank you very much for your help.

Can you please making the loop to count open and pending orders?

 
Gonçalo Marques #:

Thank you very much for your help.

Can you please making the loop to count open and pending orders?

To count pending orders if ( OrderType() > 1) 
Reason: