大体了解了Backtrader的运行原理后,尝试把一个通达信的交易策略改编为backtrader策略,并测试一下交易时间是否与通达信中出现的交易时间一致。如果一致,说明改写的策略有效。
通达信中有这样一个简单的“MA交易”指标:
可以在通达信主界面按ctrl+F——F3查找“交易”找到这个指标。
指标中这两行:
MA1:=MA(CLOSE,SHORT);
MA2:=MA(CLOSE,LONG);
修改为Backtrader代码:
# 收盘价的 para_short 日简单移动平均
self.ma1 = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.para_short)
# 收盘价的 para_long 日简单移动平均
self.ma2 = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.para_long)
通达信这两行:
平空开多:=CROSS(MA1,MA2);
平多开空:=CROSS(MA2,MA1);
直接改为Backtrader的交易条件。
根据这个通达信交易指标,制作一个简易附图指标,可以显示出买入卖出点,便于我们与backtrader回测结果进行对比。
MA1:=MA(CLOSE,SHORT);
MA2:=MA(CLOSE,LONG);
CROSS(MA1,MA2);
CROSS(MA2,MA1);
附图中可以看到买卖日期。
这个策略在Backtrader中实现后,输出运行结果:
期初资金: 100000.00
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-02-05, Close, 6.73
2021-02-08, Close, 6.69
2021-02-09, Close, 6.83
2021-02-10, Close, 6.97
2021-02-18, Close, 7.11
2021-02-19, Close, 7.22
2021-02-22, Close, 7.15
2021-02-23, Close, 7.13
2021-02-23, 买入单, 7.13
2021-02-24, 已买入, 价格: 7.13, 费用: 7.13, 佣金 0.01
2021-02-24, Close, 7.11
2021-02-25, Close, 7.12
2021-02-26, Close, 7.36
2021-03-01, Close, 7.48
2021-03-02, Close, 7.33
2021-03-03, Close, 7.32
2021-03-04, Close, 7.39
2021-03-05, Close, 7.50
2021-03-08, Close, 7.48
2021-03-09, Close, 7.42
2021-03-10, Close, 7.40
2021-03-11, Close, 7.39
2021-03-12, Close, 7.65
2021-03-15, Close, 7.61
2021-03-16, Close, 7.86
2021-03-17, Close, 8.13
2021-03-18, Close, 7.97
2021-03-19, Close, 7.92
2021-03-22, Close, 7.82
2021-03-23, Close, 7.74
2021-03-24, Close, 7.72
2021-03-25, Close, 7.68
2021-03-26, Close, 7.77
2021-03-29, Close, 7.83
2021-03-30, Close, 8.07
2021-03-31, Close, 7.92
2021-04-01, Close, 8.02
2021-04-02, Close, 8.00
2021-04-06, Close, 8.04
2021-04-07, Close, 7.95
2021-04-08, Close, 7.91
2021-04-09, Close, 8.09
2021-04-12, Close, 8.29
2021-04-13, Close, 8.27
2021-04-14, Close, 8.67
2021-04-15, Close, 8.97
2021-04-16, Close, 8.85
2021-04-19, Close, 8.80
2021-04-20, Close, 8.48
2021-04-21, Close, 8.35
2021-04-22, Close, 8.41
2021-04-23, Close, 8.37
2021-04-26, Close, 8.30
2021-04-27, Close, 7.76
2021-04-27, 卖出单, 7.76
2021-04-28, 已卖出, 价格: 7.99, 费用: 7.13, 佣金 0.01
2021-04-28, 交易利润, 毛利润 0.86, 净利润 0.84
2021-04-28, Close, 7.81
2021-04-29, Close, 7.66
2021-04-30, Close, 7.73
2021-05-06, Close, 7.73
2021-05-07, Close, 7.73
2021-05-10, Close, 7.81
2021-05-11, Close, 7.86
2021-05-12, Close, 8.03
2021-05-13, Close, 8.03
2021-05-14, Close, 7.97
2021-05-17, Close, 7.93
2021-05-18, Close, 7.84
2021-05-19, Close, 7.89
2021-05-20, Close, 7.83
2021-05-21, Close, 7.79
2021-05-24, Close, 7.81
2021-05-25, Close, 7.89
2021-05-26, Close, 7.92
2021-05-27, Close, 7.89
2021-05-27, 买入单, 7.89
2021-05-28, 已买入, 价格: 7.91, 费用: 7.91, 佣金 0.01
2021-05-28, Close, 8.68
2021-05-31, Close, 9.30
2021-06-01, Close, 8.90
2021-06-02, Close, 8.95
2021-06-03, Close, 9.12
2021-06-04, Close, 8.84
2021-06-07, Close, 8.92
2021-06-08, Close, 8.92
2021-06-09, Close, 9.11
2021-06-10, Close, 8.88
2021-06-11, Close, 8.49
2021-06-15, Close, 8.55
2021-06-16, Close, 8.93
2021-06-17, Close, 8.81
2021-06-18, Close, 8.96
2021-06-21, Close, 9.16
2021-06-22, Close, 9.04
2021-06-23, Close, 9.04
2021-06-24, Close, 8.76
2021-06-25, Close, 8.96
2021-06-28, Close, 9.04
2021-06-29, Close, 8.88
2021-06-30, Close, 9.20
2021-07-01, Close, 9.22
2021-07-02, Close, 8.73
2021-07-05, Close, 8.61
2021-07-06, Close, 9.47
2021-07-07, Close, 9.49
2021-07-08, Close, 9.64
2021-07-09, Close, 9.52
2021-07-12, Close, 10.03
2021-07-13, Close, 9.64
2021-07-14, Close, 9.74
2021-07-15, Close, 9.60
2021-07-16, Close, 9.41
2021-07-19, Close, 9.47
2021-07-20, Close, 9.19
2021-07-21, Close, 9.26
2021-07-22, Close, 9.50
2021-07-23, Close, 9.69
2021-07-26, Close, 9.25
2021-07-27, Close, 8.95
2021-07-27, 卖出单, 8.95
2021-07-28, 已卖出, 价格: 8.95, 费用: 7.91, 佣金 0.01
2021-07-28, 交易利润, 毛利润 1.04, 净利润 1.02
2021-07-28, Close, 9.04
2021-07-29, Close, 9.04
2021-07-30, Close, 8.91
2021-08-02, Close, 9.16
2021-08-03, Close, 9.38
2021-08-04, Close, 9.30
2021-08-05, Close, 9.23
2021-08-06, Close, 9.15
2021-08-09, Close, 9.15
2021-08-10, Close, 9.09
2021-08-11, Close, 9.04
2021-08-12, Close, 9.09
2021-08-13, Close, 9.60
2021-08-16, Close, 9.50
2021-08-16, 买入单, 9.50
2021-08-17, 已买入, 价格: 9.51, 费用: 9.51, 佣金 0.01
2021-08-17, Close, 9.31
2021-08-18, Close, 10.24
2021-08-19, Close, 10.01
2021-08-20, Close, 9.72
2021-08-23, Close, 10.18
2021-08-24, Close, 9.97
2021-08-25, Close, 9.91
2021-08-26, Close, 9.77
2021-08-27, Close, 9.69
2021-08-30, Close, 9.86
2021-08-31, Close, 9.47
2021-09-01, Close, 9.64
2021-09-02, Close, 9.68
2021-09-03, Close, 10.10
2021-09-06, Close, 10.09
2021-09-07, Close, 10.12
2021-09-08, Close, 10.16
2021-09-09, Close, 10.08
2021-09-10, Close, 10.36
2021-09-13, Close, 10.59
2021-09-14, Close, 10.09
2021-09-15, Close, 10.10
2021-09-16, Close, 10.18
2021-09-17, Close, 10.48
2021-09-22, Close, 10.85
2021-09-23, Close, 10.65
2021-09-24, Close, 10.22
2021-09-27, Close, 9.95
2021-09-28, Close, 9.98
2021-09-29, Close, 9.39
2021-09-29, 卖出单, 9.39
2021-09-30, 已卖出, 价格: 9.53, 费用: 9.51, 佣金 0.01
2021-09-30, 交易利润, 毛利润 0.02, 净利润 0.00
2021-09-30, Close, 9.48
2021-10-08, Close, 9.56
2021-10-11, Close, 9.61
2021-10-12, Close, 9.28
2021-10-13, Close, 9.17
2021-10-14, Close, 9.19
2021-10-15, Close, 9.08
2021-10-18, Close, 8.93
2021-10-19, Close, 8.98
2021-10-20, Close, 9.03
2021-10-21, Close, 8.82
2021-10-22, Close, 8.63
2021-10-25, Close, 8.81
2021-10-26, Close, 8.76
2021-10-27, Close, 8.42
2021-10-28, Close, 8.47
2021-10-29, Close, 8.49
2021-11-01, Close, 8.64
2021-11-02, Close, 8.55
2021-11-03, Close, 8.71
2021-11-04, Close, 8.74
2021-11-05, Close, 8.47
2021-11-08, Close, 8.37
2021-11-09, Close, 8.65
2021-11-10, Close, 8.75
2021-11-11, Close, 8.72
2021-11-12, Close, 8.77
2021-11-15, Close, 9.00
2021-11-15, 买入单, 9.00
2021-11-16, 已买入, 价格: 9.00, 费用: 9.00, 佣金 0.01
2021-11-16, Close, 8.94
2021-11-17, Close, 9.00
2021-11-18, Close, 8.87
2021-11-19, Close, 8.89
2021-11-22, Close, 8.99
2021-11-23, Close, 8.99
2021-11-24, Close, 8.85
2021-11-25, Close, 8.86
2021-11-26, Close, 8.65
2021-11-29, Close, 8.44
2021-11-29, 卖出单, 8.44
2021-11-30, 已卖出, 价格: 8.44, 费用: 9.00, 佣金 0.01
2021-11-30, 交易利润, 毛利润 -0.56, 净利润 -0.58
2021-11-30, Close, 8.55
2021-12-01, Close, 8.57
2021-12-02, Close, 8.39
2021-12-03, Close, 8.42
2021-12-06, Close, 8.58
2021-12-07, Close, 8.40
2021-12-08, Close, 8.50
2021-12-09, Close, 8.60
2021-12-10, Close, 8.62
2021-12-13, Close, 8.67
2021-12-14, Close, 8.78
2021-12-15, Close, 8.65
2021-12-15, 买入单, 8.65
2021-12-16, 已买入, 价格: 8.63, 费用: 8.63, 佣金 0.01
2021-12-16, Close, 8.42
2021-12-16, 卖出单, 8.42
2021-12-17, 已卖出, 价格: 8.40, 费用: 8.63, 佣金 0.01
2021-12-17, 交易利润, 毛利润 -0.23, 净利润 -0.25
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-27, Close, 8.69
2021-12-28, Close, 8.61
2021-12-28, 买入单, 8.61
2021-12-29, 已买入, 价格: 8.63, 费用: 8.63, 佣金 0.01
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-19, Close, 8.75
2022-01-20, Close, 8.65
2022-01-21, Close, 8.24
2022-01-21, 卖出单, 8.24
2022-01-24, 已卖出, 价格: 8.24, 费用: 8.63, 佣金 0.01
2022-01-24, 交易利润, 毛利润 -0.39, 净利润 -0.41
2022-01-24, Close, 8.10
2022-01-25, Close, 7.70
2022-01-26, Close, 7.64
2022-01-27, Close, 7.57
期末资金: 100000.64
交易日期与通达信中买卖点出现日期一致。
实现这个例子的完整python代码:
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),
('para_short', 5),
('para_long', 20),
)
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)
# 收盘价的 para_short 日简单移动平均
self.ma1 = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.para_short)
# 收盘价的 para_long 日简单移动平均
self.ma2 = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.para_long)
# MA1 = self.sma_short[0]
# MA2 = self.sma_long[0]
# 交易函数
def next(self):
# 记录收盘价
self.log('Close, %.2f' % self.dataclose[0])
# 如果有订单正在挂起,不操作
if self.order:
return
# 交易条件
# MA1上穿MA2
# buyin_flag = self.ma1 > self.ma2
# MA2上穿MA1
# sellout_flag = self.ma2 > self.ma1
# 如果没有持仓则买入
if not self.position:
# MA1上穿MA2
if self.ma1 > self.ma2:
# 买入
self.log('买入单, %.2f' % self.dataclose[0])
# 跟踪订单避免重复
self.order = self.buy()
else:
# 如果已经持仓,MA2上穿MA1
if self.ma2 > self.ma1:
# 全部卖出
self.log('卖出单, %.2f' % self.dataclose[0])
# 跟踪订单避免重复
self.order = self.sell()
# 订单状态通知,买入卖出都是下单
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))
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())
写到这里,我突发奇想,是不是可以写个程序,自动把通达信公式改写为Backtrader策略呢?