Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created August 31, 2019 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/644c0ddc29c6437e0e8a6a39cef29b16 to your computer and use it in GitHub Desktop.
Save yuyasugano/644c0ddc29c6437e0e8a6a39cef29b16 to your computer and use it in GitHub Desktop.
Matplotlib Moving Average sample wih TaLib
def SMA_TaLib(df):
df1 = df.copy()
df1["ma5"] = talib.SMA(df1['close'], timeperiod=5)
df1["ma15"] = talib.SMA(df1['close'], timeperiod=15)
df1["diff"] = df1.ma5 - df1.ma15
df1["unixtime"] = [datetime.timestamp(t) for t in df1.index]
# line and Moving Average
fig = plt.figure(figsize=(15,5))
ax = fig.add_subplot(1, 1, 1)
ax.set_title('btc/jpy by bitbank.cc API')
ax.plot(df1.index, df1.close,label="original")
ax.plot(df1.index, df1.ma15,label="15minutes")
ax.plot(df1.index, df1.ma5,label="5minutes")
ax.set_xlim(df1.index[0], df1.index[-1])
locator = mdates.AutoDateLocator()
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.grid()
# Cross points
for i in range(1, len(df1)):
if df1.iloc[i-1]["diff"] < 0 and df1.iloc[i]["diff"] > 0:
print("{}:GOLDEN CROSS".format(df1.index[i]))
ax.scatter(df1.index[i], df1.iloc[i]["ma5"], marker="o", s=100, color="b")
ax.scatter(df1.index[i], df1.iloc[i]["close"], marker="o", s=50, color="b", alpha=0.5)
if df1.iloc[i-1]["diff"] > 0 and df1.iloc[i]["diff"] < 0:
print("{}:DEAD CROSS".format(df1.index[i]))
ax.scatter(df1.index[i], df1.iloc[i]["ma5"], marker="o", s=100, color="r")
ax.scatter(df1.index[i], df1.iloc[i]["close"], marker="o", s=50, color="r", alpha=0.5)
ax.legend()
SMA_TaLib(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment