导入包:
import warnings
warnings.filterwarnings('ignore')
import time
import akshare as ak
import numpy as np
import pandas as pd
import quantstats as qs
import matplotlib.pyplot as plt
import 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)