How to trade multiple equities at once using Ibrokers in R

202 views Asked by At

Using the Ibrokers account I know how to place a trade with one ticker which , in the example below I place a trade with "DAL"

library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)

However I am interested in trading multiple tickers at once for example how can I place an order to buy "DAL" and "AAL". How can I put multiple orders into IBrokers in R?

1

There are 1 answers

0
brian On

I doubt your code even works now. To place another order just place another order. Note that they must have separate ids and the ids have to increase every order.

library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )

id <- tws.reqids(1)

contract=twsEquity(symbol = "AAL", exch = "SMART" )
# give an order id
order=twsOrder(id, action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)

# increment id
id <- id+1
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(id,action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)

I don't think that code will work. The defaults for order look to be LMT with a price of 0. You may want to try orderType = "MKT". Check the documentation for IBrokers.

I would suggest using a different API, it doesn't look like you have used the R package much so it's not a big deal to switch. It probably won't work for much longer. I think it is unsupported and the API has changed since the last update.