TA-Lib / ta-lib-python

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

atr period might not be correct #560

Closed quantfreedom closed 1 year ago

quantfreedom commented 2 years ago

Screenshot_1 why is it that when i make an atr from scratch and use math that it is off from the ta lib atr

mrjbq7 commented 2 years ago

Here's the source code:

https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_ATR.c#L218

I would check:

1) If your tr column matches talib.TRANGE. 2) If the rolling(14).mean() matches talib.SMA (I think it should). 3) If your implementation does "smoothing" like the TA-Lib one (https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_ATR.c#L281).

trufanov-nok commented 2 years ago

Because ATR is not SMA over TR: https://en.wikipedia.org/wiki/Average_true_range#Calculation So your ATR is wrong.

quantfreedom commented 2 years ago

https://www.investopedia.com/terms/a/atr.asp ... according to this ... my true range is calculated correctly ... so now the question is who is right

mrjbq7 commented 2 years ago

Two different definitions for an indicator doesn't make either right or wrong...

mrjbq7 commented 2 years ago

Look at the code I linked and decide if you agree with the definition?

quantfreedom commented 2 years ago

@mrjbq7 oh yeah i am looking at it right now and trying to compare it to tradingview ... but i wasn't meaning who is right as in to start an argument ... i was saying that as genuinely wondering ... who is actually right lol ... but now it seems like there are 4 or 5 different ways and therefor no one is really right

mrjbq7 commented 2 years ago

The TA-Lib version references Wilder, which might be the more popular definition... 🤷

quantfreedom commented 2 years ago

image very interesting ... seems like it does the SMA and the TR like yours does ... but i guess the smoothing you apply to it is what is changing the values ... i wonder if adding the option to change the smoothing would be a good idea like they do in tradingview image

quantfreedom commented 2 years ago

what is crazy is that i checked all the tradingview options and non of them are even close to our values no matter what smoothing you choose LOL ... 6 different ideas on what the best ATR calculations are LOLOLOL

trufanov-nok commented 2 years ago

I played a lot with ATR and its representations on different platforms. TA-Lib's ATR indicator implementation matches wiki definition and uses so called Wilders smoothing. Wilders smoothing is RMA in TradingView (if you open ATR indicator source and hover mouse over ta.rma function it'll pop up a hint box that says that RMA is

Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length.

That's Wilders smoothing. And in TDA Ameritrade's thinkscript it's a WildersSmoothing() function:

The Wilder's Smoothing study is similar to the Exponential Moving Average with the difference that Wilder's Smoothing uses a smoothing factor of 1/length which makes it respond more slowly to price changes compared to other moving averages.

TDA also uses 14-period Wilder's moving average by default for ATR calculation.
So these 3 (TA-Lib, TradingView, TDA) are using Wilders smoothing for ATR by default.

But if you compare TA-Lib's ATR with TradingView ATR (with RMA of course) you'll find the differences... Why? Because they calculate TR a bit differently. Their TR have an additional optional argument:

ARGUMENTS handle_na (simple bool) How NaN values are handled. if true, and previous day's close is NaN then tr would be calculated as current day high-low. Otherwise (if false) tr would return NaN in such cases. Also note, that ta.atr uses ta.tr(true). REMARKS ta.tr(false) is exactly the same as ta.tr.

If you clone ATR indicator in TradingView, edit its sourcecode and replace the line

plot(ma_function(ta.tr(true), length), title = "ATR", color=color.new(#B71C1C, 0))

with

plot(ma_function(ta.tr(false), length), title = "ATR", color=color.new(#B71C1C, 0))

Then the TradingView ATR results will match TA-Lib's ATR (it might be +/- 0.01 due to roundings and accuracy of your input data).

Btw, while TA-Lib lacks Wilders smoothing as a standolone function one can easily create such by cloning EMA and replacing the usage of #define PER_TO_K( per ) ((double)2.0 / ((double)(per + 1))) with #define PER_TO_WILDERS_K( per ) ((double)1.0 / (double)(per)). I did that. I also made the ATR that allows to choose the smoothing type as an option for internal tests. And this ATR with such Wilders smoothing matches the default TA-Lib ATR.

darose commented 1 year ago

I'm seeing the same discrepancy, but ta.tr(false) doesn't seem to resolve the issue.

For example, see the following code that computes 50-day ATR for stock NVDA over the 200 days ending on 2022-03-23:

import numpy as np
from decimal import Decimal
import talib as ta
highs=[Decimal('175.75'), Decimal('174.92'), Decimal('179.39225'), Decimal('180.395'), Decimal('180.1625'), Decimal('179.546775'), Decimal('188.35'), Decimal('193.75'), Decimal('185.365'), Decimal('189.6375'), Decimal('191.5275'), Decimal('194.2'), Decimal('193.455'), Decimal('200.7875'), Decimal('200.9875'), Decimal('201.625'), Decimal('204.56'), Decimal('205.0525'), Decimal('208.42'), Decimal('208.75'), Decimal('201.33'), Decimal('200.80125'), Decimal('205.3275'), Decimal('204.6125'), Decimal('204.1775'), Decimal('198.47'), Decimal('191.5711'), Decimal('190.42'), Decimal('188.38'), Decimal('195.27'), Decimal('198.87'), Decimal('197'), Decimal('194.42'), Decimal('196.22'), Decimal('196.46'), Decimal('198.53'), Decimal('196.3'), Decimal('199.61'), Decimal('202.22'), Decimal('203.18'), Decimal('207.33'), Decimal('205.7'), Decimal('205.0799'), Decimal('204.3'), Decimal('200.49'), Decimal('200.2899'), Decimal('202.1381'), Decimal('202.87'), Decimal('197.7'), Decimal('196.3365'), Decimal('204.95'), Decimal('208.6506'), Decimal('219.97'), Decimal('219.59'), Decimal('224.7'), Decimal('223.4'), Decimal('227.22'), Decimal('230.43'), Decimal('226.95'), Decimal('226.97'), Decimal('225.9299'), Decimal('229.86'), Decimal('228.99'), Decimal('226.1'), Decimal('225.38'), Decimal('226.26'), Decimal('229.64'), Decimal('224.1'), Decimal('223.6699'), Decimal('222.77'), Decimal('223.21'), Decimal('214.33'), Decimal('214.25'), Decimal('219.6'), Decimal('225.345'), Decimal('221.49'), Decimal('217.99'), Decimal('214.19'), Decimal('210.17'), Decimal('210.66'), Decimal('208.59'), Decimal('205.4179'), Decimal('206.48'), Decimal('207.2'), Decimal('213.22'), Decimal('212.06'), Decimal('210.63'), Decimal('210.57'), Decimal('209.9'), Decimal('217.55'), Decimal('219.31'), Decimal('222.91'), Decimal('223.79'), Decimal('224.33'), Decimal('227.11'), Decimal('231.3'), Decimal('233.5499'), Decimal('252.59'), Decimal('250.9'), Decimal('249.5'), Decimal('257.09'), Decimal('258.94'), Decimal('266.78'), Decimal('267.84'), Decimal('313.65'), Decimal('314'), Decimal('311'), Decimal('323.1'), Decimal('308.5'), Decimal('305.9'), Decimal('306.8'), Decimal('306.44'), Decimal('303.9'), Decimal('305.09'), Decimal('327.6'), Decimal('330.88'), Decimal('346.47'), Decimal('323.6'), Decimal('328.55'), Decimal('327.1'), Decimal('334.12'), Decimal('333.53'), Decimal('332.8934'), Decimal('324.7799'), Decimal('321.29'), Decimal('302.41'), Decimal('324.49'), Decimal('322.9'), Decimal('322.05'), Decimal('313.05'), Decimal('302.94'), Decimal('286.78'), Decimal('305'), Decimal('311.6'), Decimal('289.22'), Decimal('281.44'), Decimal('291.2'), Decimal('295.55'), Decimal('300.59'), Decimal('310.865'), Decimal('313.3'), Decimal('305.48'), Decimal('304.57'), Decimal('300.3'), Decimal('307.11'), Decimal('304.68'), Decimal('294.16'), Decimal('284.3799'), Decimal('284.22'), Decimal('274.69'), Decimal('280.65'), Decimal('285.95'), Decimal('284.8'), Decimal('271.969'), Decimal('266.38'), Decimal('265.4317'), Decimal('255.7904'), Decimal('248.23'), Decimal('233.8'), Decimal('229.43'), Decimal('240.57'), Decimal('239.95'), Decimal('228.58'), Decimal('245.09'), Decimal('251.45'), Decimal('258.17'), Decimal('250.77'), Decimal('246.35'), Decimal('251.82'), Decimal('252.3'), Decimal('267.25'), Decimal('269.2499'), Decimal('261.52'), Decimal('248.75'), Decimal('265.45'), Decimal('265.82'), Decimal('257.85'), Decimal('249.86'), Decimal('240.6399'), Decimal('241.5499'), Decimal('238'), Decimal('242.17'), Decimal('246.65'), Decimal('243.77'), Decimal('244.09'), Decimal('243.2599'), Decimal('236.8'), Decimal('230.33'), Decimal('223.7299'), Decimal('232.2'), Decimal('227.88'), Decimal('231.45'), Decimal('222.62'), Decimal('230.38'), Decimal('245.97'), Decimal('248.42'), Decimal('265.6899'), Decimal('271.5199'), Decimal('272.38'), Decimal('266.115')]
lows=[Decimal('172.5575'), Decimal('171.760025'), Decimal('174.4375'), Decimal('176.627525'), Decimal('177.2795'), Decimal('175.84425'), Decimal('177.562525'), Decimal('185.84'), Decimal('178.2265'), Decimal('183.85875'), Decimal('189.0775'), Decimal('190.8825'), Decimal('188.94375'), Decimal('193.19'), Decimal('196.56875'), Decimal('198.6375'), Decimal('200.19'), Decimal('202.8775'), Decimal('203.5025'), Decimal('203.3205'), Decimal('197.0075'), Decimal('197.5425'), Decimal('201.8775'), Decimal('201.140025'), Decimal('197.5275'), Decimal('188.585025'), Decimal('180.73'), Decimal('178.655'), Decimal('181.64'), Decimal('187.42'), Decimal('192.76'), Decimal('192.5'), Decimal('189.14'), Decimal('187.41'), Decimal('189.95'), Decimal('193.2803'), Decimal('192.63'), Decimal('193.61'), Decimal('192.2'), Decimal('198.28'), Decimal('203.42'), Decimal('202.1'), Decimal('201.43'), Decimal('198.3454'), Decimal('194.3'), Decimal('196.2'), Decimal('198.51'), Decimal('194.53'), Decimal('192.67'), Decimal('190'), Decimal('187.62'), Decimal('199.33'), Decimal('209.5'), Decimal('215.35'), Decimal('217.22'), Decimal('217.9'), Decimal('221.67'), Decimal('225.51'), Decimal('221.2'), Decimal('223.565'), Decimal('222.945'), Decimal('222'), Decimal('225.224'), Decimal('219.77'), Decimal('221.31'), Decimal('222.7'), Decimal('218.58'), Decimal('220.86'), Decimal('219.66'), Decimal('219.27'), Decimal('218.3'), Decimal('206.62'), Decimal('209.5'), Decimal('211.96'), Decimal('218.9'), Decimal('218.61'), Decimal('213.25'), Decimal('206.51'), Decimal('204.67'), Decimal('206.88'), Decimal('202.03'), Decimal('195.55'), Decimal('198.54'), Decimal('200.8'), Decimal('209.72'), Decimal('207.75'), Decimal('205.11'), Decimal('205.28'), Decimal('207.13'), Decimal('211.2201'), Decimal('216.62'), Decimal('216.44'), Decimal('220.37'), Decimal('219.82'), Decimal('220.83'), Decimal('225.61'), Decimal('227.7'), Decimal('239.24'), Decimal('242.8201'), Decimal('245.23'), Decimal('250'), Decimal('252.27'), Decimal('258'), Decimal('262.35'), Decimal('271.18'), Decimal('294.1'), Decimal('299.07'), Decimal('299.64'), Decimal('287.78'), Decimal('297.7701'), Decimal('296.3'), Decimal('292.47'), Decimal('297.0587'), Decimal('288'), Decimal('313.21'), Decimal('319.05'), Decimal('319'), Decimal('308.8'), Decimal('309.28'), Decimal('313.5'), Decimal('320.36'), Decimal('318.6401'), Decimal('313.8'), Decimal('310.25'), Decimal('301.3'), Decimal('280.38'), Decimal('306.51'), Decimal('314.2101'), Decimal('304.28'), Decimal('298.61'), Decimal('281.16'), Decimal('272.5'), Decimal('278.38'), Decimal('280.93'), Decimal('277.6'), Decimal('271.45'), Decimal('274.01'), Decimal('284.49'), Decimal('294.3101'), Decimal('296.4'), Decimal('300.1181'), Decimal('293.66'), Decimal('295.4'), Decimal('293.3057'), Decimal('297.85'), Decimal('283.49'), Decimal('275.33'), Decimal('270.65'), Decimal('270.57'), Decimal('256.4384'), Decimal('268.39'), Decimal('276.08'), Decimal('264.98'), Decimal('262.1001'), Decimal('257.7'), Decimal('250.52'), Decimal('240.78'), Decimal('232.63'), Decimal('208.88'), Decimal('220'), Decimal('223'), Decimal('216.75'), Decimal('212.96'), Decimal('230.52'), Decimal('238.9001'), Decimal('245.5301'), Decimal('237.8'), Decimal('236.32'), Decimal('242.02'), Decimal('239.8'), Decimal('253.53'), Decimal('256'), Decimal('237.73'), Decimal('237.55'), Decimal('247.84'), Decimal('255.52'), Decimal('241.65'), Decimal('231'), Decimal('230'), Decimal('223.01'), Decimal('208.9'), Decimal('233.81'), Decimal('237.0701'), Decimal('231.32'), Decimal('234.15'), Decimal('234.69'), Decimal('224.82'), Decimal('213.3'), Decimal('206.5'), Decimal('222.47'), Decimal('218.82'), Decimal('220.46'), Decimal('211.5901'), Decimal('213.2201'), Decimal('231.72'), Decimal('239.06'), Decimal('246.24'), Decimal('259.67'), Decimal('260.72'), Decimal('255.7501')]
closes=[Decimal('173.5825'), Decimal('174.25'), Decimal('178.2525'), Decimal('180.1875'), Decimal('177.885'), Decimal('178.1025'), Decimal('186.5725'), Decimal('186.3875'), Decimal('184.2725'), Decimal('188.8675'), Decimal('190.5725'), Decimal('192.055'), Decimal('190.31'), Decimal('199.85'), Decimal('200.2675'), Decimal('200.025'), Decimal('202.12'), Decimal('204.87'), Decimal('206.985'), Decimal('203.7175'), Decimal('199.0275'), Decimal('200.5025'), Decimal('205.125'), Decimal('202.5'), Decimal('198.415'), Decimal('189.6625'), Decimal('181.61'), Decimal('187.7975'), Decimal('186.12'), Decimal('194.1'), Decimal('195.94'), Decimal('195.58'), Decimal('192.94'), Decimal('192.08'), Decimal('195.03'), Decimal('196.62'), Decimal('194.99'), Decimal('197.5'), Decimal('198.15'), Decimal('202.74'), Decimal('206.37'), Decimal('203.66'), Decimal('202.95'), Decimal('199.36'), Decimal('196.99'), Decimal('199.05'), Decimal('201.88'), Decimal('199.5'), Decimal('194.58'), Decimal('190.4'), Decimal('197.98'), Decimal('208.16'), Decimal('219.58'), Decimal('217.93'), Decimal('222.13'), Decimal('220.68'), Decimal('226.36'), Decimal('226.88'), Decimal('223.85'), Decimal('224.41'), Decimal('223.96'), Decimal('228.43'), Decimal('226.62'), Decimal('223.39'), Decimal('221.77'), Decimal('224.78'), Decimal('221.52'), Decimal('222.42'), Decimal('223.41'), Decimal('222.42'), Decimal('219'), Decimal('211.13'), Decimal('212.46'), Decimal('219.41'), Decimal('224.82'), Decimal('220.81'), Decimal('216.6'), Decimal('206.99'), Decimal('205.17'), Decimal('207.16'), Decimal('207.42'), Decimal('197.32'), Decimal('204.51'), Decimal('207'), Decimal('210.75'), Decimal('208.31'), Decimal('206.95'), Decimal('206.71'), Decimal('209.39'), Decimal('217.46'), Decimal('218.62'), Decimal('222.22'), Decimal('222.9'), Decimal('221.03'), Decimal('226.92'), Decimal('227.26'), Decimal('231.66'), Decimal('247.17'), Decimal('244.51'), Decimal('249.41'), Decimal('255.67'), Decimal('258.27'), Decimal('264.01'), Decimal('265.98'), Decimal('298.01'), Decimal('297.52'), Decimal('308.04'), Decimal('306.57'), Decimal('294.59'), Decimal('303.9'), Decimal('303.9'), Decimal('300.25'), Decimal('302.03'), Decimal('292.61'), Decimal('316.75'), Decimal('329.85'), Decimal('319.56'), Decimal('317.46'), Decimal('326.74'), Decimal('315.03'), Decimal('333.76'), Decimal('326.76'), Decimal('314.35'), Decimal('321.26'), Decimal('306.93'), Decimal('300.37'), Decimal('324.27'), Decimal('318.26'), Decimal('304.9'), Decimal('301.98'), Decimal('281.61'), Decimal('283.37'), Decimal('304.59'), Decimal('283.87'), Decimal('278.01'), Decimal('277.19'), Decimal('290.75'), Decimal('294'), Decimal('296.4'), Decimal('309.45'), Decimal('303.22'), Decimal('300.01'), Decimal('295.86'), Decimal('294.11'), Decimal('301.21'), Decimal('292.9'), Decimal('276.04'), Decimal('281.78'), Decimal('272.47'), Decimal('274'), Decimal('278.17'), Decimal('279.99'), Decimal('265.75'), Decimal('269.42'), Decimal('259.03'), Decimal('250.67'), Decimal('241.5'), Decimal('233.74'), Decimal('233.72'), Decimal('223.24'), Decimal('227.72'), Decimal('219.44'), Decimal('228.4'), Decimal('244.86'), Decimal('246.38'), Decimal('252.42'), Decimal('239.48'), Decimal('243.19'), Decimal('247.28'), Decimal('251.08'), Decimal('267.05'), Decimal('258.24'), Decimal('239.49'), Decimal('242.67'), Decimal('264.95'), Decimal('265.11'), Decimal('245.07'), Decimal('236.42'), Decimal('233.9'), Decimal('223.87'), Decimal('237.48'), Decimal('241.57'), Decimal('243.85'), Decimal('234.77'), Decimal('242.2'), Decimal('237.14'), Decimal('229.36'), Decimal('213.52'), Decimal('215.14'), Decimal('230.14'), Decimal('226.58'), Decimal('221'), Decimal('213.3'), Decimal('229.73'), Decimal('244.96'), Decimal('247.66'), Decimal('264.53'), Decimal('267.34'), Decimal('265.24'), Decimal('256.34')]

lows_arr = np.array(lows, dtype=np.double)
highs_arr = np.array(highs, dtype=np.double)
closes_arr = np.array(closes, dtype=np.double)
ta.ATR(highs_arr, lows_arr, closes_arr, timeperiod=50)
array([        nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
               nan,         nan,         nan,         nan,         nan,
        6.109859  ,  6.20107382,  6.31325234,  6.2717873 ,  6.29595155,
        6.28003252,  6.28523187,  6.25792723,  6.24776869,  6.19091331,
        6.12679305,  6.16145719,  6.11354804,  6.12827708,  6.08711154,
        6.05516931,  6.15526592,  6.0969606 ,  6.05521939,  6.016915  ,
        5.9947767 ,  6.12248117,  6.09503155,  6.12593092,  6.1323123 ,
        6.13386605,  6.16238873,  6.24094096,  6.22612214,  6.21139969,
        6.2183717 ,  6.33140427,  6.38797618,  6.38821666,  6.38485232,
        6.34335528,  6.32688817,  6.30615041,  6.2438274 ,  6.28215085,
        6.21030784,  6.21550168,  6.15959165,  6.12659981,  6.12966782,
        6.12087446,  6.12425497,  6.42036987,  6.45356047,  6.42428926,
        6.44940348,  6.45381541,  6.5003391 ,  6.48013232,  7.30392967,
        7.55585108,  7.67433406,  7.99004738,  8.24464643,  8.3059535 ,
        8.34983443,  8.46223774,  8.42981899,  8.60302261,  9.13076216,
        9.23074691,  9.59553197,  9.69962133,  9.89102891,  9.96520833,
       10.14770416, 10.24714808, 10.42407312, 10.50618966, 10.69586586,
       11.01294855, 11.27508957, 11.25078578, 11.38117007, 11.44234667,
       11.64909973, 11.70171774, 12.00008338, 12.37348172, 12.35841208,
       12.31104384, 12.40862296, 12.3816505 , 12.26581749, 12.30980114,
       12.32724312, 12.31709826, 12.25415629, 12.14895917, 12.16597998,
       12.34646038, 12.47613118, 12.50120655, 12.52418242, 12.63873077,
       12.63115616, 12.57593303, 12.72081437, 12.66377609, 12.64490057,
       12.69023655, 12.73663982, 12.79390703, 13.03642889, 13.05010031,
       13.1404983 , 13.34168834, 13.38725457, 13.45330948, 13.43524129,
       13.41933446, 13.44334777, 13.37508082, 13.3035792 , 13.28750762,
       13.34515746, 13.34325232, 13.55218727, 13.50514352, 13.69064065,
       13.62282784, 13.81957128, 13.92037986, 13.85477026, 13.94847286,
       14.2515034 , 14.13367333, 14.04259786, 14.01234591, 13.93089899,
       13.82367901, 13.79360543, 13.85833332, 13.92576465, 13.98844936,
       13.93508037, 13.87617877, 13.81925319, 13.88606613, 13.9331448 ,
       13.84168191, 13.95384627, 13.91176734, 13.866732  , 13.79669536])

So TA-Lib computes the ATR-50 for 2022-03-23 as 13.80, while TV computes it as 13.78 - even when using RMA and ta.tr(false). I know it's only off by a small amount in this instance, but it seems to be more than can be explained by rounding.

mrjbq7 commented 1 year ago

What is ta.tr(false)?On Jul 25, 2023, at 3:00 PM, darose @.***> wrote: I'm seeing the same discrepancy, but ta.tr(false) doesn't seem to resolve the issue. For example, see the following code that computes 50-day ATR over the 200 days ending on 2022-03-23: import numpy as np from decimal import Decimal import talib as ta highs=[Decimal('175.75'), Decimal('174.92'), Decimal('179.39225'), Decimal('180.395'), Decimal('180.1625'), Decimal('179.546775'), Decimal('188.35'), Decimal('193.75'), Decimal('185.365'), Decimal('189.6375'), Decimal('191.5275'), Decimal('194.2'), Decimal('193.455'), Decimal('200.7875'), Decimal('200.9875'), Decimal('201.625'), Decimal('204.56'), Decimal('205.0525'), Decimal('208.42'), Decimal('208.75'), Decimal('201.33'), Decimal('200.80125'), Decimal('205.3275'), Decimal('204.6125'), Decimal('204.1775'), Decimal('198.47'), Decimal('191.5711'), Decimal('190.42'), Decimal('188.38'), Decimal('195.27'), Decimal('198.87'), Decimal('197'), Decimal('194.42'), Decimal('196.22'), Decimal('196.46'), Decimal('198.53'), Decimal('196.3'), Decimal('199.61'), Decimal('202.22'), Decimal('203.18'), Decimal('207.33'), Decimal('205.7'), Decimal('205.0799'), Decimal('204.3'), Decimal('200.49'), Decimal('200.2899'), Decimal('202.1381'), Decimal('202.87'), Decimal('197.7'), Decimal('196.3365'), Decimal('204.95'), Decimal('208.6506'), Decimal('219.97'), Decimal('219.59'), Decimal('224.7'), Decimal('223.4'), Decimal('227.22'), Decimal('230.43'), Decimal('226.95'), Decimal('226.97'), Decimal('225.9299'), Decimal('229.86'), Decimal('228.99'), Decimal('226.1'), Decimal('225.38'), Decimal('226.26'), Decimal('229.64'), Decimal('224.1'), Decimal('223.6699'), Decimal('222.77'), Decimal('223.21'), Decimal('214.33'), Decimal('214.25'), Decimal('219.6'), Decimal('225.345'), Decimal('221.49'), Decimal('217.99'), Decimal('214.19'), Decimal('210.17'), Decimal('210.66'), Decimal('208.59'), Decimal('205.4179'), Decimal('206.48'), Decimal('207.2'), Decimal('213.22'), Decimal('212.06'), Decimal('210.63'), Decimal('210.57'), Decimal('209.9'), Decimal('217.55'), Decimal('219.31'), Decimal('222.91'), Decimal('223.79'), Decimal('224.33'), Decimal('227.11'), Decimal('231.3'), Decimal('233.5499'), Decimal('252.59'), Decimal('250.9'), Decimal('249.5'), Decimal('257.09'), Decimal('258.94'), Decimal('266.78'), Decimal('267.84'), Decimal('313.65'), Decimal('314'), Decimal('311'), Decimal('323.1'), Decimal('308.5'), Decimal('305.9'), Decimal('306.8'), Decimal('306.44'), Decimal('303.9'), Decimal('305.09'), Decimal('327.6'), Decimal('330.88'), Decimal('346.47'), Decimal('323.6'), Decimal('328.55'), Decimal('327.1'), Decimal('334.12'), Decimal('333.53'), Decimal('332.8934'), Decimal('324.7799'), Decimal('321.29'), Decimal('302.41'), Decimal('324.49'), Decimal('322.9'), Decimal('322.05'), Decimal('313.05'), Decimal('302.94'), Decimal('286.78'), Decimal('305'), Decimal('311.6'), Decimal('289.22'), Decimal('281.44'), Decimal('291.2'), Decimal('295.55'), Decimal('300.59'), Decimal('310.865'), Decimal('313.3'), Decimal('305.48'), Decimal('304.57'), Decimal('300.3'), Decimal('307.11'), Decimal('304.68'), Decimal('294.16'), Decimal('284.3799'), Decimal('284.22'), Decimal('274.69'), Decimal('280.65'), Decimal('285.95'), Decimal('284.8'), Decimal('271.969'), Decimal('266.38'), Decimal('265.4317'), Decimal('255.7904'), Decimal('248.23'), Decimal('233.8'), Decimal('229.43'), Decimal('240.57'), Decimal('239.95'), Decimal('228.58'), Decimal('245.09'), Decimal('251.45'), Decimal('258.17'), Decimal('250.77'), Decimal('246.35'), Decimal('251.82'), Decimal('252.3'), Decimal('267.25'), Decimal('269.2499'), Decimal('261.52'), Decimal('248.75'), Decimal('265.45'), Decimal('265.82'), Decimal('257.85'), Decimal('249.86'), Decimal('240.6399'), Decimal('241.5499'), Decimal('238'), Decimal('242.17'), Decimal('246.65'), Decimal('243.77'), Decimal('244.09'), Decimal('243.2599'), Decimal('236.8'), Decimal('230.33'), Decimal('223.7299'), Decimal('232.2'), Decimal('227.88'), Decimal('231.45'), Decimal('222.62'), Decimal('230.38'), Decimal('245.97'), Decimal('248.42'), Decimal('265.6899'), Decimal('271.5199'), Decimal('272.38'), Decimal('266.115')] lows=[Decimal('172.5575'), Decimal('171.760025'), Decimal('174.4375'), Decimal('176.627525'), Decimal('177.2795'), Decimal('175.84425'), Decimal('177.562525'), Decimal('185.84'), Decimal('178.2265'), Decimal('183.85875'), Decimal('189.0775'), Decimal('190.8825'), Decimal('188.94375'), Decimal('193.19'), Decimal('196.56875'), Decimal('198.6375'), Decimal('200.19'), Decimal('202.8775'), Decimal('203.5025'), Decimal('203.3205'), Decimal('197.0075'), Decimal('197.5425'), Decimal('201.8775'), Decimal('201.140025'), Decimal('197.5275'), Decimal('188.585025'), Decimal('180.73'), Decimal('178.655'), Decimal('181.64'), Decimal('187.42'), Decimal('192.76'), Decimal('192.5'), Decimal('189.14'), Decimal('187.41'), Decimal('189.95'), Decimal('193.2803'), Decimal('192.63'), Decimal('193.61'), Decimal('192.2'), Decimal('198.28'), Decimal('203.42'), Decimal('202.1'), Decimal('201.43'), Decimal('198.3454'), Decimal('194.3'), Decimal('196.2'), Decimal('198.51'), Decimal('194.53'), Decimal('192.67'), Decimal('190'), Decimal('187.62'), Decimal('199.33'), Decimal('209.5'), Decimal('215.35'), Decimal('217.22'), Decimal('217.9'), Decimal('221.67'), Decimal('225.51'), Decimal('221.2'), Decimal('223.565'), Decimal('222.945'), Decimal('222'), Decimal('225.224'), Decimal('219.77'), Decimal('221.31'), Decimal('222.7'), Decimal('218.58'), Decimal('220.86'), Decimal('219.66'), Decimal('219.27'), Decimal('218.3'), Decimal('206.62'), Decimal('209.5'), Decimal('211.96'), Decimal('218.9'), Decimal('218.61'), Decimal('213.25'), Decimal('206.51'), Decimal('204.67'), Decimal('206.88'), Decimal('202.03'), Decimal('195.55'), Decimal('198.54'), Decimal('200.8'), Decimal('209.72'), Decimal('207.75'), Decimal('205.11'), Decimal('205.28'), Decimal('207.13'), Decimal('211.2201'), Decimal('216.62'), Decimal('216.44'), Decimal('220.37'), Decimal('219.82'), Decimal('220.83'), Decimal('225.61'), Decimal('227.7'), Decimal('239.24'), Decimal('242.8201'), Decimal('245.23'), Decimal('250'), Decimal('252.27'), Decimal('258'), Decimal('262.35'), Decimal('271.18'), Decimal('294.1'), Decimal('299.07'), Decimal('299.64'), Decimal('287.78'), Decimal('297.7701'), Decimal('296.3'), Decimal('292.47'), Decimal('297.0587'), Decimal('288'), Decimal('313.21'), Decimal('319.05'), Decimal('319'), Decimal('308.8'), Decimal('309.28'), Decimal('313.5'), Decimal('320.36'), Decimal('318.6401'), Decimal('313.8'), Decimal('310.25'), Decimal('301.3'), Decimal('280.38'), Decimal('306.51'), Decimal('314.2101'), Decimal('304.28'), Decimal('298.61'), Decimal('281.16'), Decimal('272.5'), Decimal('278.38'), Decimal('280.93'), Decimal('277.6'), Decimal('271.45'), Decimal('274.01'), Decimal('284.49'), Decimal('294.3101'), Decimal('296.4'), Decimal('300.1181'), Decimal('293.66'), Decimal('295.4'), Decimal('293.3057'), Decimal('297.85'), Decimal('283.49'), Decimal('275.33'), Decimal('270.65'), Decimal('270.57'), Decimal('256.4384'), Decimal('268.39'), Decimal('276.08'), Decimal('264.98'), Decimal('262.1001'), Decimal('257.7'), Decimal('250.52'), Decimal('240.78'), Decimal('232.63'), Decimal('208.88'), Decimal('220'), Decimal('223'), Decimal('216.75'), Decimal('212.96'), Decimal('230.52'), Decimal('238.9001'), Decimal('245.5301'), Decimal('237.8'), Decimal('236.32'), Decimal('242.02'), Decimal('239.8'), Decimal('253.53'), Decimal('256'), Decimal('237.73'), Decimal('237.55'), Decimal('247.84'), Decimal('255.52'), Decimal('241.65'), Decimal('231'), Decimal('230'), Decimal('223.01'), Decimal('208.9'), Decimal('233.81'), Decimal('237.0701'), Decimal('231.32'), Decimal('234.15'), Decimal('234.69'), Decimal('224.82'), Decimal('213.3'), Decimal('206.5'), Decimal('222.47'), Decimal('218.82'), Decimal('220.46'), Decimal('211.5901'), Decimal('213.2201'), Decimal('231.72'), Decimal('239.06'), Decimal('246.24'), Decimal('259.67'), Decimal('260.72'), Decimal('255.7501')] closes=[Decimal('173.5825'), Decimal('174.25'), Decimal('178.2525'), Decimal('180.1875'), Decimal('177.885'), Decimal('178.1025'), Decimal('186.5725'), Decimal('186.3875'), Decimal('184.2725'), Decimal('188.8675'), Decimal('190.5725'), Decimal('192.055'), Decimal('190.31'), Decimal('199.85'), Decimal('200.2675'), Decimal('200.025'), Decimal('202.12'), Decimal('204.87'), Decimal('206.985'), Decimal('203.7175'), Decimal('199.0275'), Decimal('200.5025'), Decimal('205.125'), Decimal('202.5'), Decimal('198.415'), Decimal('189.6625'), Decimal('181.61'), Decimal('187.7975'), Decimal('186.12'), Decimal('194.1'), Decimal('195.94'), Decimal('195.58'), Decimal('192.94'), Decimal('192.08'), Decimal('195.03'), Decimal('196.62'), Decimal('194.99'), Decimal('197.5'), Decimal('198.15'), Decimal('202.74'), Decimal('206.37'), Decimal('203.66'), Decimal('202.95'), Decimal('199.36'), Decimal('196.99'), Decimal('199.05'), Decimal('201.88'), Decimal('199.5'), Decimal('194.58'), Decimal('190.4'), Decimal('197.98'), Decimal('208.16'), Decimal('219.58'), Decimal('217.93'), Decimal('222.13'), Decimal('220.68'), Decimal('226.36'), Decimal('226.88'), Decimal('223.85'), Decimal('224.41'), Decimal('223.96'), Decimal('228.43'), Decimal('226.62'), Decimal('223.39'), Decimal('221.77'), Decimal('224.78'), Decimal('221.52'), Decimal('222.42'), Decimal('223.41'), Decimal('222.42'), Decimal('219'), Decimal('211.13'), Decimal('212.46'), Decimal('219.41'), Decimal('224.82'), Decimal('220.81'), Decimal('216.6'), Decimal('206.99'), Decimal('205.17'), Decimal('207.16'), Decimal('207.42'), Decimal('197.32'), Decimal('204.51'), Decimal('207'), Decimal('210.75'), Decimal('208.31'), Decimal('206.95'), Decimal('206.71'), Decimal('209.39'), Decimal('217.46'), Decimal('218.62'), Decimal('222.22'), Decimal('222.9'), Decimal('221.03'), Decimal('226.92'), Decimal('227.26'), Decimal('231.66'), Decimal('247.17'), Decimal('244.51'), Decimal('249.41'), Decimal('255.67'), Decimal('258.27'), Decimal('264.01'), Decimal('265.98'), Decimal('298.01'), Decimal('297.52'), Decimal('308.04'), Decimal('306.57'), Decimal('294.59'), Decimal('303.9'), Decimal('303.9'), Decimal('300.25'), Decimal('302.03'), Decimal('292.61'), Decimal('316.75'), Decimal('329.85'), Decimal('319.56'), Decimal('317.46'), Decimal('326.74'), Decimal('315.03'), Decimal('333.76'), Decimal('326.76'), Decimal('314.35'), Decimal('321.26'), Decimal('306.93'), Decimal('300.37'), Decimal('324.27'), Decimal('318.26'), Decimal('304.9'), Decimal('301.98'), Decimal('281.61'), Decimal('283.37'), Decimal('304.59'), Decimal('283.87'), Decimal('278.01'), Decimal('277.19'), Decimal('290.75'), Decimal('294'), Decimal('296.4'), Decimal('309.45'), Decimal('303.22'), Decimal('300.01'), Decimal('295.86'), Decimal('294.11'), Decimal('301.21'), Decimal('292.9'), Decimal('276.04'), Decimal('281.78'), Decimal('272.47'), Decimal('274'), Decimal('278.17'), Decimal('279.99'), Decimal('265.75'), Decimal('269.42'), Decimal('259.03'), Decimal('250.67'), Decimal('241.5'), Decimal('233.74'), Decimal('233.72'), Decimal('223.24'), Decimal('227.72'), Decimal('219.44'), Decimal('228.4'), Decimal('244.86'), Decimal('246.38'), Decimal('252.42'), Decimal('239.48'), Decimal('243.19'), Decimal('247.28'), Decimal('251.08'), Decimal('267.05'), Decimal('258.24'), Decimal('239.49'), Decimal('242.67'), Decimal('264.95'), Decimal('265.11'), Decimal('245.07'), Decimal('236.42'), Decimal('233.9'), Decimal('223.87'), Decimal('237.48'), Decimal('241.57'), Decimal('243.85'), Decimal('234.77'), Decimal('242.2'), Decimal('237.14'), Decimal('229.36'), Decimal('213.52'), Decimal('215.14'), Decimal('230.14'), Decimal('226.58'), Decimal('221'), Decimal('213.3'), Decimal('229.73'), Decimal('244.96'), Decimal('247.66'), Decimal('264.53'), Decimal('267.34'), Decimal('265.24'), Decimal('256.34')]

lows_arr = np.array(lows, dtype=np.double) highs_arr = np.array(highs, dtype=np.double) closes_arr = np.array(closes, dtype=np.double) ta.ATR(highs_arr, lows_arr, closes_arr, timeperiod=50)

array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 6.109859 , 6.20107382, 6.31325234, 6.2717873 , 6.29595155, 6.28003252, 6.28523187, 6.25792723, 6.24776869, 6.19091331, 6.12679305, 6.16145719, 6.11354804, 6.12827708, 6.08711154, 6.05516931, 6.15526592, 6.0969606 , 6.05521939, 6.016915 , 5.9947767 , 6.12248117, 6.09503155, 6.12593092, 6.1323123 , 6.13386605, 6.16238873, 6.24094096, 6.22612214, 6.21139969, 6.2183717 , 6.33140427, 6.38797618, 6.38821666, 6.38485232, 6.34335528, 6.32688817, 6.30615041, 6.2438274 , 6.28215085, 6.21030784, 6.21550168, 6.15959165, 6.12659981, 6.12966782, 6.12087446, 6.12425497, 6.42036987, 6.45356047, 6.42428926, 6.44940348, 6.45381541, 6.5003391 , 6.48013232, 7.30392967, 7.55585108, 7.67433406, 7.99004738, 8.24464643, 8.3059535 , 8.34983443, 8.46223774, 8.42981899, 8.60302261, 9.13076216, 9.23074691, 9.59553197, 9.69962133, 9.89102891, 9.96520833, 10.14770416, 10.24714808, 10.42407312, 10.50618966, 10.69586586, 11.01294855, 11.27508957, 11.25078578, 11.38117007, 11.44234667, 11.64909973, 11.70171774, 12.00008338, 12.37348172, 12.35841208, 12.31104384, 12.40862296, 12.3816505 , 12.26581749, 12.30980114, 12.32724312, 12.31709826, 12.25415629, 12.14895917, 12.16597998, 12.34646038, 12.47613118, 12.50120655, 12.52418242, 12.63873077, 12.63115616, 12.57593303, 12.72081437, 12.66377609, 12.64490057, 12.69023655, 12.73663982, 12.79390703, 13.03642889, 13.05010031, 13.1404983 , 13.34168834, 13.38725457, 13.45330948, 13.43524129, 13.41933446, 13.44334777, 13.37508082, 13.3035792 , 13.28750762, 13.34515746, 13.34325232, 13.55218727, 13.50514352, 13.69064065, 13.62282784, 13.81957128, 13.92037986, 13.85477026, 13.94847286, 14.2515034 , 14.13367333, 14.04259786, 14.01234591, 13.93089899, 13.82367901, 13.79360543, 13.85833332, 13.92576465, 13.98844936, 13.93508037, 13.87617877, 13.81925319, 13.88606613, 13.9331448 , 13.84168191, 13.95384627, 13.91176734, 13.866732 , 13.79669536])

So TA-Lib computes the ATR-50 for 2022-03-23 as 13.80, while TV computes it as 13.78 - even when using RMA and ta.tr(false). I know it's only off by a small amount in this instance, but it seems to be more than can be explained by rounding.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

darose commented 1 year ago

What is ta.tr(false)?

It refers to an aspect of how Trading View calculates ATR, and whether that might have some responsibility for the discrepancy between TA-Lib and TV's ATR calc. (See https://github.com/TA-Lib/ta-lib-python/issues/560#issuecomment-1301845927 )

quantfreedom commented 1 year ago

I later learned how to calc that atr using tradingviews way ... so its just different calculations