//+------------------------------------------------------------------+
//|                                              Volatility Bars.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Shaun Overton, www.onestepremoved.com"
#property link      "www.onestepremoved.com"

#property  indicator_chart_window
#property  indicator_buffers 1
#property indicator_color1 Red


extern   int      Lookback          =  300;
extern   double   Barrier           =  2.0;

double vols[];

int init()  {
   
   IndicatorDigits(Digits);
   IndicatorShortName("Volatility MA");
   
   SetIndexBuffer(0,vols);
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 1, Red);
   SetIndexLabel(0,"Vols");   
         
   return(0);
  }

int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    remaining_bars= Bars - IndicatorCounted() - 1;
   
   if( Bars - 1 < Lookback ) {
      Comment("There are not enough bars on the chart");
      return( 0 );
   }
   
   int consecutiveBars = 1;
   
   double sum = 0;
   double average, temp, divisor;
   double variance = 0;
   
   string show = "";
   
   double thisSDev;
   
   for( int i = remaining_bars; i >= 0; i--) {
     
      sum = 0;
      
      for( int j = i; j < i + Lookback; j++ ) 
         sum += Close[j]-Close[j+1];         
      
      if( Lookback != 0 )
         average = sum / Lookback;         
        
      for( int k = i; k < i + Lookback; k++) {
         
         if( Close[k] != 0 )
            temp = (Close[k]-Close[k+1])/Close[k];
         else
            temp = 0;
            
         variance += MathPow(temp-average,2);
      }
      
      
      divisor = Lookback-1;
      variance = variance / divisor;
       
      double sdev = MathSqrt(variance);
      
      if( sdev != 0 )      
         thisSDev = ( (Close[i]-Close[i+1]) / Close[i] ) / sdev;
      else
         sdev = 0;
      
      if( MathAbs(thisSDev) >= Barrier ) {         
         consecutiveBars = 1;
         vols[i] = iMA(Symbol(),Period(),consecutiveBars,0,MODE_SMA, MODE_CLOSE, i); 
         //Print(StringConcatenate(DoubleToStr(thisSDev,2), " deviations, resetting consecutive bars. Close of last bar is ", DoubleToStr( vols[i], Digits)));
      }
      else {
         consecutiveBars++;
         vols[i] = iMA(Symbol(),Period(),consecutiveBars,0,MODE_SMA, MODE_CLOSE, i);
         //Print(StringConcatenate(DoubleToStr(thisSDev,2), " deviations at ", TimeToStr( Time[i], TIME_MINUTES), ", consecutive bars = ", consecutiveBars));         
      }            
   }   
   
   return(0);
  }
//+------------------------------------------------------------------+