R中使用IBrokers的支架订单中的多个数量

时间:2016-03-24 21:25:25

标签: r trading ibrokers

我正在使用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。 Orders in Trader Workstation

有谁知道如何纠正这个问题?

作为一个完整性检查,我在没有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) 

虽然这在实践中不起作用,因为我希望利润和停止命令取消其他人一旦击中。

谢谢。

1 个答案:

答案 0 :(得分:0)

建议先下载100个订单,然后下达50个订单。在您的单一电话中进行交易的意义。 API有时奇怪地运行...特别是开源的。您可以更好地使用此处的{详细设置http://m.youtube.com/watch?v=yfhmaqFyHPI

中的Java API