my volatility indicator is repanting

 

I Had an amazing idea for a volatility indicator, then I coded up the indicators but for some reason its coming up as repaint

I'm sure if we could fix this issue we will all have an amazing volatility indicator


(I'm trying to run the indicator on the 5m chart, required_timeframe is period_current and  atr_timeframe of 15m)


#property indicator_separate_window    // Indicator is drawn in the main window
#property indicator_buffers 6       // Number of buffers
#property indicator_color1 Green     // Color of the 1st line
#property indicator_color2 Black      // Color of the 2nd line
#property indicator_color3 Black    // Color of the 2nd line
#property indicator_color4 Blue  
#property indicator_color5 Red
#property indicator_color6 clrNONE

enum BandsType
{
   ATR,
   Stdev
};

extern int candles_count = 14;
extern int atr_value =14;
extern double atr_multiplier = 1;

extern BandsType bandsType;
extern ENUM_TIMEFRAMES atr_timeframe = PERIOD_CURRENT;

//extern double perctange_of_same_direction_in_seq = 0.75;
extern ENUM_TIMEFRAMES required_timeframe = PERIOD_CURRENT;
extern bool displayOutofWaterMarkers = true;
extern bool asBar;


int mydivider;

double Buf_0[],Buf_1[], Buf_2[], Buf_3[], Buf_4[], summary_buff[];             // Declaring arrays (for indicator buffers)
//--------------------------------------------------------------------
int init() // Special function init()
  {
   SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle(0, DRAW_LINE,STYLE_SOLID,2);// Line style
   
   SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);// Line style
   
   SetIndexBuffer(2,Buf_2);         // Assigning an array to a buffer
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);// Line style
   
   SetIndexBuffer(3, Buf_3);    SetIndexStyle(3, DRAW_LINE,STYLE_SOLID,2); // SetIndexArrow(3, 167); 
   SetIndexBuffer(4, Buf_4);    SetIndexStyle(4, DRAW_LINE,STYLE_SOLID,2); // SetIndexArrow(4, 167); 
   
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,1);
   
   SetIndexBuffer(5, summary_buff);

   if (asBar)
   {
      IndicatorSetDouble(INDICATOR_MINIMUM, 0);
      IndicatorSetDouble(INDICATOR_MAXIMUM, 1);
      
      SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
      SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,1);
   }
      
   
   return 0;                          // Exit the special funct. init()
  }
  
  
  
//--------------------------------------------------------------------
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{ 
   int counted_bars = IndicatorCounted();
   
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   
   int limit = Bars - counted_bars-1;   
      
   for (int i=limit ; i>=0 ; i--)
   {
      datetime dt = Time[i];
      
      int shiftAtRequiredTimeframe = iBarShift(NULL, required_timeframe, dt, true); 
      int shiftForEnd = shiftAtRequiredTimeframe + candles_count;
      
      int shiftForAtr = iBarShift(NULL, atr_timeframe, dt, true);
      
      double closeCurrent = iClose(NULL, required_timeframe, shiftAtRequiredTimeframe);
      double closeOfPrevCandles = iClose(NULL, required_timeframe, shiftForEnd);
       
      double value = (closeCurrent - closeOfPrevCandles) / Point() / 10;
      
      double atrValue;
      
      if (bandsType == ATR)
      {
         atrValue = iATR(Symbol(), atr_timeframe, atr_value, shiftForAtr) * atr_multiplier / Point() / 10;
      }
      else if (bandsType == Stdev)
      {
         atrValue = iStdDev(NULL,atr_timeframe, atr_value,0,MODE_EMA,PRICE_CLOSE, shiftForAtr) * atr_multiplier / Point() / 10;
      }
      
      
      // Display
      if (!asBar)
      {
         Buf_0[i] = value;
         Buf_1[i] = atrValue;
         Buf_2[i] = -atrValue;
         summary_buff[i] = 0;
         
         if (displayOutofWaterMarkers)
         {
            if (value > atrValue)
            {
               Buf_3[i] = value;
               summary_buff[i] = 1;
            }
            else if (value < -atrValue)
            {
               Buf_4[i] = value;
               summary_buff[i] = -1;
            }
            else
            {
            }
         }         
      }
      else
      {
         if (value > atrValue)
         {
            Buf_3[i] = 1;
            summary_buff[i] = 1;
         }
         else if (value < -atrValue)
         {
            Buf_4[i] = 1;
            summary_buff[i] = -1;
         }
      }
   }
   
   return(rates_total);
  }
Reason: