I want to Open a Pending order when a trade is opened

 

I want to open a BUYSTOP when a new buy trade is opened and a SELLSTOP when a new sell trade is opened

I tried like this but it keeps opening buy trades over and over even when it sees the old buy trade

double trigger_price = Ask; //-- Modify this variable to your prefered method of detecting new order open price, ie price when your ea trigger new order.

bool   orderopen = 1;

for (int o=OrdersTotal(); o>=0; o--)
{
   
   OrderSelect(o,SELECT_BY_POS,MODE_TRADES);
   
   if (trigger_price == OrderOpenPrice())
   {
      orderopen = 1;
} 

}

if (orderopen == 0 && OrderType() == OP_BUY)
{
      lotsize=OrderLots();
      double price = trigger_price+(Distance*pips);
      if(StopLoss>0){stop_loss = (price - (StopLoss * pips));}else{stop_loss=0;}
      if(TakeProfit>0){take_profit = (price + (TakeProfit * pips));}else{take_profit=0;} 
      
      
      OrderSend(Symbol(),OP_BUYSTOP,(lotsize*2),price,slip,stop_loss,take_profit,NULL,Magic,0,CLR_NONE); //-- Put your OrderSend-function here.
}
 

In this case, you have to count the number of Buy/sell and BuyStop / SellStop orders.

Then use the logic you want the EA to do according to the exist orders. 

Please note that your code is not practically effecient for 2 reasons,

- In case of slippage,  trigger_price will never equal to OrderOpenPrice() !.

- OrderOpenPrice() for Buy and Sell orders are not the same (Ask !) ... So, for sure  you will get 50% of orders have the same as the slippage case.

 
Osama Shaban:

In this case, you have to count the number of Buy/sell and BuyStop / SellStop orders.

Then use the logic you want the EA to do according to the exist orders. 

Please note that your code is not practically effecient for 2 reasons,

- In case of slippage,  trigger_price will never equal to OrderOpenPrice() !.

- OrderOpenPrice() for Buy and Sell orders are not the same (Ask !) ... So, for sure  you will get 50% of orders have the same as the slippage case.

am trying to count buys and sells seperately
Reason: