0

目前我已经修改了我在这里找到的一些代码,以读取 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)
}
4

1 回答 1

0

这花了一些时间,但我得到了它的工作。

> 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) }
于 2020-07-31T19:03:32.647 回答