我正在使用 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)
尽管这在实践中行不通,因为我希望利润和止损订单一旦达到就取消其他订单。
谢谢。
最佳答案
建议将 100 个订单放在第一位,然后是 50 个订单。意味着您进行一次交易的调用。 API 的功能有时会很奇怪......尤其是开源 API。在 R 详细设置中使用 Java API 可能会有更好的运气 http://m.youtube.com/watch?v=yfhmaqFyHPI
关于r - 使用 R 中的 IBrokers 进行括号订单中的多个数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210244/