我已经编写了显示 MACD 指标图的代码。我在这里做了什么:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv(#stock_chart)
df = df.rename(columns={"<DTYYYYMMDD>":"Date"})
df['Date'] = pd.to_datetime(df['Date'].astype(str), format="%Y%m%d")
print(df.head())
#seting index for graph
df = df.set_index(df['Date'].values)
print(df.head())
我的数据框的头部是:
#Macd
S_term = 12
L_term = 26
s_ema = df.close.ewm(span=S_term, adjust=False).mean()
l_ema = df.close.ewm(span=L_term, adjust=False).mean()
#calculate MACD
macd = s_ema - l_ema
signal_term = 9
signal_line = macd.ewm(span= signal_term, adjust=False).mean()
#plot the graphs
plt.figure(figsize=(100,10)) #width = 12.2in, height = 4.5
plt.plot(df.index, macd, label='macd', color = 'red')
plt.plot(df.index, signal_line, label='Signal Line', color='blue')
plt.tight_layout()
plt.xticks(rotation=0)
plt.legend(loc='upper left')
plt.show()
最后,我得到了这个:
不管怎样,问题是尽管行索引标签是这样的:2020-08-19 or "%Y%m%d"
图形标签不完整,如果我重新排序,数据就会相互重叠。我需要做什么 ?
茅侃侃
相关分类