Created
September 28, 2020 07:29
-
-
Save yuyasugano/9bc644c7369fa9dad311f197ce5f87b3 to your computer and use it in GitHub Desktop.
Trend indicator sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
def MACD(df, s, l, signal): | |
df["macd"] = df.Close.ewm(span=s, min_periods=1).mean() - df.Close.ewm(span=l, min_periods=1).mean() | |
df["signal"] = df.macd.ewm(span=signal, min_periods=1).mean() | |
df["diff"] = df["macd"] - df["signal"] | |
xdate = [x.date() for x in df.index] | |
plt.figure(figsize=(15, 10)) | |
# plot the original closing line | |
plt.subplot(211) | |
plt.plot(xdate, df.Close, label="close") | |
plt.xlim(xdate[0], xdate[-1]) | |
plt.legend() | |
plt.grid() | |
# plot MACD and signal | |
plt.subplot(212) | |
plt.title("MACD") | |
plt.plot(xdate, df.macd, label="macd") | |
plt.plot(xdate, df.signal, label="signal") | |
plt.xlim(xdate[0], xdate[-1]) | |
plt.legend() | |
plt.grid(True) | |
# 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[0])) | |
plt.scatter(xdate[i], df.iloc[i]["macd"], 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[0])) | |
plt.scatter(xdate[i], df.iloc[i]["macd"], marker="o", s=100, color="r", alpha=0.9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment