【问题标题】:Multiple Quantities in Bracket Orders Using IBrokers in R在 R 中使用 IBrokers 的括号定单中的多个数量
【发布时间】:2016-07-12 15:17:32
【问题描述】:

我正在使用 R 中的 ibrokers 包,并试图为交易设置多个收盘价。例如,以 106 美元买入 100 股 AAPL,以 107 美元卖出 50 股,以 108 美元卖出 50 股,止损价为 105 美元。

当我发送多个获利了结单时,似乎忽略了 50 个的数量,而是收到了两个每个 100 股的卖单。

这是我正在运行的代码

tws <- twsConnect() 

stock <- twsEquity("AAPL")
parentLongId <- reqIds(tws)

parentLongOrder <- twsOrder(parentLongId, action="BUY", totalQuantity = 100, 
                            orderType = "LMT", lmtPrice = 106, 
                            transmit=TRUE)
placeOrder(tws, stock, parentLongOrder)


childLongProfitId <- reqIds(tws)
childLongProfitOrder <- twsOrder(childLongProfitId, action="SELL", totalQuantity = 50, 
                                 orderType = "LMT", lmtPrice = 107,
                                 transmit=TRUE, parentId = parentLongId)
placeOrder(tws, stock, childLongProfitOrder)

childLongProfitId2 <- reqIds(tws)
childLongProfitOrder2 <- twsOrder(childLongProfitId2, action="SELL", totalQuantity = 50, 
                                  orderType = "LMT", lmtPrice = 108,
                                  transmit=TRUE, parentId = parentLongId)
placeOrder(tws, stock, childLongProfitOrder2)

childLongStopId <- reqIds(tws)
childLongStopOrder <- twsOrder(childLongStopId, action="SELL", totalQuantity = 100, 
                               orderType = "STP", auxPrice = 105,
                               transmit=TRUE, parentId = parentLongId, account=accountNum)
placeOrder(tws, stock, childLongStopOrder)

twsDisconnect(tws) 

您可以看到,所有 3 个订单的数量都是 100,而不是买单的数量为 100,每个卖单的数量为 50。

有谁知道如何解决这个问题?

作为一个健全的检查,我输入了没有 parentId 的订单并且它有效。这是代码:

tws <- twsConnect() #open connection, R automatically pauses until manually accepted on IB.

stock <- twsEquity("AAPL")
parentLongId <- reqIds(tws)

parentLongOrder <- twsOrder(parentLongId, action="BUY", totalQuantity = 100, 
                            orderType = "LMT", lmtPrice = 106, 
                            transmit=TRUE)
placeOrder(tws, stock, parentLongOrder)


childLongProfitId <- reqIds(tws)
childLongProfitOrder <- twsOrder(childLongProfitId, action="SELL", totalQuantity = 50, 
                                 orderType = "LMT", lmtPrice = 107,
                                 transmit=TRUE)
placeOrder(tws, stock, childLongProfitOrder)

childLongProfitId2 <- reqIds(tws)
childLongProfitOrder2 <- twsOrder(childLongProfitId2, action="SELL", totalQuantity = 50, 
                                  orderType = "LMT", lmtPrice = 108,
                                  transmit=TRUE)
placeOrder(tws, stock, childLongProfitOrder2)

childLongStopId <- reqIds(tws)
childLongStopOrder <- twsOrder(childLongStopId, action="SELL", totalQuantity = 100, 
                               orderType = "STP", auxPrice = 105,
                               transmit=TRUE, parentId = parentLongId, account=accountNum)
placeOrder(tws, stock, childLongStopOrder)

twsDisconnect(tws) 

虽然这在实践中行不通,因为我希望获利和止损订单一旦命中就取消其他订单。

谢谢。

【问题讨论】:

  • 我会建议 2 个括号定单,或者一个包含 2 批 OCA 平仓定单的定单。基本上以一半的金额下两个括号订单,但利润金额不同。
  • 嗨,布莱恩。创建 2 组订单会起作用,但如果我也想移动止损点,它可能会产生问题。我需要移动它两次。我并不总是希望订单是“设置并忘记”。
  • 我刚看到这个。如果您可以将赏金延长 6 小时左右,我可以为您解决这个问题。
  • Hack-R,赏金重启。谢谢。

标签: r trading ibrokers


【解决方案1】:

建议将 100 个订单放在第一位,然后是 50 个订单。在您进行交易的单次调用中的含义。 API 的功能有时会很奇怪……尤其是开源的。在 R 详细设置中使用 Java API 可能会有更好的运气http://m.youtube.com/watch?v=yfhmaqFyHPI

【讨论】:

  • 嗨,马特,不幸的是,先下止损单没有用。尽管我知道它得到了更好的支持,但我对 java 不是很熟悉。另一种选择是使用 Excel DDE。对于像这样简单的事情,它可能会成功。挑战在于,一旦它变得更复杂,我可能会不走运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 2020-04-02
相关资源
最近更新 更多