Calculated RSI value not match with the value displaying in kite web app

AvinashKrishnappa
Hi,

I have subscribed to historical data api and I am trying to calculate RSI value for list of stock at EOD.
I wrote the below piece of code to calculate RSI value.

def RSI(df, base, period , script_name,xls_row):
"""
Function to compute Relative Strength Index (RSI)

Args :
df : Pandas DataFrame which contains ['date', 'open', 'high', 'low', 'close', 'volume'] columns
base : String indicating the column name from which the MACD needs to be computed from (Default Close)
period : Integer indicates the period of computation in terms of number of candles

Returns :
df : Pandas DataFrame with new columns added for
Relative Strength Index (RSI_$period)
"""

col_name = 'RSI_' + str(period)
delta = df[base].diff()
up, down = delta.copy(), delta.copy()

up[up < 0] = 0
down[down > 0] = 0

rUp = up.ewm(com=period - 1, adjust=False).mean()
rDown = down.ewm(com=period - 1, adjust=False).mean().abs()

df[col_name] = 100 - 100 / (1 + rUp / rDown)
df[col_name].fillna(0, inplace=True)

return df

I checked for few stocks and below is my observation:
For "ABBOTINDIA"
period: 14 , interval = "day" and RSI is calucalted on "close" price.
RSI from kite app = 39.72 on 18/11/2020 closing basis
RSI calculated from above formula = 28.29 on 18/11/2020.
Please let me know the formula which I am using above is correct for calculating RSI?

Thanks in advance.
  • madankumar
    please le me know if you found any solution to this RSI issue
  • dinezh
    dinezh edited November 20
    @AvinashKrishnappa @madankumar

    This is the best method to calculate rsi
    This method will give you RSI values which matches TRADINGVIEW RSI values.

    def calc_RSI(series, period=14):
    delta = series.diff().dropna()
    ups = delta * 0
    downs = ups.copy()
    ups[delta > 0] = delta[delta > 0]
    downs[delta < 0] = -delta[delta < 0]
    ups[ups.index[period-1]] = np.mean( ups[:period] ) #first value is sum of avg gains
    ups = ups.drop(ups.index[:(period-1)])
    downs[downs.index[period-1]] = np.mean( downs[:period] ) #first value is sum of avg losses
    downs = downs.drop(downs.index[:(period-1)])
    rs = ups.ewm(com=period-1,min_periods=0,adjust=False,ignore_na=False).mean() / \
    downs.ewm(com=period-1,min_periods=0,adjust=False,ignore_na=False).mean()
    return 100 - 100 / (1 + rs)


    df['RSI'] = calc_RSI(df['close'])
    print(df)

Sign In or Register to comment.