Odd Custom Indicator Bug

 

The Below Indicator has a bug I've been working on for a few days. When you attach it to a chart, Buf_1[] is not drawn (on any bars below bar 0), but it starts drawing from candle 0 once you attach it to a chart. Interestingly enough, if you attach it to a chart and then compile it the Buf_1 values appear, but not on the initial attachment to the chart (which makes me think its something in init).

You will see below I use the iMAOnArray function and when I try to use a different methodology for calculating Buf_1[] it works fine. Anyway I'm led to believe that it is some combination of init and iMAOnArray and am seeking some help.

I've included comments on where I believe problems may be (I've already tried ArraySetAsSeries with no luck)

Thanks!

#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern int HMA_Period = 20;
extern int shift = 0;
#property indicator_buffers 6
#property indicator_color1 Magenta
#property indicator_color2 SkyBlue

#property indicator_color3 Blue
#property indicator_color4 Blue

#property indicator_color5 Red
#property indicator_color6 Red

#property indicator_width3 2
#property indicator_width4 2

#property indicator_width5 2
#property indicator_width6 2

#property indicator_chart_window

double Buf_0[], Buf_1[], Buf_2[], Buf_3[], Buf_4[], Buf_5[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){

      SetIndexBuffer(0,Buf_0);
      SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
      
      SetIndexBuffer(1,Buf_1);
      SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
      
      SetIndexBuffer(2,Buf_2);
      SetIndexStyle(2, DRAW_HISTOGRAM, DRAW_HISTOGRAM, 2);
      
      SetIndexBuffer(3,Buf_3);
      SetIndexStyle(3, DRAW_HISTOGRAM, DRAW_HISTOGRAM,2);
      
      SetIndexBuffer(4,Buf_4);
      SetIndexStyle(4, DRAW_HISTOGRAM, DRAW_HISTOGRAM,2);
      
      SetIndexBuffer(5,Buf_5);
      SetIndexStyle(5, DRAW_HISTOGRAM, DRAW_HISTOGRAM,2);

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int  counted_bars=IndicatorCounted();
   i = Bars-counted_bars-1;
   
   while(i >= 0){
   
      Buf_0[i] = iMA(Symbol(),0,4,0,MODE_EMA,MODE_CLOSE,i);
      
      Buf_1[i] = iMAOnArray(Buf_0, 0, MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA, i + shift);//*******imaonarray could be contributing to the error
      
      if(Buf_0[i] > Buf_1[i]){
            Buf_2[i] = High[i];
            Buf_3[i] = Low [i]; 
     }
      else{
            Buf_4[i] = Low[i];
            Buf_5[i] = High[i];
      }      
      i--;
   }   
   return(0);
  }
//+------------------------------------------------------------------+

 
Can anyone speculate on this issue?
Reason: