导入包:
import warningswarnings.filterwarnings('ignore')import timeimport akshare as akimport numpy as npimport pandas as pdimport quantstats as qsimport matplotlib.pyplot as pltimport matplotlib.ticker as ticker# %matplotlib inline
获取ETF数据:
# 获取 ETF 历史数据etf_1 = ak.fund_etf_hist_em(symbol="159967", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_1"}).set_index("date")etf_2 = ak.fund_etf_hist_em(symbol="512890", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_2"}).set_index("date")# 合并数据data = pd.concat([etf_1, etf_2], axis=1).dropna().iloc[:]data.index = pd.to_datetime(data.index)
计算涨跌幅:
# 获取 ETF 历史数据etf_1 = ak.fund_etf_hist_em(symbol="159967", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_1"}).set_index("date")etf_2 = ak.fund_etf_hist_em(symbol="512890", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_2"}).set_index("date")# 合并数据data = pd.concat([etf_1, etf_2], axis=1).dropna().iloc[:]data.index = pd.to_datetime(data.index)
获得信号:
# 获取 ETF 历史数据etf_1 = ak.fund_etf_hist_em(symbol="159967", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_1"}).set_index("date")etf_2 = ak.fund_etf_hist_em(symbol="512890", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_2"}).set_index("date")# 合并数据data = pd.concat([etf_1, etf_2], axis=1).dropna().iloc[:]data.index = pd.to_datetime(data.index)
绘图:
# 获取 ETF 历史数据etf_1 = ak.fund_etf_hist_em(symbol="159967", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_1"}).set_index("date")etf_2 = ak.fund_etf_hist_em(symbol="512890", period="daily", adjust="hfq")[["日期","收盘"]] \.rename(columns={"日期":"date","收盘":"etf_2"}).set_index("date")# 合并数据data = pd.concat([etf_1, etf_2], axis=1).dropna().iloc[:]data.index = pd.to_datetime(data.index)
如果觉得《ETF动量策略》对你有帮助,请点赞、收藏,并留下你的观点哦!