0

I'm trying to get SPX option put prices for a given strike (2700) and expiration (15/11/2018) using the following code:

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)

where the two functions eWrapper.data.Last and snapShot are below reported.

I get this:

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 data farm è OK:euhmds

2 -1 2106 Connessione con il HMDS data farm è OK:ushmds

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

The connection seems to work but no object are created.

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)))
    }
  }
}

1 Answers1

0

If I translate this message:

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

I get this translation:

Part of the required market data is not subscribed to. Independent ticks are still active.SPX S&P 500 Stock Index/TOP/ALL deferred market data not available

Which means that you miss some market data subscription. You need to check this. Try running a few option contracts and get their details.

If I run the code:

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" 

I see that CBOE is the trading exchange of this contract. You need to see if you have the correct market data subscription to see option contracts on CBOE. I think you need to have market data subscription to OPRA.

phiver
  • 23,048
  • 14
  • 44
  • 56
  • Thanks for your comment. I checked my data subscriptions, the R message you mentioned is not very clear since OPRA (US Options Exchanges) is already included. – A. Hoffman Aug 29 '18 at 20:24
  • DId you try and get a result back from getting the contractdetails? – phiver Aug 30 '18 at 07:47
  • Yes I get the same your results. Moreover, I tried again the original code with the eWrapper.data.Last and snapShot functions and I get the last put option price for only few strike prices, for others R does not stop running (and stamp the message "Part of the required market data is not subscribed to. Independent ticks are still active.SPX S&P 500 Stock Index/TOP/ALL deferred market data not available"). This lead a problem in loops with multiple expiries and strikes... – A. Hoffman Aug 30 '18 at 10:19
  • For the ones where you get this order, check the timing of when you are running this code. afaik IB does not have deferred market data for the api's. Try to see with which tickers you get this issue and then check what you can see in TWS. I must say that I seldomly use this part of Ibrokers, because I can never interrupt it correctly without interrupting the R process and then my object is not created. I only use it to push data to a file. You might want to post your question on r-sig-finance mail list. – phiver Aug 30 '18 at 11:34