so at a minimum you must have length+1 bars to compute ATR(length).
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.
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
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.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;