//+------------------------------------------------------------------+
//|                                              Fozzy Indicator.mq4 |
//|                                                             M256 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "M256"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

extern int       RSIPeriod=8;
extern int       BollPeriod=20;
extern int       MAPeriod=8;

extern int       bRequireBollingerCross = 1;

extern int       SignalWidth = 2;

//---- buffers
double LongBuffer[];
double ShortBuffer[];

int init()
{
   SetIndexStyle(0,DRAW_ARROW, EMPTY, SignalWidth);
   SetIndexArrow(0,221);
   SetIndexBuffer(0,LongBuffer);
   SetIndexEmptyValue(0,0.0);
   
   SetIndexStyle(1,DRAW_ARROW, EMPTY, SignalWidth);
   SetIndexArrow(1,222);
   SetIndexBuffer(1,ShortBuffer);
   SetIndexEmptyValue(1,0.0);

   return(0);
}

int start()
{
   int    counted_bars=IndicatorCounted();
   int    Pos = Bars;
   
   for(int i = 0; i < Pos; i++)
   {
      //Calculations of indicators
      //--------------------------
      double RSI[3]; //The RSI
      double BollMiddle[3]; //Middle bollinger band
      double RSIMa[3]; //Moving average of RSI
      
      RSIMa[0] = MAofRSI(Symbol(), 0, MAPeriod, i);
      RSIMa[1] = MAofRSI(Symbol(), 0, MAPeriod, i+1);
      RSIMa[2] = MAofRSI(Symbol(), 0, MAPeriod, i+2);
   
      BollMiddle[0] = MAofRSI(Symbol(), 0, BollPeriod, i);
      BollMiddle[1] = MAofRSI(Symbol(), 0, BollPeriod, i+1);
      BollMiddle[2] = MAofRSI(Symbol(), 0, BollPeriod, i+2);
   
      RSI[0] = iRSI(Symbol(), 0, RSIPeriod, PRICE_CLOSE, i);
      RSI[1] = iRSI(Symbol(), 0, RSIPeriod, PRICE_CLOSE, i+1);
      RSI[2] = iRSI(Symbol(), 0, RSIPeriod, PRICE_CLOSE, i+2);
      //--------------------------
      
      //Plot long entries
      //-----------------      
      if(RSI[1] < RSIMa[1] && RSI[0] > RSIMa[0] && //If the RSI crossed its MA
         RSI[1] < BollMiddle[1] && RSIMa[1] < BollMiddle[1] && //And the cross occured below the middle band
         (bRequireBollingerCross == 0 || RSI[0] > BollMiddle[0])) //And the RSI crossed the middle band
         {
            LongBuffer[i] = Low[i] - 20 * Point;
         }
      //-----------------


      //Plot short entries
      //------------------
      if(RSI[1] > RSIMa[1] && RSI[0] < RSIMa[0] && //If the RSI crossed its MA
         RSI[1] > BollMiddle[1] && RSIMa[1] > BollMiddle[1] &&  //And the cross occured below the middle band
         (bRequireBollingerCross == 0 || RSI[0] < BollMiddle[0])) //And the RSI crossed the middle band
         {
            ShortBuffer[i] = High[i] + 20 * Point;
         }
      //------------------
   }
   
   return(0);
}



double MAofRSI(string symbol, int timeframe, int period, int shift)
{
   double total = 0;

   for(int i = 0; i < period; i++)
      total += iRSI(Symbol(), timeframe, period, PRICE_CLOSE, i + shift);

   return(total/period);
}