So current I do it like this:
contract <- lapply(sym, function(x) twsEquity(x, 'SMART','ISLAND'))
lapply(contract, function(x) reqHistoricalData(tws, Contract=x, barSize = "1 day", duration = "1 D", verbose = FALSE))
sym is merely a vector of 30 or so stock symbols. This is extremely slow.
Therefore, this could not be the right way to do it. In my live trading session, I must monitor 100s of stocks. Updates to their last traded price must be retrieve in slip seconds, not minutes.
You can use the function reqMktData
with snapshot set to TRUE
.
sym <- c("AAPL", "MSFT")
contracts <- lapply(sym, function(x) twsEquity(x, 'SMART','ISLAND'))
last_prices <- lapply(contracts, function(x) reqMktData(tws,
Contract = x,
snapshot = TRUE))
Ignore the warnings you are getting.
And note that the lastTimeStamp is in your local time, not the timestamp on the exchange.
last_prices
[[1]]
lastTimeStamp symbol bidSize bidPrice askPrice askSize lastPrice Volume Open High Low Close
1 2020-08-31 18:38:48 AAPL 4 129.46 129.48 3 129.48 1311118 127.67 130.05 126 124.81
[[2]]
lastTimeStamp symbol bidSize bidPrice askPrice askSize lastPrice Volume Open High Low Close
1 2020-08-31 18:38:47 MSFT 1 225.68 225.7 3 225.69 146282 227.1 228.7 224.31 228.91