6

I am working with the ibrokers package in R and am trying to set multiple closing prices for a trade. For example, buy 100 shares of AAPL at $106, sell 50 at $107 and 50 at $108, with a stop price of $105.

When I send the multiple profit taking orders, it seems like the quantity of 50 is ignored, instead I get two sell orders for 100 shares each.

This is the code I am 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 the buy and 50 for each of the sell orders. Orders in Trader Workstation

Does anyone know how this can be corrected?

As a sanity check, I entered in orders without the parentId and it worked. Here is the code for that:

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) 

Though this won't work in practice since it I want the profit and stop orders to cancel the others once hit.

Thank you.

epo3
  • 2,991
  • 2
  • 33
  • 60
mks212
  • 901
  • 1
  • 18
  • 40
  • 1
    I would suggest 2 bracket orders or just one with 2 batches of OCA closing orders. Essentially place two bracket orders for half the amount but with different profit amounts. – brian Mar 24 '16 at 21:47
  • Hi Brian. Creating 2 sets of orders will work, but it may create problems if I want to move the stop as well. I'll need to move it twice. I don't always want the orders to be "set and forget." – mks212 Mar 24 '16 at 23:00
  • I just saw this. If you could extend the bounty by 6 hours or so I could take care of this for you. – Hack-R Apr 03 '16 at 19:51
  • Hack-R, bounty restarted. Thanks. – mks212 Apr 06 '16 at 19:02

1 Answers1

0

Suggest putting your 100 orders 1st followed by the 50 orders. Meaning in your single call to place a trade. APIs function strangely at times... Especially open source ones. You may have better luck using the Java API in R detailed setup here http://m.youtube.com/watch?v=yfhmaqFyHPI

Matt
  • 2,602
  • 13
  • 36
  • Hi Matt, Putting the stop order first did not work unfortunately. I am not very familiar with java though I know it is much better supported. Another alternative is to use the Excel DDE. For something as simple as this, it will likely do the trick. The challenge is that as soon as it gets more complicated, I may be out of luck with it. – mks212 Apr 05 '16 at 15:23