-
-
Save ultragtx/6831eb04dfe9e6ff50d0f334bdcb847d to your computer and use it in GitHub Desktop.
# 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 | |
Further everything I have read about dropna() says it is a pandas function, pandas is built on numpy but the functions are specific to pandas
This works for that line of code
'r, t_1, t_2 = StochRSI(pd.DataFrame(numpy.array(chistory[sym])))' also works as a pd.Series()
Did you remove from input: all rows with timestamps who do not have any olhvc value ?
What should we pass in the series? The OHLC data or just open data?
You need to pass closing series (OHLC 'Close')
I was getting "NaN" as the output when passing in a list of numbers into the StochRSI function until I added the 'min_periods=0' argument to the 'rolling' functions at the bottom. Did anyone else have this problem?
after careful review of ku and binance api candle data i noticed trading view close values , for btc, are 100 to 200 off from both exchange API candle closes vs what TV charts say for the same UTC candles close price....manipulation to give edge against us and make the indicators relying on close values wrong or make the charts give basic traders the wrong idea? ...
You are awesome , this the only code that i had found similar to Trading view.
Thanks
This is how I am calling the function
'r, t_1, t_2 = StochRSI(numpy.array(chistory[sym]))'
Where chistory contains the closing prices from the 1m chart for whatever ticker symbol is being iterated.
the I will put the .diff().dropna() line back in and run a breakpoint to see if I can get some more info.