用tushare虽然方便,但毕竟不是本机数据,可能受网速、权限等各方面的限制。前面我们有这篇文章:
通达信日线数据转换为feather格式,提高后续数据处理速度https://blog.csdn.net/bq_cui/article/details/122730357?spm=1001.2014.3001.5501介绍了如何把通达信日线数据转换为feather格式。我们这次使用转换好的feather格式数据来进行数据回测。
如果没有feather格式数据,请下载。使用feather格式后,运行速度显著提升。
运行结果:
期初资金: 100000.00
2021-01-22, Close, 7.37
2021-01-25, Close, 7.25
2021-01-26, Close, 7.21
2021-01-27, Close, 7.27
2021-01-28, Close, 7.19
2021-01-29, Close, 7.02
2021-02-01, Close, 7.08
2021-02-02, Close, 6.98
2021-02-03, Close, 6.69
2021-02-04, Close, 6.57
......
2021-12-13, 买入单, 8.67
2021-12-14, 已买入, 价格: 8.66, 费用: 8.66, 佣金 0.01
2021-12-14, Close, 8.78
2021-12-15, Close, 8.65
2021-12-16, Close, 8.42
2021-12-16, 卖出单, 8.42
2021-12-17, 已卖出, 价格: 8.40, 费用: 8.66, 佣金 0.01
2021-12-17, 交易利润, 毛利润 -0.26, 净利润 -0.28
2021-12-17, Close, 8.32
2021-12-20, Close, 8.37
2021-12-21, Close, 8.43
2021-12-22, Close, 8.47
2021-12-23, Close, 8.30
2021-12-24, Close, 8.53
2021-12-24, 买入单, 8.53
2021-12-27, 已买入, 价格: 8.53, 费用: 8.53, 佣金 0.01
2021-12-27, Close, 8.69
2021-12-28, Close, 8.61
2021-12-29, Close, 8.63
2021-12-30, Close, 8.73
2021-12-31, Close, 8.88
2022-01-04, Close, 8.95
2022-01-05, Close, 8.70
2022-01-06, Close, 8.97
2022-01-07, Close, 8.89
2022-01-10, Close, 8.84
2022-01-11, Close, 8.89
2022-01-12, Close, 9.26
2022-01-13, Close, 9.09
2022-01-14, Close, 9.17
2022-01-17, Close, 9.16
2022-01-18, Close, 8.77
2022-01-18, 卖出单, 8.77
2022-01-19, 已卖出, 价格: 8.75, 费用: 8.53, 佣金 0.01
2022-01-19, 交易利润, 毛利润 0.22, 净利润 0.20
2022-01-19, Close, 8.75
2022-01-20, Close, 8.65
2022-01-21, Close, 8.24
2022-01-24, Close, 8.10
2022-01-25, Close, 7.70
2022-01-26, Close, 7.64
2022-01-27, Close, 7.57
期末资金: 99999.28
代码:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 6 17:37:50 2022
@author: freepy
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from datetime import datetime # For datetime objects
# Import the backtrader platform
import backtrader as bt
import pandas as pd
# 创建策略继承bt.Strategy
class TestStrategy(bt.Strategy):
params = (
# 均线参数设置15天,15日均线
('maperiod', 15),
)
def log(self, txt, dt=None):
# 记录策略的执行日志
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
# 保存收盘价的引用
self.dataclose = self.datas[0].close
# 跟踪挂单
self.order = None
# 买入价格和手续费
self.buyprice = None
self.buycomm = None
# 加入均线指标
self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod)
# 订单状态通知,买入卖出都是下单
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# broker 提交/接受了,买/卖订单则什么都不做
return
# 检查一个订单是否完成
# 注意: 当资金不足时,broker会拒绝订单
if order.status in [order.Completed]:
if order.isbuy():
self.log(
'已买入, 价格: %.2f, 费用: %.2f, 佣金 %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
elif order.issell():
self.log('已卖出, 价格: %.2f, 费用: %.2f, 佣金 %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
# 记录当前交易数量
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('订单取消/保证金不足/拒绝')
# 其他状态记录为:无挂起订单
self.order = None
# 交易状态通知,一买一卖算交易
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log('交易利润, 毛利润 %.2f, 净利润 %.2f' %
(trade.pnl, trade.pnlcomm))
def next(self):
# 记录收盘价
self.log('Close, %.2f' % self.dataclose[0])
# 如果有订单正在挂起,不操作
if self.order:
return
# 如果没有持仓则买入
if not self.position:
# 今天的收盘价在均线价格之上
if self.dataclose[0] > self.sma[0]:
# 买入
self.log('买入单, %.2f' % self.dataclose[0])
# 跟踪订单避免重复
self.order = self.buy()
else:
# 如果已经持仓,收盘价在均线价格之下
if self.dataclose[0] < self.sma[0]:
# 全部卖出
self.log('卖出单, %.2f' % self.dataclose[0])
# 跟踪订单避免重复
self.order = self.sell()
start = '2020-01-01'
end = '2022-01-31'
def get_data(code, start_date, end_date):
df_tdx = pd.read_feather(r'./dataout/tdx/'+code+r'.day.feather')
df_tdx.index=pd.to_datetime(df_tdx.date, format = '%Y%m%d')
df_tdx_b=df_tdx.truncate(before=start_date, after = end_date)
df_tdx_b['openinterest']=0
df_tdx_b.rename(columns={'vol':'volume'}, inplace = True)
df_tdx_b=df_tdx_b[['open','high','low','close','volume','openinterest']]
return df_tdx_b
dataframe=get_data('sh600851', datetime.strptime(start,'%Y-%m-%d'), datetime.strptime(end,'%Y-%m-%d'))
if __name__ == '__main__':
# 初始化cerebro回测系统设置
cerebro = bt.Cerebro()
# 取得股票历史数据
data = bt.feeds.PandasData(dataname=dataframe, fromdate = datetime.strptime(start,'%Y-%m-%d'), todate = datetime.strptime(end,'%Y-%m-%d'))
# 为Cerebro引擎添加策略
cerebro.addstrategy(TestStrategy)
# 加载交易数据
cerebro.adddata(data)
# 设置投资金额
cerebro.broker.setcash(100000.0)
# 设置佣金为0.001,除以100去掉%号
cerebro.broker.setcommission(commission=0.001)
#获取回测开始时的总资金
print('期初资金: %.2f' % cerebro.broker.getvalue())
#运行回测系统
cerebro.run()
#获取回测结束后的总资金
print('期末资金: %.2f' % cerebro.broker.getvalue())