量化交易第二节课:低卖高卖策略回测
2022-07-20 16:04:43
299次阅读
0个评论
最后修改时间:2022-07-20 16:05:59
1.下载股票数据
from pandas_datareader import data as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
start_date ='2020-01-01'
end_date='2020-03-20'
zgpa = dt.DataReader('601318.ss','yahoo',start_date,end_date)
zgpa.head()
2.处理数据
#print(zgpa.index)
zgpa_signal=pd.DataFrame(index=zgpa.index)
zgpa_signal['price']=zgpa['Adj Close']
zgpa_signal['diff']=zgpa_signal['price'].diff()
zgpa_signal=zgpa_signal.fillna(0.0)
zgpa_signal['signal']=np.where(zgpa_signal['diff']>=0,0,1)
zgpa_signal['order']=zgpa_signal['signal'].diff()*100
zgpa_signal.head()
3.回测数据可视化
intial_cash =20000
zgpa_signal['stock']=zgpa_signal['order']*zgpa_signal['price']
#用启动资金减去现金流变化的累加,就是小瓦剩余的价值
zgpa_signal['cash']=intial_cash-(zgpa_signal['order'].diff()*zgpa_signal['price']).cumsum()
zgpa_signal['total'] =zgpa_signal['cash']+zgpa_signal['stock']
#zgpa_signal.head(90)
plt.figure(figsize=(10,6))
plt.plot(zgpa_signal['total'],label='total value')
plt.plot(zgpa_signal['order'].cumsum()*zgpa_signal['price'],'--',label='stock value')
plt.grid()
plt.legend(loc='center right')
plt.show()
4.结论
从图中可以看到1月中旬到2月中旬,总资产也有一定的增长,但是涨幅也不明显。资产到最后还是处于大幅缩水状态。当然回测过程中没有加入交易成本和税费等。如果考虑这些进去,那算出来的是净利润或者净损失。
0
0