简体   繁体   中英

How to trade multiple equities at once using Ibrokers in R

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM