I work with the ibrokers package in R and try to set multiple closing prices for trading. For example, buy 100 AAPL shares for $ 106, sell $ 50 for $ 107 and $ 50 for $ 108 with stop prices of $ 105.
When I send several profit orders, it seems that the quantity 50 is ignored, instead I receive two sales orders of 100 shares each.
This is the code I'm running
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)
You can see that the quantity is 100 for all 3 orders instead of 100 for a purchase and 50 for each sales order. 
Does anyone know how to fix this?
As a health check, I entered orders without parentId and worked. Here is the code for this:
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)
Although this will not work in practice, since I want to make a profit and stop orders to cancel others when they get there.
Thanks.