Coder Social home page Coder Social logo

Comments (5)

xmatthias avatar xmatthias commented on May 5, 2024

there's another issue where this is discussed in the strategy repo: freqtrade/freqtrade-strategies#30

Feel free to improve performance (but honestly, you'll have problems with that i'll think).
The way ATR is calculated requires a loop (current value is based on the result of the same calculation for the previous value).

Every indicator that requires a loop will automatically be a lot slower than any indicator that can be vectorized (calculated in one go across the whole timerange) - and there is very little possibility to optimize these calls.

That said, i just noticed that we have 2 different calculations of atr:
one in volatility.py (this will most likely be wrong as it seems to only use close - but uses the average_true_range directly from pyti - so having that fixed would require an issue against the pyti repository.

The second version is in indicators (which is vendored from qtpylib). At first glance, the calculation seems ... better aligned to investopedia ...

however please investigate in the linked issue above, as i think also this calculation does not match with tradingview (however i don't see tradingview as gold standard, maybe their implementation is wrong).

from technical.

baont avatar baont commented on May 5, 2024

well i found this code on stackoverflow https://stackoverflow.com/questions/40256338/calculating-average-true-range-atr-on-ohlc-data-with-python and tested myself

def wwma(values, n):
    """
     J. Welles Wilder's EMA 
    """
    return values.ewm(alpha=1/n, adjust=False).mean()

def atr(df, n=14):
    data = df.copy()
    high = data[HIGH]
    low = data[LOW]
    close = data[CLOSE]
    data['tr0'] = abs(high - low)
    data['tr1'] = abs(high - close.shift())
    data['tr2'] = abs(low - close.shift())
    tr = data[['tr0', 'tr1', 'tr2']].max(axis=1)
    atr = wwma(tr, n)
    return atr

It's extremely extremely extremely fast

from technical.

xmatthias avatar xmatthias commented on May 5, 2024

it might be fast - but it's most likely not aligned to the calculation of investopedia - which does not mention the use of Wilders MA at all.

You can also use from technical.vendor.qtpylib.indicators import atr - which will also be extremely fast (as said, it's in technical twice for no real reason)

from technical.

baont avatar baont commented on May 5, 2024

i could change to values.rolling(14).mean(). Haven't tested it yet

from technical.

xmatthias avatar xmatthias commented on May 5, 2024

as said, technical does contain an implementation of atr already, which is fast.
I'll remove the pyti mapper so it'll be clear which one to use.

from technical.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.