ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

ATR differences with TradingView

Open 2W-12 opened this issue 4 years ago • 7 comments

Let's take weekly BINANCE:BTCUSDT indicator since beginning (14.08.2017) and do ATR with period=7 and RMA with period=7.

Python results:

import pandas as pd
import talib as ta

df = pd.DataFrame({'datetime': {0: '2017-08-14 00:00:00', 1: '2017-08-21 00:00:00', 2: '2017-08-28 00:00:00', 3: '2017-09-04 00:00:00', 4: '2017-09-11 00:00:00', 5: '2017-09-18 00:00:00', 6: '2017-09-25 00:00:00', 7: '2017-10-02 00:00:00', 8: '2017-10-09 00:00:00', 9: '2017-10-16 00:00:00', 10: '2017-10-23 00:00:00', 11: '2017-10-30 00:00:00', 12: '2017-11-06 00:00:00', 13: '2017-11-13 00:00:00'}, 'open': {0: 4261.48, 1: 4069.13, 2: 4310.01, 3: 4505.0, 4: 4153.62, 5: 3690.0, 6: 3660.02, 7: 4400.0, 8: 4640.0, 9: 5710.0, 10: 5975.0, 11: 6133.01, 12: 7345.1, 13: 5839.94}, 'high': {0: 4485.39, 1: 4453.91, 2: 4939.19, 3: 4788.59, 4: 4394.59, 5: 4123.2, 6: 4406.52, 7: 4658.0, 8: 5922.3, 9: 6171.0, 10: 6189.88, 11: 7590.25, 12: 7770.02, 13: 8123.15}, 'low': {0: 3850.0, 1: 3400.0, 2: 4124.54, 3: 3603.0, 4: 2817.0, 5: 3505.55, 6: 3653.69, 7: 4110.0, 8: 4550.0, 9: 5037.95, 10: 5286.98, 11: 6030.0, 12: 5325.01, 13: 5699.99}, 'close': {0: 4086.29, 1: 4310.01, 2: 4509.08, 3: 4130.37, 4: 3699.99, 5: 3660.02, 6: 4378.48, 7: 4640.0, 8: 5709.99, 9: 5950.02, 10: 6169.98, 11: 7345.01, 12: 5811.03, 13: 8038.0}})
atr = ta.ATR(df.high, df.low, df.close, timeperiod=7)

print(atr)
0             NaN
1             NaN
2             NaN
3             NaN
4             NaN
5             NaN
6             NaN
7      935.745714
8      998.110612
9     1017.387668
10    1001.032287
11    1080.920531
12    1275.790455
13    1439.700390

Now compare to TradingView built-in ATR (its using RMA) Here is pine script

//@version=4
study("My Script")
plot(atr(7), title="ATR")

TradingView results are: ATR: NaN NaN NaN NaN NaN NaN NaN 948.2300000000000 891.0542857142860 959.8036734693880 984.5531486880470 972.8884131611830 1056.797211281010 1255.1133239551500

Anyone had same issue ?

2W-12 avatar Jan 07 '21 19:01 2W-12

I assume most of the difference is RMA which is different than TA-Lib uses:

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

mrjbq7 avatar Jan 07 '21 19:01 mrjbq7

I assume most of the difference is RMA which is different than TA-Lib uses:

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

Any ideas how to do same RMA as TradingView using pandas/numpy ?

df.ewm(alpha=1).mean() didn't really help me :(

2W-12 avatar Jan 07 '21 19:01 2W-12

This might help you:

https://stackoverflow.com/questions/48055618/how-tradingview-pine-script-rma-function-works-internally

mrjbq7 avatar Jan 07 '21 19:01 mrjbq7

This might help you:

https://stackoverflow.com/questions/48055618/how-tradingview-pine-script-rma-function-works-internally

Not really helping. TradingView using formula as mentioned there (https://www.tradingview.com/pine-script-reference/v4/#fun_rma)

pine_rma(src, length) =>
	alpha = 1/length
	sum = 0.0
	sum := na(sum[1]) ? sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])
plot(pine_rma(close, 15))

It's exactly same

2W-12 avatar Jan 07 '21 19:01 2W-12

Same as what?

Try building a pure python version and then see what's different.

mrjbq7 avatar Jan 07 '21 19:01 mrjbq7

Link u gave me it's same code in link i gave in initial post.

I wish to do it pythonic way, i tried almost everything, but can't achieve results and my knowledge is not enough in pandas/numpy, otherwise I would propose fix myself. If anyone can help will be great.

2W-12 avatar Jan 07 '21 20:01 2W-12

You can follow this logic: Actually, you should first calculate the True Range (talib.TRANGE) and then apply the appropriate smoothing function:

  • You could see the aforementioned logic in Tradingview's implementation of ATR
indicator(title="Average True Range", shorttitle="ATR", overlay=false, timeframe="", timeframe_gaps=true)
length = input.int(title="Length", defval=14, minval=1)
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)
plot(ma_function(ta.tr(true), length), title = "ATR", color=color.new(#B71C1C, 0))

  • Pythonic Implementation:

def smoothing(prices: pd.Series, smoothing_type, smoothing_period) -> pd.Series:
        if smoothing_type == 'sma':
            return talib.SMA(prices, smoothing_period)
        elif smoothing_type == 'ema':
            return talib.EMA(prices, smoothing_period)
        elif smoothing_type == 'rma':
            return pandas_ta.rma(prices, smoothing_period)
        elif smoothing_type is None:
            return prices
    
def run() -> pd.Series:
    atr = smoothing(talib.TRANGE(
        high,
        low,
        close,
    ), "rma", 14)
    return atr

Katebsaber avatar Apr 24 '22 11:04 Katebsaber