版本1.9.34.116將OCO (又名 One Cancel Others)添加到回測庫中。
筆記
這僅在回測中實現,還沒有針對實時經紀人的實現
筆記
更新為1.9.36.116版本。盈透證券支持StopTrail 、 StopTrailLimit和OCO 。
OCO始終將組中的oco一個訂單指定為參數ocoStopTrailLimit:代理模擬和IB代理具有 asme 行為。指定:price作為初始止損觸發價格(同時指定trailamount),然後plimi作為初始限價。兩者之差將決定limitoffset(限價與止損觸發價之間的距離)
使用模式試圖保持用戶友好。因此,如果策略中的邏輯決定現在是發出訂單的時刻,則可以像這樣使用OCO :
def next(self):
...
o1 = self.buy(...)
...
o2 = self.buy(..., oco=o1)
...
o3 = self.buy(..., oco=o1) # or even oco=o2, o2 is already in o1 group
簡單的。 1 st order o1將類似於組長。通過使用oco命名參數指定o1 , o2和o3成為OCO 組的一部分。看到片段中的註釋表明o3也可以通過指定o2成為組的一部分(它已經是組的一部分)
隨著小組的成立,將發生以下情況:
- 如果該組中的任何訂單被執行、取消或到期,其他訂單將被取消
下面的示例展示了OCO概念。帶有情節的標準執行:
$ ./oco.py --broker cash=50000 --plot
筆記
現金增加到50000 ,因為資產達到4000的價值並且1件的 3 個訂單至少需要12000貨幣單位(經紀人的默認值為10000 )
用下面的圖表。
這實際上並沒有提供太多信息(它是標準的SMA Crossover策略)。該示例執行以下操作:
當快速 SMA 穿過慢速 SMA 向上時,發出 3 個訂單
order1是一個Limit訂單,將在limdays天(策略參數)到期,close價減少一個百分比作為限價order2是一個Limit訂單,到期時間更長,限價更低。order3為Limit單,進一步降低限價
因此order2和order3的執行不會發生,因為:
-
order1將首先執行,這應該會觸發其他人的取消
或者
order1將過期,這將觸發其他訂單的取消
系統保留 3 個訂單的ref標識符,並且僅當notify_order中的三個ref標識符顯示為Completed 、 Cancelled 、 Margin或Expired時才會發出新的buy
在持有一些柱的頭寸後簡單地退出。
為了嘗試跟踪實際執行,會生成文本輸出。其中一些:
2005-01-28: Oref 1 / Buy at 2941.11055 2005-01-28: Oref 2 / Buy at 2896.7722 2005-01-28: Oref 3 / Buy at 2822.87495 2005-01-31: Order ref: 1 / Type Buy / Status Submitted 2005-01-31: Order ref: 2 / Type Buy / Status Submitted 2005-01-31: Order ref: 3 / Type Buy / Status Submitted 2005-01-31: Order ref: 1 / Type Buy / Status Accepted 2005-01-31: Order ref: 2 / Type Buy / Status Accepted 2005-01-31: Order ref: 3 / Type Buy / Status Accepted 2005-02-01: Order ref: 1 / Type Buy / Status Expired 2005-02-01: Order ref: 3 / Type Buy / Status Canceled 2005-02-01: Order ref: 2 / Type Buy / Status Canceled ... 2006-06-23: Oref 49 / Buy at 3532.39925 2006-06-23: Oref 50 / Buy at 3479.147 2006-06-23: Oref 51 / Buy at 3390.39325 2006-06-26: Order ref: 49 / Type Buy / Status Submitted 2006-06-26: Order ref: 50 / Type Buy / Status Submitted 2006-06-26: Order ref: 51 / Type Buy / Status Submitted 2006-06-26: Order ref: 49 / Type Buy / Status Accepted 2006-06-26: Order ref: 50 / Type Buy / Status Accepted 2006-06-26: Order ref: 51 / Type Buy / Status Accepted 2006-06-26: Order ref: 49 / Type Buy / Status Completed 2006-06-26: Order ref: 51 / Type Buy / Status Canceled 2006-06-26: Order ref: 50 / Type Buy / Status Canceled ... 2006-11-10: Order ref: 61 / Type Buy / Status Canceled 2006-12-11: Oref 63 / Buy at 4032.62555 2006-12-11: Oref 64 / Buy at 3971.8322 2006-12-11: Oref 65 / Buy at 3870.50995 2006-12-12: Order ref: 63 / Type Buy / Status Submitted 2006-12-12: Order ref: 64 / Type Buy / Status Submitted 2006-12-12: Order ref: 65 / Type Buy / Status Submitted 2006-12-12: Order ref: 63 / Type Buy / Status Accepted 2006-12-12: Order ref: 64 / Type Buy / Status Accepted 2006-12-12: Order ref: 65 / Type Buy / Status Accepted 2006-12-15: Order ref: 63 / Type Buy / Status Expired 2006-12-15: Order ref: 65 / Type Buy / Status Canceled 2006-12-15: Order ref: 64 / Type Buy / Status Canceled
發生以下情況:
第一批訂單發出。訂單 1 到期,訂單 2 和 3 被取消。正如預期的那樣。
幾個月後,又發出了另外一批 3 份訂單。在這種情況下,訂單 49
Completed,50 和 51 立即取消最後一批和第一批一樣
現在讓我們檢查一下沒有OCO的行為:
$ ./oco.py --strat do_oco=False --broker cash=50000 2005-01-28: Oref 1 / Buy at 2941.11055 2005-01-28: Oref 2 / Buy at 2896.7722 2005-01-28: Oref 3 / Buy at 2822.87495 2005-01-31: Order ref: 1 / Type Buy / Status Submitted 2005-01-31: Order ref: 2 / Type Buy / Status Submitted 2005-01-31: Order ref: 3 / Type Buy / Status Submitted 2005-01-31: Order ref: 1 / Type Buy / Status Accepted 2005-01-31: Order ref: 2 / Type Buy / Status Accepted 2005-01-31: Order ref: 3 / Type Buy / Status Accepted 2005-02-01: Order ref: 1 / Type Buy / Status Expired
就是這樣,這並不多(沒有訂單執行,也不需要圖表)
該批訂單下達
訂單 1 到期,但由於策略獲取了參數
do_oco= False,訂單 2 和 3 不屬於OCO組因此,訂單 2 和 3 不會被取消,並且因為默認的到期增量是
1000天后,它們永遠不會隨著樣本的可用數據而到期(2 年的數據)系統從不發出第二輪訂單。
示例使用
$ ./oco.py --help
usage: oco.py [-h] [--data0 DATA0] [--fromdate FROMDATE] [--todate TODATE]
[--cerebro kwargs] [--broker kwargs] [--sizer kwargs]
[--strat kwargs] [--plot [kwargs]]
Sample Skeleton
optional arguments:
-h, --help show this help message and exit
--data0 DATA0 Data to read in (default:
../../datas/2005-2006-day-001.txt)
--fromdate FROMDATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: )
--todate TODATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: )
--cerebro kwargs kwargs in key=value format (default: )
--broker kwargs kwargs in key=value format (default: )
--sizer kwargs kwargs in key=value format (default: )
--strat kwargs kwargs in key=value format (default: )
--plot [kwargs] kwargs in key=value format (default: )
示例代碼
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,
switchp1p2=False, # switch prices of order1 and order2
oco1oco2=False, # False - use order1 as oco for order3, else order2
do_oco=True, # use oco or not
)
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()
def next(self):
if self.orefs:
return # pending orders do nothing
if not self.position:
if self.cross > 0.0: # crossing up
p1 = self.data.close[0] * (1.0 - self.p.limit)
p2 = self.data.close[0] * (1.0 - 2 * 2 * self.p.limit)
p3 = self.data.close[0] * (1.0 - 3 * 3 * self.p.limit)
if self.p.switchp1p2:
p1, p2 = p2, p1
o1 = self.buy(exectype=bt.Order.Limit, price=p1,
valid=datetime.timedelta(self.p.limdays))
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o1.ref, p1))
oco2 = o1 if self.p.do_oco else None
o2 = self.buy(exectype=bt.Order.Limit, price=p2,
valid=datetime.timedelta(self.p.limdays2),
oco=oco2)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o2.ref, p2))
if self.p.do_oco:
oco3 = o1 if not self.p.oco1oco2 else oco2
else:
oco3 = None
o3 = self.buy(exectype=bt.Order.Limit, price=p3,
valid=datetime.timedelta(self.p.limdays2),
oco=oco3)
print('{}: Oref {} / Buy at {}'.format(
self.datetime.date(), o3.ref, p3))
self.orefs = [o1.ref, o2.ref, o3.ref]
else: # in the market
if (len(self) - self.holdstart) >= self.p.hold:
self.close()
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()