Difference in ATR calculation

 

Is anyone able to tell me the difference between how MT4 calculates the ATR as opposed to NinjaTrader?

I'm trying to match NT's version to MT4, but the results are wildly different over all time frames. Really appreciate an explanation, coz I can't pick where the discrepancy is.

NT ATR source code attached...

Thanks,

oz


Files:
 

it's very simple count the bars

 

It's also obvious that the server's being used in both of these examples are not synchronized to the same GMT timestamp, this is an important factor in any candle-based indicator-driven assessment situation as the specific values for the OHLC for the candles themselves will differ in accordance to the server's timestamp.

Drove me nuts back in 2007 when IBFX had their micro sever and retail server (both live account servers mind you) unsynchronized so badly (off by minutes) that I had clients with retail accounts that would miss entire trade opportunities while clients on the micro accounts were not.

Lesson there is that not only is each broker their own market maker in a sense, but also each server within the broker as well. Personally I think the days of the off-exchange forex market are numbered.

 

Thank you Phillip and jolqwer. The issue is now clear. Much work to do on sync I think...
 
ozfader:
Thank you Phillip and jolqwer. The issue is now clear. Much work to do on sync I think...


Actually, it's still not there I'm afraid...

Both platforms sync'd to brokers GMT timestamp. MT4 plots the candle open while NT plots the candle close, but other than that, everything the same, including OHLC (discounting fractional pips).

There's something in the way each version calcs I think...

 

Mathematically they appear to be the same equation.

MT4 ATR calc:

TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
NT7 ATR calc:
trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(High[0] - Low[0], Math.Abs(High[0] - Close[1])));

Edit: Never mind my prior post, the equations are identical, I had an error in my manual math equation.

So no mathematical reasoning for the discrepancy, both equations produce identical results.

At this point you'd be best to export a table of High/Low/Close values and recreate the ATR in Excel, then you'll see which one is producing the problem.

 
1005phillip:

Mathematically they appear to be the same equation.

MT4 ATR calc:

NT7 ATR calc:

Edit: Never mind my prior post, the equations are identical, I had an error in my manual math equation.

So no mathematical reasoning for the discrepancy, both equations produce identical results.

At this point you'd be best to export a table of High/Low/Close values and recreate the ATR in Excel, then you'll see which one is producing the problem.


Thanks for your advice Phillip. Will do...

oz

 
1005phillip:

So no mathematical reasoning for the discrepancy, both equations produce identical results.

I'm not sure about that.

MT4 uses a simple moving average of the range of each bar. Many other systems use an EMA. The NinjaTrader code seems to use something else again:

Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period));

It's years since I abandoned NinjaTrader, but on e.g. a 14-bar ATR that looks as though it means "13/14ths of yesterday's average, plus the current value, divided by 14". That's not the same as an SMA, and on the whole it's going to give a smoother line. (It's what Wikipedia refers to as a "modified moving average" or "smoothed moving average" - https://en.wikipedia.org/wiki/Moving_average)

 
jjc:

It's years since I abandoned NinjaTrader, but on e.g. a 14-bar ATR that looks as though it means "13/14ths of yesterday's average, plus the current value, divided by 14". That's not the same as an SMA, and on the whole it's going to give a smoother line. (It's what Wikipedia refers to as a "modified moving average" or "smoothed moving average" - https://en.wikipedia.org/wiki/Moving_average)

As a follow up....

I can exactly replicate MT4's ATR values by exporting the data to Excel, calculating the range of each bar, and then doing a simple moving average of the last n values - i.e. Excel's AVERAGE() function. If I'm right about what NinjaTrader is doing then it's equivalent to replacing the MODE_SMA in line 67 of the source code for ATR.mq4 with MODE_SMMA.

 

Hi guys. As I understood the NT7 code, it was using the technically correct Wilder version of ATR as described here:

http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:average_true_range_a

That is:

Current ATR = [(Prior ATR x 13) + Current TR] / 14

  - Multiply the previous 14-day ATR by 13.
  - Add the most recent day's TR value.
  - Divide the total by 14

However, it appears that the MT4 version is simply using an average of the true range. Not sure if this is a simple change in the MA mode or more like a change in how AtrBuffer is calculated. I figured it would need to be something like:

AtrBuffer[i] = (((AtrPeriod-1)*AtrBuffer[i+1])+TempBuffer[i])/AtrPeriod;

Thoughts?

 
jjc:

As a follow up....

I can exactly replicate MT4's ATR values by exporting the data to Excel, calculating the range of each bar, and then doing a simple moving average of the last n values - i.e. Excel's AVERAGE() function. If I'm right about what NinjaTrader is doing then it's equivalent to replacing the MODE_SMA in line 67 of the source code for ATR.mq4 with MODE_SMMA.



Implemented, see attached.

tradeboy:

AtrBuffer[i] = (((AtrPeriod-1)*AtrBuffer[i+1])+TempBuffer[i])/AtrPeriod;

Thoughts?


Implemented, see attached.

(they produce identical results, verified, but figured I'd do both for sake of completion since this was easy changes)

Reason: