Why doesn't this have consistant output?

 
#property copyright "none"
#property link      "https://www.eof.com"
#property version   "1.00"
#property description   "r"
#property indicator_separate_window
#property indicator_buffers   1
#property indicator_plots   1
#property indicator_color1  Blue
#property indicator_type1   DRAW_LINE

input int rPeriod=6;
double rBuffer[];

int OnInit()
  {

   SetIndexBuffer(0,rBuffer,INDICATOR_DATA);
  
   PlotIndexSetString(0,PLOT_LABEL,"r");
   IndicatorSetString(INDICATOR_SHORTNAME,"r");
  
   return(INIT_SUCCEEDED);
  }
 
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[])
    {
        if ( rates_total <= rPeriod ) return (0);
        if (prev_calculated < 0) return (0);
       
        for(int i=rates_total; i>2 && !IsStopped(); i--)
            {
            rBuffer[i] = MathAbs(high[i]-high[i-2])-MathAbs(low[i]-low[i-2]);
            }
       
        return(rates_total);
    }


This indicator is clearly not finished, but it should have an output that looks like one.  I'm wondering why it doesn't?  Sometimes it shows the correct output, but then sometimes it doesn't.  If I right click > refresh, the indicator disappears from the indicator window.


Thanks.

 
antiseptic:


This indicator is clearly not finished, but it should have an output that looks like one.  I'm wondering why it doesn't?  Sometimes it shows the correct output, but then sometimes it doesn't.  If I right click > refresh, the indicator disappears from the indicator window.


Thanks.

Change this :


        for(int i=rates_total; i>2 && !IsStopped(); i--)
            {
            rBuffer[i] = MathAbs(high[i]-high[i-2])-MathAbs(low[i]-low[i-2]);
            }

to this

        for(int i=0; i<rates_total && !IsStopped(); i++)
            {
            rBuffer[i] = (i>1) ? MathAbs(high[i]-high[i-2])-MathAbs(low[i]-low[i-2]) : EMPTY_VALUE;
            }
Or optimize it to calculate only the changed bars
 

OIC, it couldn't calculate bars that didn't exist.

Thanks again.