Skip to content

Instantly share code, notes, and snippets.

@so1tsuda
Created June 20, 2018 15:49
Show Gist options
  • Save so1tsuda/5fd182f81210fd196c2d0e13e44af8c8 to your computer and use it in GitHub Desktop.
Save so1tsuda/5fd182f81210fd196c2d0e13e44af8c8 to your computer and use it in GitHub Desktop.
# Calculating RSI and StochRSI that give the same value as Trading View
# calculating RSI (gives the same values as TradingView)
# https://stackoverflow.com/questions/20526414/relative-strength-index-in-python-pandas
def 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)
# calculating Stoch RSI (gives the same values as TradingView)
# https://www.tradingview.com/wiki/Stochastic_RSI_(STOCH_RSI)
def StochRSI(series, period=14, smoothK=3, smoothD=3):
# Calculate RSI
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()
rsi = 100 - 100 / (1 + rs)
# Calculate StochRSI
stochrsi = (rsi - rsi.rolling(period).min()) / (rsi.rolling(period).max() - rsi.rolling(period).min())
stochrsi_K = stochrsi.rolling(smoothK).mean()
stochrsi_D = stochrsi_K.rolling(smoothD).mean()
return stochrsi, stochrsi_K, stochrsi_D
# calculating Stoch RSI
# -- Same as the above function but uses EMA, not SMA
def StochRSI_EMA(series, period=14, smoothK=3, smoothD=3):
# Calculate RSI
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()
rsi = 100 - 100 / (1 + rs)
# Calculate StochRSI
stochrsi = (rsi - rsi.rolling(period).min()) / (rsi.rolling(period).max() - rsi.rolling(period).min())
stochrsi_K = stochrsi.ewm(span=smoothK).mean()
stochrsi_D = stochrsi_K.ewm(span=smoothD).mean()
return stochrsi, stochrsi_K, stochrsi_D
@kitt-th
Copy link

kitt-th commented May 8, 2019

Thanks very much for this - it was bugging me that TA-LIB's stoch rsi values never matched up with trading view.
I do have . a question about the StochRSI function though as I'm still getting different results than trading view with the exact same settings of 14 period K & D=3. The RSI is exactly the same but the Stoch values of K & D are always around 2-3 points lower (on a scale of 0-100). Do you have any idea why that might be so?

Cheers

@kitt-th
Copy link

kitt-th commented May 8, 2019

hmmm I have to take that back.... everything is lined up fine now. Once again - thanks for doing this!

@kitt-th
Copy link

kitt-th commented May 8, 2019

3rd comment... sometimes it matches and sometimes not. rsi calc still spot on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment