Back to Community
talib.BBANDS working correctly, but talib.rsi not working as expected

I am using two indicators (BBANDS, RSI) from talib library.
When I double-checked the values from my codes and the values in tradingview, I found that the output of rsi is different from the value of rsi in tradingview.

My codes are like this:

closes = # Candle data I've prepared  
upper, middle, lower = talib.BBANDS(closes, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)  
rsi = talib.RSI(closes, timeperiod=14)

print(middle[-1])  
print(rsi[-1])  

The values of BBANDS from my codes are exactly the same to the ones in tradingview.
However, the RSI value is a little different from the rsi value in tradingview.

I set the same parameters on tradingview as well.
My tradingview parameters for RSI was length=14.

I don't what makes this issue. Could you help me a little?
Thanks.

1 response

I hope this will help to understand original Welles Wilder RSI calculation.

              100  
RSI = 100 - --------  
             1 + RS

RS = Average Gain / Average Loss

To simplify the calculation explanation, RSI has been broken down into its basic components: RS, Average Gain and Average Loss.
This RSI calculation is based on 14 periods, which is the default suggested by Wilder in his book. Losses are expressed as positive values, not negative values.

The very first calculations for average gain and average loss are simple 14-period averages.

First Average Gain = Sum of Gains over the past 14 periods / 14.  
First Average Loss = Sum of Losses over the past 14 periods / 14

The second, and subsequent, calculations are based on the prior averages and the current gain loss:

Average Gain = [(previous Average Gain) x 13 + current Gain] / 14.  
Average Loss = [(previous Average Loss) x 13 + current Loss] / 14.

Taking the prior value plus the current value is a smoothing technique similar to that used in calculating an exponential moving average which in terms of Digital Signal Processing is Infinite Impulse Response Filter(IIR). This also means that RSI values become more accurate as the calculation period extends. To exactly replicate RSI numbers, a formula will need at least 250 data points, you should use the same data and calculate at the same time of the day.