如果使用 IBrokers 包,如果标的合约今天没有成交量或没有最后价格,下面的代码最终会停止。
reqMktData(tws, ticker, eventWrapper = eWrapper.data(1), CALLBACK = snapShot)
我想做的是从 ewrapper 函数中删除交易量以及最后和最后的价格,这样它就不需要从 IBKR 中提取。话虽这么说,我对如何删除它有点困惑。
下面是来自 github 的 eWrapper.Code
eWrapper.data <- function(n) {
# internally updated data
# .data. <- character(8)
#
# get.data <- function() return(.data.)
#
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,7),ncol=7),0),
.Dimnames=list(NULL,
c("BidSize","BidPrice",
"AskPrice","AskSize",
"Last","LastSize","Volume")))),n))
eW$tickPrice <- function(curMsg, msg, timestamp, file, ...)
{
tickType = msg[3]
msg <- as.numeric(msg)
id <- msg[2] #as.numeric(msg[2])
data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
# data[[1]] <- rbind(data[[1]],.xts(matrix(rep(NA_real_,7),nc=7), Sys.time()))
nr.data <- NROW(data[[id]])
#data[[id]][1] <- as.numeric(Sys.time()) #timestamp
if(tickType == .twsTickType$BID) {
data[[id]][nr.data,1:2] <- msg[5:4]
} else
if(tickType == .twsTickType$ASK) {
data[[id]][nr.data,3:4] <- msg[4:5]
} else
if(tickType == .twsTickType$LAST) {
data[[id]][nr.data,5] <- msg[4]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
eW$tickSize <- function(curMsg, msg, timestamp, file, ...)
{
data <- eW$get.Data("data")
tickType = msg[3]
msg <- as.numeric(msg)
id <- as.numeric(msg[2])
# data[[1]] <- rbind(data[[1]],.xts(matrix(rep(NA_real_,7),nc=7), Sys.time()))
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
#data[[id]][1] <- as.numeric(Sys.time()) #timestamp
if(tickType == .twsTickType$BID_SIZE) {
data[[id]][nr.data,1] <- msg[4]
} else
if(tickType == .twsTickType$ASK_SIZE) {
data[[id]][nr.data,4] <- msg[4]
} else
if(tickType == .twsTickType$LAST_SIZE) {
data[[id]][nr.data,6] <- msg[4]
} else
if(tickType == .twsTickType$VOLUME) {
data[[id]][nr.data,7] <- msg[4]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}
我已经尝试删除它们拉入“音量”、“最后一个”和“最后一个大小”的行,如下所示,但这仍然不起作用。提前致谢。
eWrapper.data <- function(n) {
# internally updated data
# .data. <- character(8)
#
# get.data <- function() return(.data.)
#
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,7),ncol=7),0),
.Dimnames=list(NULL,
c("BidSize","BidPrice",
"AskPrice","AskSize",
"Last","LastSize","Volume")))),n))
eW$tickPrice <- function(curMsg, msg, timestamp, file, ...)
{
tickType = msg[3]
msg <- as.numeric(msg)
id <- msg[2] #as.numeric(msg[2])
data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
# data[[1]] <- rbind(data[[1]],.xts(matrix(rep(NA_real_,7),nc=7), Sys.time()))
nr.data <- NROW(data[[id]])
#data[[id]][1] <- as.numeric(Sys.time()) #timestamp
if(tickType == .twsTickType$BID) {
data[[id]][nr.data,1:2] <- msg[5:4]
} else
if(tickType == .twsTickType$ASK) {
data[[id]][nr.data,3:4] <- msg[4:5]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
eW$tickSize <- function(curMsg, msg, timestamp, file, ...)
{
data <- eW$get.Data("data")
tickType = msg[3]
msg <- as.numeric(msg)
id <- as.numeric(msg[2])
# data[[1]] <- rbind(data[[1]],.xts(matrix(rep(NA_real_,7),nc=7), Sys.time()))
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
#data[[id]][1] <- as.numeric(Sys.time()) #timestamp
if(tickType == .twsTickType$BID_SIZE) {
data[[id]][nr.data,1] <- msg[4]
} else
if(tickType == .twsTickType$ASK_SIZE) {
data[[id]][nr.data,4] <- msg[4]
}
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}