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

RSI value getting from ta-lib.RSI function is not matching to RSI value posted on financial sites.

Open sum-itverma opened this issue 5 years ago • 4 comments

Hi, I calculate RSI value for stock using ta-lib.RSI method, but result is not matching with values posted on financial websites. I calculate RSI value based on close price and High price. but result is same.

Actual: RSI value is Expected: RSI value get from ta-lib.RSI method should be same as other financial sites post.

Not sure whether its library issue or not. Please help me to get answer for this.

Financial asset=> NSE: NIFTY50 Actual value calculated with RSI function=> 47 Expected value, which other financial websites are showing is => 68

sum-itverma avatar Oct 07 '20 17:10 sum-itverma

Perhaps you can provide more information, which financial asset you are looking at, how different or similar the values are, etc.

mrjbq7 avatar Oct 10 '20 15:10 mrjbq7

I have updated financial asset information and rsi values what is expect and what is getting.

sum-itverma avatar Oct 11 '20 09:10 sum-itverma

Are they calculated over the same interval?

Do you have a test case showing what the prices are that you are using as an input?

I'd love to help, but without more information, it's difficult for me.

The RSI calculation allows for three different approaches to how the average ups and downs are calculated:

  • Simple Moving Average
  • Exponential Moving Average
  • Wilder’s Smoothing Method

You can see more information about the different approaches here:

https://www.macroption.com/rsi-calculation/

It might be that the website you are looking at uses a slightly different definition.

Here's a Simple Moving Average version:

import pandas as pd
import numpy as np

def RS(c, n):
    c = pd.Series(c)
    diffs = c.rolling(2).apply(lambda x: x.iloc[-1] - x.iloc[0])
    ups = diffs.rolling(n).apply(lambda x: np.sum(x[x > 0]))
    downs = diffs.rolling(n).apply(lambda x: -np.sum(x[x < 0]))
    rs = ups / downs
    rs[downs == 0] = 0
    return rs

def RSI(c, n):
    rs = RS(c, n)
    rsi = 100 - (100 / (1 + rs))
    return rsi

Maybe you can build the other two and see which that website is using...

mrjbq7 avatar Oct 11 '20 15:10 mrjbq7

Hi, Thankyou for your prompt reply. I tried rsi calculation as per share link. But did not get expected result. expected value should be as per this link https://www.google.com/search?client=firefox-b-d&q=HDFC+BANK+RSI

Following are the updated method. Please let me know if something wrong in method.

  • Exponential Moving Average method

def RS(c, n): alpha = 2/(n+1) c = pd.Series(c) diffs = c.rolling(2).apply(lambda x: x.iloc[-1] - x.iloc[0]) Ut = diffs.rolling(n).apply(lambda x: np.sum(x[x > 0])) Dt = diffs.rolling(n).apply(lambda x: -np.sum(x[x < 0])) Ut_1 = diffs.rolling(n-1).apply(lambda x: np.sum(x[x > 0])) AvgUt_1 = (Ut_1)/(n-1) Dt_1 = diffs.rolling(n-1).apply(lambda x: -np.sum(x[x < 0])) AvgDt_1 = (Dt_1)/(n-1) AvgUt = alpha*Ut + (1-alpha)*AvgUt_1 AvgDt = alpha*Dt + (1-alpha)*AvgDt_1 rs = AvgUt/AvgDt rs[Dt == 0] = 0 rsi = 100 - (100 / (1 + rs)) return rsi

  • Wilder’s Smoothing Method

def RS(c, n): alpha = 1/n c = pd.Series(c) diffs = c.rolling(2).apply(lambda x: x.iloc[-1] - x.iloc[0]) Ut = diffs.rolling(n).apply(lambda x: np.sum(x[x > 0])) Dt = diffs.rolling(n).apply(lambda x: -np.sum(x[x < 0])) Ut_1 = diffs.rolling(n-1).apply(lambda x: np.sum(x[x > 0])) AvgUt_1 = (Ut_1)/(n-1) Dt_1 = diffs.rolling(n-1).apply(lambda x: -np.sum(x[x < 0])) AvgDt_1 = (Dt_1)/(n-1) AvgUt = alpha*Ut + (1-alpha)*AvgUt_1 AvgDt = alpha*Dt + (1-alpha)*AvgDt_1 rs = AvgUt/AvgDt rs[Dt == 0] = 0 rsi = 100 - (100 / (1 + rs)) return rsi

Following is experiment result.

RSI algorithm Price candle HDFC Bank RSI
SMA Close 75.052411
EMA Close 74.77
Wilder's Close 74.63
SMA Average price 84.073775
EMA Average price 83.709473
Wilder's Average price 83.520999
SMA Open 73.853211
EMA Open 73.94819
Wilder's Open 73.995772
SMA Low 81.956192
EMA Low 81.501519
Wilder's Low 81.265481

sum-itverma avatar Oct 18 '20 13:10 sum-itverma