Created
August 18, 2019 04:54
-
-
Save yuyasugano/6f0efe5c71b4a166493762c1a509f814 to your computer and use it in GitHub Desktop.
Matplotlib RSI sample
This file contains hidden or 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
def plot_RSI(df, window): | |
df1 = df.copy() | |
diff = df1.close.diff(periods=1).values | |
xdate = [x.date() for x in df1.index] | |
RSI = [] | |
for i in range(window+1, len(xdate)): | |
neg = 0 | |
pos = 0 | |
for value in diff[i-window:i+1]: | |
if value > 0: | |
pos += value | |
if value < 0: | |
neg += value | |
pos_ave = pos/window | |
neg_ave = np.abs(neg/window) | |
rsi = pos_ave/(pos_ave+neg_ave)*100 | |
RSI.append(rsi) | |
# draw RSI figure | |
plt.plot(xdate[window+1:], RSI, label = "RSI {}".format(window), lw=2.5, alpha=0.6) | |
plt.xlim(xdate[window+1], xdate[-1]) | |
plt.ylim(0,100) | |
plt.legend() | |
def RSI(df): | |
df1 = df.copy() | |
xdate = [x.date() for x in df1.index] | |
plt.figure(figsize=(15,10)) | |
# plot the original | |
plt.subplot(211) | |
plt.plot(xdate, df1.close,label="original") | |
plt.xlim(xdate[0], xdate[-1]) | |
plt.legend() | |
plt.grid() | |
# plot RSI | |
plt.subplot(212) | |
plt.grid() | |
plt.title("RSI") | |
plot_RSI(df1, window=9) | |
plot_RSI(df1, window=22) | |
plot_RSI(df1, window=42) | |
plt.fill_between(xdate, np.ones(len(xdate))*30, color="blue", alpha=0.2) | |
plt.fill_between(xdate, np.ones(len(xdate))*70, np.ones(len(xdate))*100, color="red", alpha=0.2) | |
plt.plot(xdate, np.ones(len(xdate))*30, color="blue", linestyle="dotted") | |
plt.plot(xdate, np.ones(len(xdate))*70, color="red", linestyle="dotted") | |
plt.show() | |
RSI(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment