Skip to content

Instantly share code, notes, and snippets.

@yongghongg
Last active July 24, 2022 11:07
Show Gist options
  • Save yongghongg/cd305d8e7fbbed06b84ea6786df5bae5 to your computer and use it in GitHub Desktop.
Save yongghongg/cd305d8e7fbbed06b84ea6786df5bae5 to your computer and use it in GitHub Desktop.
# Stochastic
def Stochastic(df, window, smooth_window):
stochastic = pd.DataFrame()
stochastic['%K'] = ((df['Close'] - df['Low'].rolling(window).min()) \
/ (df['High'].rolling(window).max() - df['Low'].rolling(window).min())) * 100
stochastic['%D'] = stochastic['%K'].rolling(smooth_window).mean()
stochastic['%SD'] = stochastic['%D'].rolling(smooth_window).mean()
stochastic['UL'] = 80
stochastic['DL'] = 20
return stochastic
stochastic = Stochastic(df, 14, 3)
stochastic_plot = [
mpf.make_addplot((stochastic[['%K', '%D', '%SD', 'UL', 'DL']]), ylim=[0, 100], panel=2, ylabel='Stoch')
]
mpf.plot(df, type='candle', volume=True, addplot=stochastic_plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment