-
-
Save ultragtx/6831eb04dfe9e6ff50d0f334bdcb847d to your computer and use it in GitHub Desktop.
Functions that calculate RSI and StochRSI which give the same value as Trading View. I wrote these functions as RSI and StochRSI functions from TA-Lib give different values as TV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
aldefrawy
commented
May 26, 2024
via email
If u can write in Arabic I prefer it over English 🤗
On Sun, May 26, 2024, 10:41 AM Mohammad Aldefrawy ***@***.***>
wrote:
… Thank u my friend,
Yes u can easily buy one of 2 ways, first u can copy and paste the results
in an empty file and read it in excel as csv, second u can import excel
helper library that can save the results directly to an excel file.
On Sun, May 26, 2024, 2:03 AM syedarsalan111 ***@***.***>
wrote:
> ***@***.**** commented on this gist.
> ------------------------------
>
> the code seems perfect ,
> can you please do me a favor , i wanna put that stoch RSI K/D values on
> an excel file (but only for the last month and current month)
>
> —
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/ultragtx/6831eb04dfe9e6ff50d0f334bdcb847d#gistcomment-5068821>
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AR6JFVX2OV2OKPKO5KS3CQLZEEKELBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4TGMBXHEZTINVHORZGSZ3HMVZKMY3SMVQXIZI>
> .
> You are receiving this email because you commented on the thread.
>
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
> .
>
>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment