Why does the iMAOnArray give two different results for the last two bars of my code

 

This indicator plots days range, with a 10 day average.

The code that misbehaves

 
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Magenta
#property indicator_color3 Orange
#property indicator_level1 75
#property indicator_level2 120
#property indicator_levelcolor White
extern int AtrPeriod=10;
//---- buffers
double AtrBuffer[];
double AtrBufferSPIKE[];
double AtrBufferAVE[];
double TempBuffer1[];
double TempBuffer2[];
double TempBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   
   IndicatorBuffers(5);
   IndicatorDigits(0);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBufferAVE);
   short_name="Ians_HLR("+ AtrPeriod +") ";
   IndicatorShortName(short_name + " Days Pip Range: Average / Last Day");
   SetIndexLabel(0,"HLR_Ave");
   SetIndexDrawBegin(0,AtrPeriod);
   
   
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,AtrBuffer);
   SetIndexDrawBegin(1,AtrPeriod);
   SetIndexLabel(1,"HLR_Day");
   
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,AtrBufferSPIKE);
   SetIndexDrawBegin(2,AtrPeriod);
   SetIndexLabel(2,"HLR_Spike");
   
   
   SetIndexBuffer(2,TempBuffer1);
   SetIndexBuffer(3,TempBuffer2);
   SetIndexBuffer(4,TempBuffer3);
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   if(Period() != PERIOD_D1)
   {
   Comment(" CAUTION : Ians_HLR only works on Daily Charts !!");
   return(0);
   }
  
   int i,k,counted_bars=IndicatorCounted();
 
   if(Bars<=AtrPeriod) return(0);
 
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) 
      {
         AtrBufferAVE[Bars-i]=0;
         AtrBufferSPIKE[Bars-i]=0;
         AtrBuffer[Bars-i]=0;
      }
 
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=High[i];
      double low =Low[i];
      double range = NormalizeDouble((high-low)/Point,0);
 
      TempBuffer1[i]=range;
      TempBuffer2[i]=range;
      if (range >= 120) TempBuffer3[i]=range; else TempBuffer3[i]=0;
      i--;
     }
 
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   
   for(i=0; i<limit; i++)
   {
      AtrBuffer[i]=TempBuffer2[i];
      AtrBufferSPIKE[i]=TempBuffer3[i];
      AtrBufferAVE[i]=iMAOnArray(TempBuffer1,Bars,AtrPeriod,0,MODE_SMA,i);
   }
   
   return(0);
  }

The code that the average does not give incorrect last two bars on the (lime) average line

 
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Magenta
#property indicator_color3 Orange
#property indicator_level1 75
#property indicator_level2 120
#property indicator_levelcolor White
extern int AtrPeriod=10;
//---- buffers
double AtrBuffer[];
double AtrBufferSPIKE[];
double AtrBufferAVE[];
double TempBuffer1[];
double TempBuffer2[];
double TempBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   
   IndicatorBuffers(5);
   IndicatorDigits(0);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBufferAVE);
   short_name="Ians_HLR("+ AtrPeriod +") ";
   IndicatorShortName(short_name + " Days Pip Range: Average / Last Day");
   SetIndexLabel(0,"HLR_Ave");
   SetIndexDrawBegin(0,AtrPeriod);
   
   
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,AtrBuffer);
   SetIndexDrawBegin(1,AtrPeriod);
   SetIndexLabel(1,"HLR_Day");
   
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,AtrBufferSPIKE);
   SetIndexDrawBegin(2,AtrPeriod);
   SetIndexLabel(2,"HLR_Spike");
   
   
   SetIndexBuffer(2,TempBuffer1);
   SetIndexBuffer(3,TempBuffer2);
   SetIndexBuffer(4,TempBuffer3);
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   if(Period() != PERIOD_D1)
   {
   Comment(" CAUTION : Ians_HLR only works on Daily Charts !!");
   return(0);
   }
  
   int i,k,counted_bars=IndicatorCounted();
 
   if(Bars<=AtrPeriod) return(0);
 
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) 
      {
         AtrBufferAVE[Bars-i]=0;
         AtrBufferSPIKE[Bars-i]=0;
         AtrBuffer[Bars-i]=0;
      }
 
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=High[i];
      double low =Low[i];
      double range = NormalizeDouble((high-low)/Point,0);
 
      TempBuffer1[i]=range;
      TempBuffer2[i]=range;
      if (range >= 120) TempBuffer3[i]=range; else TempBuffer3[i]=0;
      i--;
     }
 
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   
   for(i=0; i<limit; i++)
   {
      //AtrBuffer[i]=TempBuffer2[i]; //I take these two arrays out and iMAOnArray works ok
      //AtrBufferSPIKE[i]=TempBuffer3[i];
      AtrBufferAVE[i]=iMAOnArray(TempBuffer1,Bars,AtrPeriod,0,MODE_SMA,i);
   }
   
   
   return(0);
  }

very weird, My computer has plenty of grunt.

//AtrBuffer[i]=TempBuffer2[i]; //I take these two arrays out and iMAOnArray works ok
//AtrBufferSPIKE[i]=TempBuffer3[i];

Reducing demans on the loop when iMAOnArray is calculated sems to fix the average, but limits what I want the inidcator to do, I tried separating them into two loops loading the final buffers, this made no difference.

Any ideas . Thanks in Advance...