Requesting Help in implementing desired trade placing system in EA.

Muso LLC  

Hi, I am coding a basic RSI trading bot that takes trades when the the RSI is reverting back to the mean (RSI middle of 50) after leaving the upper or lower band. So basically if the RSI has gone under 37.5, and then when the RSI is peaked sell of and is going back up towards the mean, I want place a trade when it hits 37.5. The system I have now is using the OnTick function and is placing the trades prematurely, when the trade is first hitting the lower or upper band the trade is place, not when I want which is when it is returning to the  band, which is when the down side or upside move is complete and the minor trend starts reversing. I am trying to code either a delay or some sort of function where the trade is place until at-least the two prior candles where below or above the band level and then when the current candle begins after the band level, to place the trade, instead of when the RSI first hits the band.

Here is a snippet of the code:

void OnTick(){

  int bars = iBars(_Symbol,PERIOD_CURRENT);
   
  if(totalBars != bars){
  totalBars = bars;
   
      double rsi[];
      double atr[];
      
      CopyBuffer(handle,0,1,2,rsi);
      CopyBuffer(handle2,0,1,2,atr);
      
      SlPoints = (atr[0] * 1);
      TpPoints = (atr[0] * 4);
      // Print(SlPoints);
      // Print(TpPoints);
    //-----------------------------------------------------  
      if(rsi[1] > upper_band && rsi[0] < upper_band){
         
         Print("Sell Signal -----------------------------------------------");
         Print("Stop loss is ", SlPoints, " TP is ", TpPoints);
            
         double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         bid = NormalizeDouble(bid,_Digits);
                 
         // double sl = bid + SlPoints *_Point;
         double sl = bid + atr[0]* 1;
         sl = NormalizeDouble(sl,_Digits);
            
         // double tp = bid - TpPoints *_Point;
            
         double tp = bid - atr[0] * 4;
         tp = NormalizeDouble(tp,_Digits);
            
         trade.Sell(Lots,_Symbol,bid,sl,tp);
         
      }else if(rsi[1] < lower_band && rsi[0] > lower_band){

         Print("Buy Signal ++++++++++++++++++++++++++++++++++++++++++++++++");
         Print("Stop loss is ", SlPoints, " TP is ", TpPoints);
            
         double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         ask = NormalizeDouble(ask,_Digits);
            
         // double sl = ask - SlPoints *_Point;
         double sl = ask - atr[0]* 1;
         sl = NormalizeDouble(sl,_Digits);
            
         // double tp = ask + TpPoints *_Point;
         double tp = ask + atr[0] * 4;
         tp = NormalizeDouble(tp,_Digits);
            
         trade.Buy(Lots,_Symbol,ask,sl,tp);
         }
   }
Reason: