Calculation of RSI

vishyarjun
vishyarjun edited August 2023 in Python client
Hi All,

I'm using historical data API to collect the historical data for 1 min, 5 min, day etc and attempting to calculate RSI, Divergence, Stochastic etc. However the value that I get seems slightly off from the actual kite charts.

Below is the logic I'm using. To put it in words, i follow 2 step approach one for the first 14 rows and other for the remaining as per investopedia (https://investopedia.com/terms/r/rsi.asp)


if len(df) >= rolling_window + 7: # Ensure at least 14 rows are available for the initial incomplete window
df['DIFF'] = df['CLOSE'].diff()
df['GAIN'] = np.where(df['DIFF'] > 0, df['DIFF'], 0)
df['LOSS'] = np.where(df['DIFF'] < 0, -df['DIFF'], 0)
# Calculate average gains and losses for the initial incomplete window
df['AVG_GAIN'] = df['GAIN'].rolling(window=rolling_window, min_periods=1).mean()
df['AVG_LOSS'] = df['LOSS'].rolling(window=rolling_window, min_periods=1).mean()
df['RS'] = np.where(df['AVG_LOSS'] != 0, df['AVG_GAIN'] / df['AVG_LOSS'], np.inf)
df['RS'] = np.where(df['AVG_GAIN'] != 0, df['RS'], np.finfo(float).max)
df['RSI'] = 100 - (100 / (1 + np.minimum(df['RS'], 100)))
stochastic_window = 7
df['StochRSI'] = (df['RSI'] - df['RSI'].rolling(window=stochastic_window).min()) / \
(df['RSI'].rolling(window=stochastic_window).max() - df['RSI'].rolling(window=stochastic_window).min())
# Continue calculations for the rest of the rows
df['AVG_GAIN'][rolling_window:] = df['GAIN'][rolling_window:].rolling(window=rolling_window, min_periods=1).mean()
df['AVG_LOSS'][rolling_window:] = df['LOSS'][rolling_window:].rolling(window=rolling_window, min_periods=1).mean()
df['RS'][rolling_window:] = np.where(df['AVG_LOSS'][rolling_window:] != 0, df['AVG_GAIN'][rolling_window:] / df['AVG_LOSS'][rolling_window:], np.inf)
df['RS'][rolling_window:] = np.where(df['AVG_GAIN'][rolling_window:] != 0, df['RS'][rolling_window:], np.finfo(float).max)
df['RSI'][rolling_window:] = 100 - (100 / (1 + np.minimum(df['RS'][rolling_window:], 100)))
df['StochRSI'][rolling_window:] = (df['RSI'][rolling_window:] - df['RSI'][rolling_window:].rolling(window=stochastic_window).min()) / \
(df['RSI'][rolling_window:].rolling(window=stochastic_window).max() - df['RSI'][rolling_window:].rolling(window=stochastic_window).min())


While i was testing, I realized the values I'm getting are slightly off from actual values shown in kite chart. Example INFY at 25-08-2023 03:25 PM in a 5 min chart shows 29.62 (RSA 7) while the above calculation gives me 22.40. While its not a lot, its certainly important to calculate the right values for right algorithms to execute.

I understand that this forum is not to seek help for defining trading strategy. however many developers might cross through the same path of calculating RSI etc. it will be worth using some standard calculation techniques in a common programming language.

Any help will be much appreciated.

Regards,
Arjun.
This discussion has been closed.