Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 28, 2020 07:06
Show Gist options
  • Save yuyasugano/2280de76a047256b51463e59fc7a65fb to your computer and use it in GitHub Desktop.
Save yuyasugano/2280de76a047256b51463e59fc7a65fb to your computer and use it in GitHub Desktop.
Trend indicator sample
import matplotlib.pyplot as plt
def SMA(df, s, l):
df["sma"] = df.Close.rolling(window=s).mean()
df["lma"] = df.Close.rolling(window=l).mean()
df["diff"] = df.sma - df.lma
xdate = [x.date() for x in df.index]
plt.figure(figsize=(15, 5))
plt.plot(xdate, df.Close, label="close")
plt.plot(xdate, df.sma,label="short")
plt.plot(xdate, df.lma,label="long")
plt.xlim(xdate[0], xdate[-1])
plt.grid()
# Cross points
for i in range(1, len(df)):
if df.iloc[i-1]["diff"] < 0 and df.iloc[i]["diff"] > 0:
print("{}:GOLDEN CROSS".format(xdate[i]))
plt.scatter(xdate[i], df.iloc[i]["sma"], marker="o", s=100, color="b", alpha=0.9)
if df.iloc[i-1]["diff"] > 0 and df.iloc[i]["diff"] < 0:
print("{}:DEAD CROSS".format(xdate[i]))
plt.scatter(xdate[i], df.iloc[i]["sma"], marker="o", s=100, color="r", alpha=0.9)
plt.legend()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment