[英]How do I get implied volatility from TWS into R using IBrokers?
目前我已經修改了我在此處找到的一些代碼,以讀取 R 中期權的買入/賣出價格。 然后我使用 calculateImpliedVolatility 將這些反饋給 TWS 以獲得隱含波動率。 看來我應該能夠在沒有使用.twsTickType$MODEL_OPTION 的第二步的情況下獲得它們。 我試圖修改用於買賣價格的相同代碼,但無法使其正常工作。 這是我嘗試過的:
eWrapper.data.Opt_Model <- function(n) {
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,8),nc=8),0),
.Dimnames=list(NULL,c("ImpVol","Delta","tv","pvdiv","gamma","vega",'theta','spot')))),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())
nr.data <- NROW(data[[id]])
if(tickType == .twsTickType$MODEL_OPTION) {
data[[id]][nr.data,1:8] <- msg[4:11]
}
#else
# if(tickType == .twsTickType$ASK) {
# data[[id]][nr.data,2] <- msg[4]
# }
eW$assign.Data("data", data)
c(curMsg, msg)
}
return(eW)
}
這花了一些時間,但我得到了它的工作。
> eWrapper.data.Opt_Model <- function(n) { eW <- eWrapper(NULL) # use
> basic template eW$assign.Data("data",
> rep(list(structure(.xts(matrix(rep(NA_real_,8),nc=8),0),
> .Dimnames=list(NULL,c('modelOption: impVol: ',' delta: ',' modelPrice:
> ',' pvDiv ',' gamma: ',' vega: ',' theta: ',' undPrice: ')))),n))
> eW$tickOptionComputation <- 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())
> nr.data <- NROW(data[[id]])
> if(tickType == .twsTickType$MODEL_OPTION) {
> data[[id]][nr.data,1:8] <- msg[4:11]
> }
> #else
> # if(tickType == .twsTickType$ASK) {
> # data[[id]][nr.data,2] <- msg[4]
> # }
> eW$assign.Data("data", data)
> c(curMsg, msg) }
> return(eW) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.