Limiting OrdersTotal to only allow 1 open order at a time.

 

Hi,


I am new to learning MQL4 coding and I am trying to limit my program to oly allow one open order at a time however in my backtest I am getting multiple orders opened up.   Here is my code.

extern int TakeProfit = 40,
           StopLoss = 40;
extern double lots = 0.10;


void OnTick()
  {
   // create a variable for the signal
   string signal="";

   // symbol,timeframe,for 20 candles,Deviation 2,no shift,close price, candle 1
   double LowerBollingerBand=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
   double UpperBollingerBand=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_UPPER,1);
   
   // symbol,timeframe,for 20 candles,Deviation 2,no shift,close price, candle 2
   double LastLowerBollingerBand=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_LOWER,2);
   double LastUpperBollingerBand=iBands(_Symbol,_Period,20,2,0,PRICE_CLOSE,MODE_UPPER,2);   
   
   //if we have no open positions or orders
      if(OrdersTotal()<1)   
   
     // if close for candle 2 was below lower band
     if ((Close[2]<LastLowerBollingerBand)
     &&
     // if close for candle 1 was above lower band
      (Close[1]>LowerBollingerBand))
      {
          signal="buy";
      }   
     
   
     // if close for candle 2 was above upper band
     if ((Close[2]>LastUpperBollingerBand)
     &&
     // if close for candle 1 was below upper band
     (Close[1]<UpperBollingerBand))
      {
          signal="sell";
      }
     
      if(OrdersTotal()<1 && signal == "buy")
      bool resBuy;
      resBuy = OrderSend(_Symbol, OP_BUY, lots, Ask, 3, StopLoss, Ask+TakeProfit*_Point, NULL, 0, 0, Green);
   
     if(OrdersTotal()<1 && signal == "sell")
      bool resSell;
      resSell = OrderSend(_Symbol, OP_SELL, lots, Bid, 3, StopLoss, Bid-TakeProfit*_Point, NULL, 0, 0, Green);
   
   Comment("The current signal is: ", signal, "\nOrdersTotal is: ", OrdersTotal());   
  }



I have tried OrdersTotal()==0 && signal == "sell" as well as  OrdersTotal()<=1 && signal == "sell" and nothing is working.  Any advice would be great.

Files:
 
allenmclain417: I have tried … and nothing is working.  Any advice would be great.
if(OrdersTotal()<1 && signal == "buy")
      bool resBuy;
resBuy = OrderSend(…

Your OrderSend is not part of the if statement.

if(condition) statement;
//or
if(condition){
  multiple;
  statements;
}