IB によって提供される NA フィールドを回避するには、「データ」構造を -1 で初期化する必要があります。したがって、次の変更があります。
is.negative <- function (x) {
tmp <- FALSE
if (is.numeric(x)) tmp <- (x<0)
return (tmp)
}
eWrapper.mydata <- function (n)
{
eW <- eWrapper(NULL)
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(-1, 2), ncol = 2), 0), .Dimnames = list(NULL, c("Ex-Date","Amount")))), n))
eW$tickString <- function(curMsg, msg, timestamp, file, ...) {
data <- eW$get.Data("data")
tickType = msg[3]
#print(paste("curMsg",curMsg,"tickString",tickType,sep=" "))
#print(msg)
id <- as.numeric(msg[2])
attr(data[[id]], "index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
if (tickType == 59) {
data[[id]][nr.data, 1:2] <- unlist(strsplit(msg[4],","))[3:4]
}
#print(data)
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}
snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...)
{
if (missing(eWrapper)) eWrapper <- eWrapper()
names(eWrapper$.Data$data) <- eWrapper$.Data$symbols
con <- twsCon[[1]]
# print("snapShot")
if (inherits(twsCon, "twsPlayback")) {
sys.time <- NULL
while (TRUE) {
if (!is.null(timestamp)) {
last.time <- sys.time
sys.time <- as.POSIXct(strptime(paste(readBin(con,
character(), 2), collapse = " "), timestamp))
if (!is.null(last.time)) {
Sys.sleep((sys.time - last.time) * playback)
}
curMsg <- .Internal(readBin(con, "character",
1L, NA_integer_, TRUE, FALSE))
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, format(sys.time,
timestamp), file, ...)
}
else {
curMsg <- readBin(con, character(), 1)
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
Sys.sleep(5 * playback)
}
}
}
else {
while (TRUE) {
socketSelect(list(con), FALSE, NULL)
curMsg <- .Internal(readBin(con, "character", 1L,NA_integer_, TRUE, FALSE))
# print(paste("snapShot curMsg: ",curMsg,sep=""))
if (!is.null(timestamp)) {
processMsg(curMsg, con, eWrapper, format(Sys.time(),timestamp), file, ...)
}
else {
processMsg(curMsg, con, eWrapper, timestamp,file, ...)
}
if (!any(sapply(eWrapper$.Data$data, is.negative)))
return(do.call(rbind, lapply(eWrapper$.Data$data, as.data.frame)))
}
}
}