Need only 1 pending order sent when price rouch or break S/R

 

Hello, I am trying to create an EA where the condition is :

At the beginning, price is between support and resistant line.

if price break resistant, then create only 1 Sell stop pending order price at middle of S/R.
but if price back to between S and R price, and going up and break the resistant again, it make 2nd Sell stop again (I don't want the 2nd sell stop is created before the pending order is executed).

How to do this ?

void OnTick()
  {
//---   
      
   PriceAsk = MarketInfo(Symbol(), MODE_ASK);
   PriceBid = MarketInfo(Symbol(), MODE_BID);
   double MidSNR = S1 + ((R1-S1) / 2);
   
   if (PriceAsk >= R1) {    // Price start touching R1
      int ticket=OrderSend(Symbol(),OP_SELLLIMIT,Lots,MidSNR,3,R1,S1,"My Order",1889,0,clrGreen); 
      if(ticket<0) 
        { 
         Print("OrderSend failed with error #",GetLastError()); 
        } 
      else 
         Print("OrderSend placed successfully");    
      }   
   if (PriceBid <= S1) {    // Price start touching S1
      int ticket=OrderSend(Symbol(),OP_BUYLIMIT,Lots,MidSNR,3,S1,R1,"Order",1889,0,clrGreen); 
      if(ticket<0) 
        { 
         Print("OrderSend failed with error #",GetLastError()); 
        } 
      else 
         Print("OrderSend placed successfully");    
      }   
   
   
   // Showing a message box with the values.
   MessageBox("Bid = " + DoubleToString(PriceBid, Digits) + " Ask = " + DoubleToString(PriceAsk, Digits));
  }

Maybe this picture explain more ;


Files: