Skip to content

Instantly share code, notes, and snippets.

@jsrimr
Created September 6, 2019 04:52
Show Gist options
  • Save jsrimr/7d0734704a6472671332d345ac207e85 to your computer and use it in GitHub Desktop.
Save jsrimr/7d0734704a6472671332d345ac207e85 to your computer and use it in GitHub Desktop.
def get_stochastic(df, n=15, m=5, t=3):
# highest price during n days
ndays_high = df.h.rolling(window=n, min_periods=1).max()
# lowest price during n days
ndays_low = df.l.rolling(window=n, min_periods=1).min()
# Fast%K
kdj_k = ((df.c - ndays_low) / (ndays_high - ndays_low))
# Fast%D (=Slow%K)
kdj_d = kdj_k.ewm(span=m).mean()
# Slow%D
kdj_j = kdj_d.ewm(span=t).mean()
return kdj_j.mean()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment