Backtrader 文档学习-Bracket Orders
1. 概述
组合订单类型是一个非常宽泛的订单类别,只要brokder支持的订单类型都可以,
包括(Market, Limit, Close, Stop, StopLimit, StopTrail, StopTrailLimit, OCO)。
该功能用于回测,交互broker
Bracket订单不是单个订单,而是由3个订单组成,考虑多头情况。
- 一个主要的买入订单,通常设置为限价或止损限价订单。
- 一个低位卖出订单,通常设置为止损单以限制损失。(止损单)
- 一个高位卖出订单,通常设置为限价单以获利。(止盈单)
相反如果是空头情况,有相应的卖出和2个买入订单。
低/高位订单实际上在主订单周围创建了一个bracket。
从逻辑上讲,以下规则适用:
- 3个订单是同时提交的,以避免其中任何一个单独触发
- 低/高侧订单被标记为主订单的子订单
- 子订单在主订单被执行之前不会被激活
- 主订单的取消同时取消了低端和高端的子订单
- 一旦激活,任何低/高端的订单的执行或取消都会自动取消另外的订单
2.使用模式
创建Bracket订单集有两种可能性 :
- 单次触发3个订单
- 手动触发3个订单
(1) 单次触发Bracket订单
要控制bracket订单,backtrader在
strategy
中提供了2个新的方法:
buy_bracket 和 sell_bracket
下面一条语句返回一个包含3个订单组合
brackets = self.buy_bracket(limitprice=14.00, price=13.50, stopprice=13.00)
注意上面参数 stopprice 和 limitprice 如何环绕在 price 周围。
实际成交的数据默认是data0,成交数量由默认的sizer自动计算得到。除了可以指定这两个参数,也可以指定其他参数,执行精细控制。
因为当发出sell_bracket订单时,低端和高端将被转向,参数的命名遵循常规stop 和 limit 。
- 代码返回list,list中包含3个订单[main, stop, limit]。
- 因为当生成一个sell_bracket订单,low和high会环绕在price两边,参数的命名遵循stop和 limit的约定。
- stop意味着止损(低的一侧是买入,高的一侧是卖出)
- limit意味着止盈(高的一侧是买入,低的一侧是卖出)
(2)手工触发Backet订单
涉及到3个订单的生成,通过transmit和parent参数实现。规则:
- 主端订单必须首先创建,并且传入transmit=False
- 低/高侧订单必传入parent=main_side_order
- 要创建的第一个低/高侧订单必须传入transmit=False
- 最后一个要创建的命令(低位或高位)设置transmit=True
测试用例:
mainside = self.buy(price=13.50, exectype=bt.Order.Limit, transmit=False)
lowside = self.sell(price=13.00, size=mainside.size, exectype=bt.Order.Stop,
transmit=False, parent=mainside)
highside = self.sell(price=14.00, size=mainside.size, exectype=bt.Order.Limit,
transmit=True, parent=mainside)
- 保持跟踪主侧订单,标识它是其他订单的父订单
- 控制传输,以确保只有最后一个订单会被联合触发
- 定义执行类型
- 定义低位和高位的size
由于size必须相同。如果没有手工指定参数,最终用户引入了sizer,那么sizer实际上可以为订单指示不同的值。这就是为什么必须先设置主侧订单后手动添加到调用中。
3. 示例
交易核心代码
o1 exectype=bt.Order.Limit 主单
o2 exectype=bt.Order.Stop 止损单
o3 exectype=bt.Order.Limit 止盈单
if self.cross > 0.0: # crossing up
close = self.data.close[0]
p1 = close * (1.0 - self.p.limit)
p2 = p1 - 0.02 * close
p3 = p1 + 0.02 * close
valid1 = datetime.timedelta(self.p.limdays)
valid2 = valid3 = datetime.timedelta(self.p.limdays2)
if self.p.switchp1p2:
p1, p2 = p2, p1
valid1, valid2 = valid2, valid1
if not self.p.usebracket:
o1 = self.buy(exectype=bt.Order.Limit,
price=p1,
valid=valid1,
transmit=False)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o1.ref, p1))
o2 = self.sell(exectype=bt.Order.Stop,
price=p2,
valid=valid2,
parent=o1,
transmit=False)
print('{}: Oref {} / Sell Stop at {}'.format(
self.datetime.date(), o2.ref, p2))
o3 = self.sell(exectype=bt.Order.Limit,
price=p3,
valid=valid3,
parent=o1,
transmit=True)
print('{}: Oref {} / Sell Limit at {}'.format(
self.datetime.date(), o3.ref, p3))
self.orefs = [o1.ref, o2.ref, o3.ref]
python ./bracket.py --plot
(1)主侧订单过期
2005-01-28: Oref 1 / Buy at 2941.11055
2005-01-28: Oref 2 / Sell Stop at 2881.99275
2005-01-28: Oref 3 / Sell Limit at 3000.22835
2005-01-31: Order ref: 1 / Type Buy / Status Submitted
2005-01-31: Order ref: 2 / Type Sell / Status Submitted
2005-01-31: Order ref: 3 / Type Sell / Status Submitted
2005-01-31: Order ref: 1 / Type Buy / Status Accepted
2005-01-31: Order ref: 2 / Type Sell / Status Accepted
2005-01-31: Order ref: 3 / Type Sell / Status Accepted
2005-02-01: Order ref: 1 / Type Buy / Status Expired
2005-02-01: Order ref: 2 / Type Sell / Status Canceled
2005-02-01: Order ref: 3 / Type Sell / Status Canceled
第一个案例中,主侧订单过期,这自动取消了其他两个订单。
2005-02-01 1号买单过期,2/3号订单取消。
(2)止损单执行
2005-08-11: Oref 16 / Buy at 3337.3891999999996
2005-08-11: Oref 17 / Sell Stop at 3270.3059999999996
2005-08-11: Oref 18 / Sell Limit at 3404.4723999999997
2005-08-12: Order ref: 16 / Type Buy / Status Submitted
2005-08-12: Order ref: 17 / Type Sell / Status Submitted
2005-08-12: Order ref: 18 / Type Sell / Status Submitted
2005-08-12: Order ref: 16 / Type Buy / Status Accepted
2005-08-12: Order ref: 17 / Type Sell / Status Accepted
2005-08-12: Order ref: 18 / Type Sell / Status Accepted
2005-08-12: Order ref: 16 / Type Buy / Status Completed
2005-08-18: Order ref: 17 / Type Sell / Status Completed
2005-08-18: Order ref: 18 / Type Sell / Status Canceled
第二个案例中,主侧订单已完成,低位(在买入情况下为止损)被执行,限制了损失 。
2005-08-12 ,16号主单完成,17号止损单完成,18日止盈单取消
(3)止盈单执行
2005-09-26: Oref 22 / Buy at 3383.92535
2005-09-26: Oref 23 / Sell Stop at 3315.90675
2005-09-26: Oref 24 / Sell Limit at 3451.94395
2005-09-27: Order ref: 22 / Type Buy / Status Submitted
2005-09-27: Order ref: 23 / Type Sell / Status Submitted
2005-09-27: Order ref: 24 / Type Sell / Status Submitted
2005-09-27: Order ref: 22 / Type Buy / Status Accepted
2005-09-27: Order ref: 23 / Type Sell / Status Accepted
2005-09-27: Order ref: 24 / Type Sell / Status Accepted
2005-09-27: Order ref: 22 / Type Buy / Status Completed
2005-10-04: Order ref: 24 / Type Sell / Status Completed
2005-10-04: Order ref: 23 / Type Sell / Status Canceled
第三个案例中,主侧订单已完成,高位(限价)被执行,能够注意到,因为完成的id是22和24,高侧订单是最后一个发出的,这意味着未执行的低侧订单id是23。
2005-09-27,22号主单完成,23号止损单取消,24号止盈单完成。
(4)图示
It can be immediately seen that the losing trades align around the same value and winning trades too, which is the purpose of the backeting. Controlling both sides.
能够立刻看到,亏损交易都在同一个值附近,赢利交易也是如此,这就是backeting的目的。控制双侧,即控制止损,控制盈利。
(5)用参数usebracket=True
python ./bracket.py --strat usebracket=True --plot
相同的结果。
图示:
4. 完整代码
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import datetime
import backtrader as bt
class St(bt.Strategy):
params = dict(
ma=bt.ind.SMA,
p1=5,
p2=15,
limit=0.005,
limdays=3,
limdays2=1000,
hold=10,
usebracket=False, # use order_target_size
switchp1p2=False, # switch prices of order1 and order2
)
def notify_order(self, order):
print('{}: Order ref: {} / Type {} / Status {}'.format(
self.data.datetime.date(0),
order.ref, 'Buy' * order.isbuy() or 'Sell',
order.getstatusname()))
if order.status == order.Completed:
self.holdstart = len(self)
if not order.alive() and order.ref in self.orefs:
self.orefs.remove(order.ref)
def __init__(self):
ma1, ma2 = self.p.ma(period=self.p.p1), self.p.ma(period=self.p.p2)
self.cross = bt.ind.CrossOver(ma1, ma2)
self.orefs = list()
if self.p.usebracket:
print('-' * 5, 'Using buy_bracket')
def next(self):
if self.orefs:
return # pending orders do nothing
if not self.position:
if self.cross > 0.0: # crossing up
close = self.data.close[0]
p1 = close * (1.0 - self.p.limit)
p2 = p1 - 0.02 * close
p3 = p1 + 0.02 * close
valid1 = datetime.timedelta(self.p.limdays)
valid2 = valid3 = datetime.timedelta(self.p.limdays2)
if self.p.switchp1p2:
p1, p2 = p2, p1
valid1, valid2 = valid2, valid1
if not self.p.usebracket:
o1 = self.buy(exectype=bt.Order.Limit,
price=p1,
valid=valid1,
transmit=False)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o1.ref, p1))
o2 = self.sell(exectype=bt.Order.Stop,
price=p2,
valid=valid2,
parent=o1,
transmit=False)
print('{}: Oref {} / Sell Stop at {}'.format(
self.datetime.date(), o2.ref, p2))
o3 = self.sell(exectype=bt.Order.Limit,
price=p3,
valid=valid3,
parent=o1,
transmit=True)
print('{}: Oref {} / Sell Limit at {}'.format(
self.datetime.date(), o3.ref, p3))
self.orefs = [o1.ref, o2.ref, o3.ref]
else:
os = self.buy_bracket(
price=p1, valid=valid1,
stopprice=p2, stopargs=dict(valid=valid2),
limitprice=p3, limitargs=dict(valid=valid3),)
self.orefs = [o.ref for o in os]
else: # in the market
if (len(self) - self.holdstart) >= self.p.hold:
pass # do nothing in this case
def runstrat(args=None):
args = parse_args(args)
cerebro = bt.Cerebro()
# Data feed kwargs
kwargs = dict()
# Parse from/to-date
dtfmt, tmfmt = '%Y-%m-%d', 'T%H:%M:%S'
for a, d in ((getattr(args, x), x) for x in ['fromdate', 'todate']):
if a:
strpfmt = dtfmt + tmfmt * ('T' in a)
kwargs[d] = datetime.datetime.strptime(a, strpfmt)
# Data feed
data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, **kwargs)
cerebro.adddata(data0)
# Broker
cerebro.broker = bt.brokers.BackBroker(**eval('dict(' + args.broker + ')'))
# Sizer
cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')'))
# Strategy
cerebro.addstrategy(St, **eval('dict(' + args.strat + ')'))
# Execute
cerebro.run(**eval('dict(' + args.cerebro + ')'))
if args.plot: # Plot if requested to
cerebro.plot(**eval('dict(' + args.plot + ')'))
def parse_args(pargs=None):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=(
'Sample Skeleton'
)
)
parser.add_argument('--data0', default='./datas/2005-2006-day-001.txt',
required=False, help='Data to read in')
# Defaults for dates
parser.add_argument('--fromdate', required=False, default='',
help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
parser.add_argument('--todate', required=False, default='',
help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
parser.add_argument('--cerebro', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--broker', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--sizer', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--strat', required=False, default='',
metavar='kwargs', help='kwargs in key=value format')
parser.add_argument('--plot', required=False, default='',
nargs='?', const='{}',
metavar='kwargs', help='kwargs in key=value format')
return parser.parse_args(pargs)
if __name__ == '__main__':
runstrat()