TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.49k stars 1.74k forks source link

Sometimes I encounter that the NATR indicator data with talib is wrong? #477

Open boujrafh opened 2 years ago

boujrafh commented 2 years ago

Hi,

Sometimes I encounter that the NATR indicator data via talib is wrong?

when the value is good : please check in attachment : the data of the NATR indicator is located between 0.3 and 3

Capture d’écran 2021-11-18 222856

Sometimes it goes up to 369 ????

Capture d’écran 2021-11-18 222736

could you explain me why please ?

I use this function :

real = NATR(high, low, close, timeperiod=14)

and my version Python is 3.9.2 and my version talib is TA-Lib==0.4.19 and i tested with the last 0.4.21 but i have the same issue

trufanov-nok commented 2 years ago

Could you execute:

import talib
import numpy as np

h = [12.0511, 12.0539, 12.0914, 12.1217, 12.1192, 12.1013, 12.1418, 12.1450, 12.1130, 12.1322, 12.1431, 12.1543, 12.1285, 12.1274, 12.1725, 12.1794]
l = [12.0081, 12.0226, 12.0464, 12.0827, 12.0500, 12.0512, 12.0647, 12.0940, 12.1032, 12.1002, 12.1027, 12.1074, 12.1120, 12.1132, 12.1259, 12.1463]
c = [12.0231, 12.0469, 12.0879, 12.1192, 12.0532, 12.0648, 12.1411, 12.1130, 12.1088, 12.1096, 12.1430, 12.1254, 12.1146, 12.1259, 12.1669, 12.1591]

talib.NATR(np.array(h), np.array(l), np.array(c), 14)

talib.__ta_version__

I'm getting

>>> talib.NATR(np.array(h), np.array(l), np.array(c), 14)
array([       nan,        nan,        nan,        nan,        nan,
              nan,        nan,        nan,        nan,        nan,
              nan,        nan,        nan,        nan, 0.33410318,
       0.32988226])

which seems to be fine.

boujrafh commented 2 years ago

Thank you, I test and I return yoy a feedback

scilaci commented 2 years ago

Hello, about NATR and also ATR,

Which mathematical formula is used ?

Because using the formula referenced from this webiste I've differents results than talib

        # NATR ta-lib (in blue)
        dataframe['natr'] = ta.NATR(dataframe['high'], dataframe['low'], dataframe['close'], 14)
        # ATR ta-lib (in blue)
        dataframe['atr'] = ta.ATR(dataframe['high'], dataframe['low'], dataframe['close'], 14)

        # ATR from formula (in red)
        """
            ATR = Average ( True Range, n )

            Where:

                True Range = Max of ( High - Low ), ( High -PreviousClose ), ( PreviousClose - Low )
                Average = Simple, Exponential, Weighted, and Triangular
                n = Time period
        """
        high_low = dataframe['high'] - dataframe['low']
        high_pc = np.abs(dataframe['high'] - dataframe['close'].shift(1))
        pc_low = np.abs(dataframe['close'].shift(1) - dataframe['low'])
        tr_df = pd.concat([high_low, high_pc, pc_low], axis=1)

        true_range = np.max(tr_df, axis=1)

        dataframe['atr2'] = true_range.rolling(14).mean()

        # NATR from formula (in red)
        """
            NATR = ATR(n) / Close * 100

            Where: ATR(n) = Average True Range over ‘n’ periods.
        """
        dataframe['natr2'] = dataframe['atr2'] / dataframe['close'] * 100

image

and sometime:

image

trufanov-nok commented 2 years ago

@scilaci ta-lib's NATR is NATR[t] = (ATR(t, n) / Close[t]) * 100 and ATR(t,n) for data length t (t > n) and period n is:

ATR[0,n-1] = NULL                                              // t < n
ATR[n] = SMA(TRANGE[0,n], n)                         // t = n
ATR[t] = (ATR[t−1] × (n − 1) + TRANGE[t] ) /  n  // t > n 

where TRANGE[t]: max( high[t] - low[t], abs(close[t-1] - low[t]), abs(close[t-1] - high[t]))

In your code ATR is just a dataframe['atr2'] = true_range.rolling(14).mean() Which in terms of TA-Lib is ATR[t] = SMA( TRANGE(t), n=14 ).

The TA-Lib's calculation seems to be correct according to the wikipedia: https://en.wikipedia.org/wiki/Average_true_range#Calculation And the doc its refers to: https://www.earnforex.com/guides/average-true-range/