【问题标题】:How to get the latest option prices on SPX using IBrokers in R如何在 R 中使用 IBrokers 获取 SPX 的最新期权价格
【发布时间】:2018-08-28 23:14:07
【问题描述】:

我正在尝试使用以下代码获取给定行使价 (2700) 和到期 (2018 年 11 月 15 日) 的 SPX 期权看跌期权价格:

require("IBrokers")
tws <- twsConnect()
opt=twsOPT("",symbol="SPX",right="P", strike="2700", expiry="20181115")
test1=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)

下面报告了两个函数 eWrapper.data.Last 和 snapShot。

我明白了:

2 -1 2104 Connessione con il Server dei dati di mercato è OK:usfuture

2 -1 2104 Connessione con il Server dei dati di mercato è OK:eufarm

2 -1 2104 Connessione con il Server dei dati di mercato è OK:usopt

2 -1 2104 Connessione con il Server dei dati di mercato è OK:usfarm.us

2 -1 2106 Connessione con il HMDS 数据场 è OK:euhmds

2 -1 2106 Connessione con il HMDS 数据场 è OK:ushmds

2 1 10090 Parte dei dati di mercato richiesti non sono sottoscritti。 Tick indipendenti dalle sottoscrizioni sono ancora attivi.Dati di mercatodifferiti non disponibili.SPX S&P 500 Stock Index/TOP/ALL

连接似乎有效,但没有创建对象。

eWrapper.data.Last <- function(n) {
  eW <- eWrapper(NULL)  # use basic template
  eW$assign.Data("data", 
rep(list(structure(.xts(matrix(rep(NA_real_,2),nc=2),0),

.Dimnames=list(NULL,c("LastSize","Last")))),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$LAST) {
      data[[id]][nr.data,2] <- 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])
    attr(data[[id]],"index") <- as.numeric(Sys.time())
    nr.data <- NROW(data[[id]])
    if(tickType == .twsTickType$LAST_SIZE) {
      data[[id]][nr.data,1] <- msg[4]
    } 
    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]]
  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))
      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.na)))
        return(do.call(rbind, lapply(eWrapper$.Data$data,
                                     as.data.frame)))
    }
  }
}

【问题讨论】:

    标签: r ibrokers


    【解决方案1】:

    如果我翻译这条消息:

    2 1 10090 Parte dei dati di mercato richiesti non sono sottoscritti。 Tick indipendenti dalle sottoscrizioni sono ancora attivi.Dati di mercatodifferiti non disponibili.SPX S&P 500 Stock Index/TOP/ALL

    我得到了这个翻译:

    部分所需市场数据未订阅。独立的 报价仍然活跃。SPX S&P 500 股票指数/TOP/ALL 延期市场 数据不可用

    这意味着您错过了一些市场数据订阅。你需要检查一下。尝试运行一些期权合约并获取它们的详细信息。

    如果我运行代码:

    library(IBrokers)
    tws <- twsConnect()
    
    serverVersion(tws)
    [1] "76"
    
    opt <- twsOption(local = "", symbol = "SPX", expiry="20181115", strike="2700", right="P")
    details <- reqContractDetails(tws, opt)
    
    details[[1]]$validExchanges
    [1] "SMART" "CBOE" 
    

    我看到CBOE是这个合约的交易交易所。您需要查看您是否拥有正确的市场数据订阅才能在 CBOE 上查看期权合约。我认为您需要订阅 OPRA 的市场数据。

    【讨论】:

    • 感谢您的评论。我检查了我的数据订阅,你提到的 R 消息不是很清楚,因为 OPRA(美国期权交易所)已经包括在内。
    • 您是否尝试从获取合同详细信息中得到结果?
    • 是的,我得到了和你一样的结果。此外,我再次尝试了使用 eWrapper.data.Last 和 snapShot 函数的原始代码,我得到了仅几个执行价格的最后一个看跌期权价格,因为其他 R 不会停止运行(并标记消息“所需市场的一部分数据未订阅。独立报价仍然活跃。SPX S&P 500 股票指数/TOP/ALL 延期市场数据不可用")。这会导致多个到期和罢工的循环出现问题......
    • 对于您获得此订单的那些,请检查您运行此代码的时间。 afaik IB 没有 api 的延迟市场数据。尝试查看您遇到此问题的代码,然后查看您在 TWS 中可以看到的内容。我必须说我很少使用Ibrokers的这一部分,因为如果不中断R进程,我永远无法正确中断它,然后我的对象就没有被创建。我只用它来将数据推送到文件中。您可能想在 r-sig-finance 邮件列表上发布您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多