I can't see the problem in my moving average code

 
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

extern int HMA_Period = 21;
extern int History = 500;

double hma[];

int init()
  {
   IndicatorBuffers(1);
   SetIndexBuffer(0, hma);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted(), i = Bars - counted_bars - 1;
   double temp;

   if (i > History - 1)
      i = History - 1;
      
   while ( i >= 1 )
   {
      hma[i] = iMA(NULL,0,MathFloor
      (HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
      iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
      
      i--;
   }
   
   i = Bars - counted_bars - 1;
   
   while ( i >= 1 )
   {
      temp = iMAOnArray(hma,0,MathFloor(MathSqrt
      (HMA_Period)),0,MODE_LWMA,i);
      
      hma[i] = temp;
      i--;
   }
   
   return(0);
  }
  

//+------------------------------------------------------------------+

The problem is that in some bars it shows some strange peaks like that one in the image, and if I'm sure it's a problem in the code because if I use another hull's moving average indicator it doesn't have those peaks. Can anyone see the problem in my code? :S

 

In this image I put my hull moving average's indicator (black line) and another hull moving average indicator with the same period (red line).That peak must come from a problem in the code...

 

 
   i = Bars - counted_bars - 1;
   
   while ( i >= 1 )
   {
      temp = iMAOnArray(hma,0,MathFloor(MathSqrt
      (HMA_Period)),0,MODE_LWMA,i);
      
      hma[i] = temp;
      i--;
   }

You can't do that. The moment you update hma[bars-1] then all other values generated by iMAonArray are bogus.

Add a second buffer.