多改几个通达信交易指标有助于熟悉Backtrader开发。
这里记录一下通达信内置的mace交易指标公式的修改和测试结果。
通达信附图指标:
DIFF:=EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);
DEA:=EMA(DIFF,MID);
MACD:=2*(DIFF-DEA);
平空开多:=CROSS(MACD,0);
平多开空:=CROSS(0,MACD);
STICKLINE(平空开多,0,30,2,0),COLOR0000FF;
STICKLINE(平多开空,0,30,2,0),COLORWHITE;
python代码:
# -*- coding: utf-8 -*-
"""
Created on Feb 21 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日均线
('para_mid', 12),
('para_short', 9),
('para_long', 26),
)
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.para_mid)
# # 收盘价的 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)
# 交易条件
# DIFF:=EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);
# DEA:=EMA(DIFF,MID);
# MACD:=2*(DIFF-DEA);
self.DIFF = bt.indicators.EMA(self.data.close, period=self.params.para_short) - \
bt.indicators.EMA(self.data.close, period=self.params.para_long)
self.DEA = bt.indicators.EMA(self.DIFF, period=self.params.para_mid)
self.MACD = 2 * (self.DIFF - self.DEA)
# 交易函数
def next(self):
# 记录收盘价
self.log('Close, %.2f' % self.dataclose[0])
# 如果有订单正在挂起,不操作
if self.order:
return
# 如果没有持仓则买入
if not self.position:
# MACD 上穿 0
if self.MACD > 0:
# 买入
self.log('买入单, %.2f' % self.dataclose[0])
# 跟踪订单避免重复
self.order = self.buy()
else:
# 如果已经持仓, 0 上穿 MACD
if 0 > self.MACD:
# 全部卖出
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())
通过对比,可以看到python回测买卖日期与通达信是对应的。
但是这个策略的盈利能力很差:
。。。。。。
2022-01-20, 已卖出, 价格: 8.75, 费用: 8.53, 佣金 0.01
2022-01-20, 交易利润, 毛利润 0.22, 净利润 0.20
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.84