Skip to content

Drastically improve the speed of ATR calculation - #326

Open
yarimiz wants to merge 1 commit into
bukosabino:masterfrom
yarimiz:improve-ATR-speed
Open

yarimiz wants to merge 1 commit into
bukosabino:masterfrom
yarimiz:improve-ATR-speed

Conversation

@yarimiz

@yarimiz yarimiz commented Oct 27, 2023

Copy link
Copy Markdown
Contributor

This PR is to replace the for loop used to calculate the average part of Average True Range.

The result of the calculation is exactly the same, but it runs about ~17 faster.
For loops are bad when using pandas.

@bukosabino

Copy link
Copy Markdown
Owner

Hi @yarimiz ,

The results are similar, but not exactly the same, right?

You can test your code locally:

$ git clone https://github.com/bukosabino/ta.git
$ cd ta
$ make init
$ make test

@Groni3000

Copy link
Copy Markdown
Contributor

@bukosabino Hi is not entirely wrong btw. Correctness depends on implementation (sad to say it (ಥ﹏ಥ) ). For example, I have tested smoothing method of pandas_ta (another ta lib) and they smooth thier true ranges like I did in my implementation (and I show they are identical). They call it RMA (and use it by default). @yarimiz did the same but with adjust=False (I use adjust=False too). Who is right? Who is wrong? I don't know. It just depends on preferred way to smooth things, it's neither wrong or right, I guess.
image

Ok, you can say "it's another ta lib, I don't care how they perform smoothing". Lets took a look into TradingView implementation, here is their code of built-in ATR indicator (so they even have docs to it):
image

As we can see, they default smoothing method is RMA (though their RMA is freaking strange, I can't get the same values as they do, but their docs declare it as "It is the exponentially weighted moving average with alpha = 1 / length." and code alpha * src + (1 - alpha) * nz(sum[1]), so, I suppose it .ewm(alpha=1/window, adjust=False), but they have additional strange logic not the theme of topic).

@yarimiz If you want to speed up it a little bit, you shoud write it like that:
image
And here is 200_000 test
image

@Khalizo

Khalizo commented Jan 22, 2024

Copy link
Copy Markdown

ook into TradingVie

any update on this @yarimiz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants