简体   繁体   English

如何使用盈透证券 (IB) API (ib_insync) 为 Python 复制括号定单功能

[英]How to replicate bracket orders functionality using Interactive Brokers (IB) API (ib_insync) for Python

I have been struggling with getting the bracket order to behave where I can pass an auto expire parameter (goodTillDate) for only the parent.我一直在努力让括号顺序表现得只能为父级传递自动过期参数(goodTillDate)。 When passing it in the bracket order code, it adds it to the limit and stop as well which defeats the purpose of a bracket order.在括号定单代码中传递它时,它会将其添加到限制和停止中,这违背了括号定单的目的。 I also want to be able to identify two limits for each bracket.我还希望能够为每个括号确定两个限制。 Anyone have any code which mirrors the parent/child relationship and OCO functions of a bracket order but with individual orders?任何人都有任何代码可以反映括号订单的父/子关系和 OCO 功能,但带有单独的订单? One parent, and three children (two limits for our target profit takers, and one stop)?一个父母,三个孩子(我们的目标获利者有两个限制,一个停止)?

First you submit the parent with the transmit flag as False, then specify parentId for each of the children and submit the last order with transmit as True.首先,您提交带有传输标志为 False 的父代,然后为每个子代指定 parentId 并提交带有传输为 True 的最后一个订单。

For two limits, you just split the order and submit the legs.对于两个限制,您只需拆分订单并提交边。 Alternatively you can experiment with conditional orders, specifying time.或者,您可以尝试有条件的订单,指定时间。

How would the children stay alive when the parent has expired?父母过世了,孩子们怎么活下去? Unless you mean partial fills.除非你的意思是部分填充。

A late answer to this question... I had the same problem and found a solution, with a little help, but I still wanna share, because it took me weeks to fix this.这个问题的答案迟到了......我遇到了同样的问题并找到了解决方案,并得到了一点帮助,但我仍然想分享,因为我花了数周时间来解决这个问题。

My old code placing a bracket order looked like this:我放置括号顺序的旧代码如下所示:

order = ib.bracketOrder('BUY', amount, limit, takeprofit, stoploss, outsideRth=True, tif='GTC')    
for ord in order:
    ib.placeOrder(contract, ord)

This will place a bracket order with TIF (time in force) set to GTC (good till cancelled) for all 3 orders, the parent and the 2 children.这将为所有 3 个订单(父订单和 2 个子订单)下一个括号订单,其中 TIF(有效时间)设置为 GTC(在取消之前有效)。

When I changed it to TIF=GTD and specified a time, this, of course, applied to the whole bracket order.当我将其更改为 TIF=GTD 并指定时间时,这当然适用于整个括号顺序。 So if it fills in the desired time, the takeprofit and stoploss will disappear after the GTD time expires.因此,如果它在所需时间填写,则在 GTD 时间到期后,止盈和止损将消失。 Not good.不好。

Then, someone sent me a little help and now this code works for me:然后,有人给了我一些帮助,现在这段代码对我有用:

bracket = ib.bracketOrder('BUY', amount, limit, takeprofit,stoploss, outsideRth=True)

gtddelta = (datetime.now() + timedelta(seconds=45)).strftime("%Y%m%d %H:%M:%S")
bracket.parent.tif = 'GTD'
bracket.parent.goodTillDate = gtddelta
    
for order in bracket:
    ib.placeOrder(contract, order)

This sets a 45 second GTD for the parent order.这为父订单设置了 45 秒的 GTD。 If it does not get filled, the whole bracket order will be cancelled.如果没有成交,整个括号定单将被取消。 If it does get filled, the takeprofit and the stoploss order remain, with TIF=GTC.如果它被执行,止盈和止损订单仍然存在,TIF=GTC。

Please note the changes to the old code.请注意旧代码的更改。 It is important to call the fist variable 'bracket' to be able to define the 'bracket.parent' later (look into the definiton).调用第一个变量“bracket”以便以后能够定义“bracket.parent”是很重要的(查看定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM