Skip to content

Instantly share code, notes, and snippets.

@nikkoxgonzales
Created July 31, 2021 18:49
Show Gist options
  • Save nikkoxgonzales/cdf8e32e46fffb5c00685492bd16b992 to your computer and use it in GitHub Desktop.
Save nikkoxgonzales/cdf8e32e46fffb5c00685492bd16b992 to your computer and use it in GitHub Desktop.
TradingView Stochastic RSI implementation using python since TA-Lib fails to do this.
# Credits to this wrong code: https://gist.github.com/mouchh/b236f6a3a75b6a216b10e6fc44cf090f
import talib
import pandas as pd
k = 3
d = 3
period = 14
rsi = talib.RSI(np_array_closes, period)
df = pd.DataFrame(rsi)
stochastic_rsi = 100 * (df - df.rolling(period).min()) / (df.rolling(period).max() - df.rolling(period).min())
K = stochastic_rsi.rolling(k).mean()
D = K.rolling(d).mean().iloc[-1].iloc[0]
K = K.iloc[-1].iloc[0]
print(f"K: {K} | D: {D}")
# The more data you have the accurate this will be with the tradingview graphs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment