python 回测设置
程序员文章站
2022-07-13 15:38:53
...
本程序是关于回测,策略使用上章择时选股策略,
例程代码
# N日突破择时策略
import pandas_datareader.data as web
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
cash_hold = 100000 #初始资金
posit_num = 0 #持股数目
market_total = 0 #持股市值
skip_days = 0 #持股/持币状态
#股票数据获取及处理接口
def GetStockDatApi(stockName=None,stockTimeS=None,stockTimeE=None, N1=15,N2=5):
stockdata = web.DataReader(stockName, "yahoo", stockTimeS, stockTimeE)
stockdata['N1_High'] = stockdata.High.rolling(window=N1).max()#计算最近N1个交易日最高价
# expanding 从最开始到当前的最大值
expan_max = stockdata.Close.expanding().max()
stockdata['N1_High'].fillna(value=expan_max,inplace=True)#目前出现过的最大值填充前N1个nan
stockdata['N2_Low'] = stockdata.Low.rolling(window=N2).min()#计算最近N2个交易日最低价
expan_min = stockdata.Close.expanding().min()
stockdata['N2_Low'].fillna(value=expan_min,inplace=True)#目前出现过的最小值填充前N2个nan
#收盘价超过N1最高价 买入股票持有
buy_index = stockdata[stockdata.Close > stockdata.N1_High.shift(1)].index
stockdata.loc[buy_index,'signal'] = 1
#收盘价超过N2最低价 卖出股票持有
sell_index = stockdata[stockdata.Close < stockdata.N2_Low.shift(1)].index
stockdata.loc[sell_index,'signal'] = 0
stockdata['signal'].fillna(method = 'ffill',inplace = True)
stockdata['signal'] = stockdata.signal.shift(1)
stockdata['signal'].fillna(method = 'bfill',inplace = True)
return stockdata
# N日突破买卖信号区间显示
skip_days = 0
df_stockload = GetStockDatApi("600410.SS",datetime.datetime(2018, 10, 1), datetime.datetime(2019, 4, 1))
print(df_stockload)
df_stockload.Close.plot()
for kl_index, today in df_stockload.iterrows():
for kl_index, today in df_stockload.iterrows():
# 遍历signal 买入/卖出执行代码
# 买入/卖出执行代码
if today.signal == 1 and skip_days == 0: # 买入
start = df_stockload.index.get_loc(kl_index)
skip_days = -1
posit_num = int(cash_hold / today.Close) # 资金转化为股票
cash_hold = 0
elif today.signal == 0 and skip_days == -1: # 卖出 避免未买先卖
end = df_stockload.index.get_loc(kl_index)
skip_days = 0
cash_hold = int(posit_num * today.Close) # 股票转化为资金
market_total = 0
if skip_days == -1: # 持股
market_total = int(posit_num * today.Close)
df_stockload.loc[kl_index, 'total'] = market_total
else: # 空仓
df_stockload.loc[kl_index, 'total'] = cash_hold
print(df_stockload.total)
df_stockload.total.plot(grid=True)
plt.show()
"""
#买/卖时间
buy: 2018-11-07 00:00:00
sell: 2018-11-26 00:00:00
buy: 2019-01-17 00:00:00
sell: 2019-01-22 00:00:00
buy: 2019-02-19 00:00:00
"""
输出结果
下一篇: jq 自定义video视频进度条