Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drastically improve the speed of ATR calculation #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yarimiz
Copy link
Contributor

@yarimiz yarimiz commented Oct 27, 2023

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
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
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
Copy link

Khalizo commented Jan 22, 2024

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