Why ATR is coded like that??!!

 

Hi everyone, my mt4 platform comes with ATR indicator that uses this calculation: 


   for(i=limit; i<rates_total; i++)
     {
      ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
      ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-InpAtrPeriod])/InpAtrPeriod;
     }

I don't understand why it calculates like that, isn't it wrong??? I would use this one instead: 


double CalculateATR(int timeframe,int index_forloop,int period){

      int index_atr =  iBarShift(NULL,timeframe,Time[index_forloop],false);
      double sum_adr=0;

      for(int i=index_atr+1; i<=index_atr+period; i++)
        {
         sum_adr += iHigh(NULL,timeframe,i)-iLow(NULL,timeframe,i);
        }

      return(sum_adr/period);
      
};

Where index_forloop is just the index if the function is called inside a foor loop in order to get ATR of higher timeframes. This is how I would calculate it even on excel, is my version wrong or what? Why don't standard mt4 atr is like that?

 

That's to account for gaps in consecutive candles. No version is wrong, but they do slightly different things

However the original is more efficient since it avoids repeating calculations in the moving average (ATR buffer)

 
ironhak: I don't understand why it calculates like that, isn't it wrong??? I would use this one instead: 

Both are wrong! They both generate the average of range ( Hi- Li ), not the average of True Range (max( Hi, Ci+1 )-min( Li, Ci+1 ).

The first uses SMMA. The second uses SMA.
          The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (2019)