# Is there a maximum averaging period size for iATR? I'm getting some strange results... - page 2

16820

1. ` if (Bars==3000) Print (iATR(Symbol(),Period(),3000,0)); //Prints 0`
`TR[i] = max( High[i], Close[i+1] ) - min( Low[i], Close[i+1] )`
so at a minimum you must have length+1 bars to compute ATR(length).
2. The new code (MQL4\indicators\ATR.mq4) does a
`ATR[i] = Atr[i+1] + (TR[i]-TR[i+length])/length`
This is more efficient SMA than summing up length elements (which the old code did.) But, if the previous value is computed wrong, it takes length new values to reset. This is what your Bars=4500/6000 shows.

3. The code contains (my comments in red)
ExtATRBuffer[InpAtrPeriod]=firstValue;
limit=InpAtrPeriod+1; // The +1 is correct here - the last valid value
// is InpAtrPeriod, calculate remaining values.
}
else
limit=prev_calculated-1; // Here prev_calculated-1 must be recalculated.
// But if rates_total == InpAtrPeriod+1 there is
// no ATR[i-1] value
//--- the main loop of calculations
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;
Reported at service desk #978330 fix is do not recalculate the firstValue when rates_total == InpAtrPeriod + 1
```// if(rates_total<=InpAtrPeriod || InpAtrPeriod<=0)
if(rates_total<=InpAtrPeriod+1 || InpAtrPeriod<=0)
```