0
$\begingroup$

I'm a software engineer and I'm not so good at maths, I am writing some software which performs technical analysis on stocks but it appears my maths is slightly off and I have spent hours and hours trying to figure out which part of my formula is wrong and I can't figure it out. I am hoping some maths genius may be able to discover my mistake.

Essentially, I am trying to calculate the Keltner Channels of a given stock, which seems to work fine for the first period, but all subsequent periods are very slightly wrong.

Here is my sample data: enter image description here

This is data of the first ever 32 trading days of stock BYND, you can illustrate this data yourself by going to https://uk.tradingview.com/chart/AFGZbVHw/ - then if you add in an indicator and add in Keltner Channels (KC) and Average True Range (ATR), you can see the data in that spreadsheet visualised, I've attached a picture of it below: enter image description here

In the top left of each indicator there is a settings icon where you can adjust the indicator, I have adjused the keltner channels to 5 periods and multipler to 2, the ATR is also adjusted to 5 periods as it is used to calculate the keltner channels.

To calculate the 3 Keltner Channel Values the formula is:
Keltner High Value = EMA Value + 2 * Average True Range (ATR)
Keltner Middle Value = EMA Value
Keltner Low Value = EMA Value - 2 * Average True Range (ATR)

If you look at my spreadsheet, I calculated all of the ATR and EMA values correctly, they match perfectly with the TradingView charts. When I calculate the first set of Keltner values, they are also a perfect match with TradingView, but the problem is when I calculate the next Keltner Channel values, they no longer match the TradingView values, I get the following values:

ATR = 13.04
Keltner Low = 44.5068061 (should be 46.82)
Keltner Middle = 70.58999 (is correct and matches TradingView at 70.59)
Keltner High = 96.67319163 (should be 94.36)

I can upload my spreadsheet so you can see my calculations, but I think the main problem is that when you calculate the second keltner channel values, the ATR value is no longer correct, although I know ATR at that position is 13.04 (you can even see the TradingView chart show this), the actual value used in the keltner channel is something like 94.36 - 70.59 / 2 = 11.885 so it's like they are not even using the ATR value for that period. Where on earth are they calculating the 11.885 value from? Or does the formula for keltner change somehow on the second iteration???

Any guidance would be appreciated, happy to provide the excel file too, just not sure how to upload it.

EDIT: My excel file can be downloaded from here: https://gofile.io/d/XZdFaH

$\endgroup$

1 Answer 1

1
$\begingroup$

Well, I am an idiot. It turns out all my calculations were correct, but there are multiple ways to calcualte ATR and I was just calculating it using a different method.

It turns out that TradingView is using the ATR EMA Smoothing algorithm for calculating the ATR value, where as I am using the ATR RMA Smoothing algorithm.

It's annoying that there are multiple ways to calculate ATR which all give different results, very confusing! But at least now I know.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .