Confused about iATR

 

Hi, I always had the idea that iATR(NULL, NULL, 1, i) was the same as (High[i] - Low[i]). Both are suppose to calculate the range of the i'th bar. However I found this is not the case and I'm getting strange data showing up. I'm using the following code

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  Blue
#property indicator_width1  3

double myBuffer[];

int init()
  {
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,myBuffer);
   IndicatorShortName("iATR - myATR");
   return(0);
  }

int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for(int i=0; i<limit; i++)
   {
      myBuffer[i] = iATR(NULL, NULL, 1, i) - (High[i] - Low[i]);  
   }

   return(0);
  }

The indicator above is displaying actual differences between the two ATR calculations. When I look at the D1 timeframe, the indicator is displaying a single spike every so often as illustrated below.


My original intent was to calculate the 5 day ATR via iATR(NULL, PERIOD_D1, 5, i). I compared this to ((High[i] -Low[i]) + (High[i+1] -Low[i+1]) + (High[i+2] -Low[i+2]) + (High[i+3] -Low[i+3]) + (High[i+4] -Low[i+4]))/5 and noticed slight differences between the calculations.

Would anyone know why a difference is showing?

Any help most appreciated

Cheers,

Mag1kus

 

Your understanding of True Range is faulty.

It is not High[i] - Low[i].

It is Max(High[i]. Close[i+1]) - Min(Low[i], Close[i+1])

You'll notice that the 'differences' occur at gaps

 
Thanks for that, results are more consistent now when including the previous bar's close.