显然,交易者可以在实时交易时段轻松监控 100 支股票。
这是仅检索 30 多个股票代码的实时数据的代码:
tws <- twsConnect()
sym <- c("AAPL", "MSFT", "GOOGL", "TWTR", "MSFT", "BABA","JD", "WLTW", "DIA",
"QQQ", "VOO", "MOMO","IBB", "VOOG", "VYM","VNQ", "BIDU", "BA",
"AYX", "ROKU", "UBER","ACB", "CRON", "TTD","BAM", "AZO", "JPM",
"WMT", "GRUB", "TSN")
contracts <- lapply(sym, function(x) twsEquity(x, 'SMART','ISLAND'))
system.time(
last_prices <- lapply(contracts, function(x) reqMktData(tws,
Contract = x,
snapshot = TRUE))
)
这是性能指标:
2 -1 2104 Market data farm connection is OK:usfarm
2 -1 2106 HMDS data farm connection is OK:ushmds
2 -1 2158 Sec-def data farm connection is OK:secdefil
user system elapsed
0.408 0.367 46.727
Warning message:
In .Internal(gc(verbose, reset, full)) :
closing unused connection 6 (->localhost:7496)
这显然是不可接受的。仅检索 30 多支股票的实时数据不应超过 40 秒以上。
使用 quantmod::getSymbols,这个操作基本上是即时的。
我究竟做错了什么?
我有一个 8 核 XEON 处理器。也是最新款之一。不可能是我的硬件。