# RSI calculation

Hi,
I am trying to calculate RSI and match values with Kite chart but it is always off. Please check this python snippet for get RSI(14) for INFY(408065) on 20 August, 10:14 AM.

from kiteconnect import KiteConnect
import numpy as np
from datetime import datetime
from utility.config import ACCESS_TOKEN, API_KEY

def get_rsi(instrument_token):
k = KiteConnect(api_key=API_KEY)
k.set_access_token(ACCESS_TOKEN)
history = k.historical_data(instrument_token,
datetime(2021, 8, 20, 10, 0, 0),
datetime(2021, 8, 20, 10, 14, 0),
"minute")
close_list = [i['close'] for i in history]
print(len(close_list))
gain_list = list()
loss_list = list()
for index, i in enumerate(close_list):
if index > 0:
change = i - close_list[index - 1]
if change > 0:
gain_list.append(change)
loss_list.append(0)
elif change < 0:
gain_list.append(0)
loss_list.append(abs(change))
else:
gain_list.append(0)
loss_list.append(0)
else:
gain_list.append(0)
loss_list.append(0)

rs = np.mean(gain_list) / np.mean(loss_list)
rsi = 100 - (100 / (1 + rs))
return rsi

print(get_rsi(408065))

Explanation:
1. Fetch candle data with interval of 1 minute from 10:00 to 10:14.
2. Make a list of close prices, gain prices and loss prices
3. Calculate RS by dividing the mean of gain_prices and loss_prices and then feed to RSI formula.

Seems to be straight forward formula but result is off.
According to Kite it should be 47.64 but I am getting 52.5210. Where am I doing wrong?
Tagged:
• from kiteconnect import KiteConnect
import numpy as np
from datetime import datetime
from utility.config import ACCESS_TOKEN, API_KEY

def get_rsi(instrument_token):
k = KiteConnect(api_key=API_KEY)
k.set_access_token(ACCESS_TOKEN)
history = k.historical_data(instrument_token,
datetime(2021, 8, 20, 10, 0, 0),
datetime(2021, 8, 20, 10, 14, 0),
"minute")
close_list = [i['close'] for i in history]
print(len(close_list))
gain_list = list()
loss_list = list()
for index, i in enumerate(close_list):
if index > 0:
change = i - close_list[index - 1]
if change > 0:
gain_list.append(change)
loss_list.append(0)
elif change < 0:
gain_list.append(0)
loss_list.append(abs(change))
else:
gain_list.append(0)
loss_list.append(0)
else:
gain_list.append(0)
loss_list.append(0)

rs = np.mean(gain_list) / np.mean(loss_list)
rsi = 100 - (100 / (1 + rs))
return rsi

print(get_rsi(408065))
• Nevermind, I got RSI using pandas which is matching kite value
• I am facing the same issue.
How did you resolve it using pandas?