开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Ibrokers package in R - Get option prices


Simone
 

Hi everyone,
?
I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

The final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:
?
require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)

where the two functions eWrapper.data.Last and snapShot are below reported.
?
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, )))
        return(do.call(rbind, lapply(eWrapper$.Data$data,
                                     as.data.frame)))
    }
  }
}

I get this on the console:??

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:
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 last R message is not very clear since OPRA market data (which includes SPX) is already included in my market data subscription.

In some cases, I get the last put option price for only few strike prices, for others R does not stop running and stamp the 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

This lead to problems in loops with multiple expirations and strikes.

Any help would be greatly appreciated.

Simone


 

My Italian is terrible, but I think it means you are not subscribed (lit. under written) and it suggests that you activate it.

Could that be the case, that you have not subscribed or activated the necessary subscription??

Someone with better Italian will probably be able to help more


M


On Fri, 31 Aug 2018, 09:26 Simone, <simone.gallo1990@...> wrote:
Hi everyone,
?
I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

The final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:
?
require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)

where the two functions eWrapper.data.Last and snapShot are below reported.
?
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, ...<span class="m_5544574937103192364gmail-pun" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; vertical-align: baseline; box-sizing: inherit; color: #3033


Simone
 

Hi Mark,

I've checked my?data subscriptions,?OPRA (which includes SPX) is already included.?Do you think other data subscriptions are required for SPX?

I tried also during trading hours to avoid deferred market data but the result is the same.

Is there an alternative way to get historical option prices (specifying for example yesterday at a particular hour)??

Thanks,
Simone


Il giorno ven 31 ago 2018 alle ore 12:23 mark collins <mark.collins@...> ha scritto:
My Italian is terrible, but I think it means you are not subscribed (lit. under written) and it suggests that you activate it.

Could that be the case, that you have not subscribed or activated the necessary subscription??

Someone with better Italian will probably be able to help more


M

On Fri, 31 Aug 2018, 09:26 Simone, <simone.gallo1990@...> wrote:
Hi everyone,
?
I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

The final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:
?
require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)

where the two functions eWrapper.data.Last and snapShot are below reported.
?
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 


 

my italian is bad, too. But may this is issue be related to spx-index-and spx-futures different subscriptions? I mixed this type of assets in the past as well. Not sure if this is of any help. But maybe it is worth of checking...



Simone <simone.gallo1990@...> schrieb am 12:42 Freitag, 31.August 2018:


Hi Mark,

I've checked my?data subscriptions,?OPRA (which includes SPX) is already included.?Do you think other data subscriptions are required for SPX?

I tried also during trading hours to avoid deferred market data but the result is the same.

Is there an alternative way to get historical option prices (specifying for example yesterday at a particular hour)??

Thanks,
Simone


Il giorno ven 31 ago 2018 alle ore 12:23 mark collins <mark.collins@...> ha scritto:
My Italian is terrible, but I think it means you are not subscribed (lit. under written) and it suggests that you activate it.

Could that be the case, that you have not subscribed or activated the necessary subscription??

Someone with better Italian will probably be able to help more


M

On Fri, 31 Aug 2018, 09:26 Simone, <simone.gallo1990@...> wrote:
Hi everyone,
?
I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

The final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:
?
require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)

where the two functions eWrapper.data.Last and snapShot are below reported.
?
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 



 

开云体育

Simone

?

If you don’t specify the exchange, you get the SPX options on CBOE, which would indeed be a separate subscription.

?

Richard

?

?

From: [email protected] <[email protected]> On Behalf Of Simone
Sent: 30 August 2018 23:42
To: [email protected]
Subject: [TWS API] Ibrokers package in R - Get option prices

?

Hi everyone,

?

I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

T
he final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:

?

require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)


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

?

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, )))
??????? return(do.call(rbind, lapply(eWrapper$.Data$data,
?? ??????????????????????????????????as.data.frame)))
??? }
? }
}

I get this on the console:??

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:
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 last R message is not very clear since OPRA market data (which includes SPX) is already included in my market data subscription.

In some cases, I get the last put option price for only few strike prices, for others R does not stop running and stamp the 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

This lead to problems in loops with multiple expirations and strikes.

Any help would be greatly appreciated.

Simone


Simone
 

Hi Richard?

I relate to options on spx index on CBOE, for which I have the following market data subscription:

OPRA (US Options Exchanges):?Provide options data of AMEX, ARCA, BATS, BOX, BSE, CBOE2, CBOE, ISE NASDAQ and PHLX.

Do I need a different data subscription?

Thanks
Simone


Il giorno ven 31 ago 2018 alle ore 13:34 Richard L King <rlking@...> ha scritto:

Simone

?

If you don’t specify the exchange, you get the SPX options on CBOE, which would indeed be a separate subscription.

?

Richard

?

?

From: [email protected] <[email protected]> On Behalf Of Simone
Sent: 30 August 2018 23:42
To: [email protected]
Subject: [TWS API] Ibrokers package in R - Get option prices

?

Hi everyone,

?

I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

T
he final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:

?

require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)


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

?

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) <span style="font-family:"inherit&


 

开云体育

I can’t see anything in the market data subscriptions page called ‘OPRA (US Options Exchanges)’, so I’m not sure what you’re referring to.

?

I believe the subscription you need is this:

?

‘US Equity and Options Add-On Streaming Bundle

Includes streaming realtime quotes for NYSE (CTA/Network A), AMEX (CTA/Network B), NASDAQ (UTP/Network C), and OPRA (US Options). US Securities Snapshot and Futures Value Bundle is required prior to activating this service.’

?

I have that subscription and can access the SPX options with no problems.

?

Richard

?

?

?

From: [email protected] <[email protected]> On Behalf Of Simone
Sent: 31 August 2018 13:20
To: [email protected]
Subject: Re: [TWS API] Ibrokers package in R - Get option prices

?

Hi Richard?

?

I relate to options on spx index on CBOE, for which I have the following market data subscription:

OPRA (US Options Exchanges):?Provide options data of AMEX, ARCA, BATS, BOX, BSE, CBOE2, CBOE, ISE NASDAQ and PHLX.

Do I need a different data subscription?

?

Thanks

Simone

?

Il giorno ven 31 ago 2018 alle ore 13:34 Richard L King <rlking@...> ha scritto:

Simone

?

If you don’t specify the exchange, you get the SPX options on CBOE, which would indeed be a separate subscription.

?

Richard

?

?

From: [email protected] <[email protected]> On Behalf Of Simone
Sent: 30 August 2018 23:42
To: [email protected]
Subject: [TWS API] Ibrokers package in R - Get option prices

?

Hi everyone,

?

I'm trying to get SPX put option prices for a given strike and expiration using the IBrokers package in R.

T
he final aim is to fill an option price chain from IB. Not necessarily with the latest option prices available, but also with bid/ask prices for a given date/hour would be useful.

I'm using the following code:

?

require(IBrokers)
tws=twsConnect()
ticker="SPX"
exp="20181115"
k=2700
opt=twsOption(local="",symbol=ticker,expiry=exp,strike=k,right="P")
optPrice=reqMktData(tws,opt,eventWrapper=eWrapper.data.Last(1),CALLBACK=snapShot)


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

?

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) <span style="font-family:"inherit&


 

Simone,
If you have the OPRA subscription it covers the options but not the underlying SPX index. For the index you would need CBOE Streaming Market Indexes or US Securities Snapshot and Futures Value Bundle.?
?
So with only the options subscription TWS can return some tick types such as bid/ask/last, but can't return the greek values which require the real time index price to calculate. Thats why it returns the message:?
?
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"
?
However you can still receive the bid/ask/last/ values for the options. I would double-check if those are being returned. Please note there won't be a 'last' price in the current day if the option hasn't traded.
Josh (IBKR)


Simone
 

Richard, Josh,

thank you so much for the explanation.

With OPRA subscription (using the R code I posted in the first mail) I get the last values for few option strikes/expirations only (maybe for those contracts that are traded).

Now, I've just made the subscription to US Equity and Options Add-On Streaming Bundle.

If I run the R code with the new subscription I do not get the following message anymore:
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"

However, like before the code does not stop running and does not create objects (it's 15 min I run the code, I'll wait), I don't know if this is a problem related to the R code I'm using.

Can I overcome the limitation of the last price getting only bid/ask prices for a particular day? I just need to bring in R an option chain.

Thanks
Simone


Il giorno ven 31 ago 2018 alle ore 16:14 Josh via Groups.Io <jb201448=[email protected]> ha scritto:
Simone,
If you have the OPRA subscription it covers the options but not the underlying SPX index. For the index you would need CBOE Streaming Market Indexes or US Securities Snapshot and Futures Value Bundle.?
?
So with only the options subscription TWS can return some tick types such as bid/ask/last, but can't return the greek values which require the real time index price to calculate. Thats why it returns the message:?
?
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"
?
However you can still receive the bid/ask/last/ values for the options. I would double-check if those are being returned. Please note there won't be a 'last' price in the current day if the option hasn't traded.
Josh (IBKR)


 

Well, your R code does have a while(TRUE) loop control so I'd not expect it to stop unless something hits the conditions for breaking the loop.... it's probably stuck there or somewhere similar. Are you printing out the process steps as it goes?

Best wishes,

M

On 31 August 2018 at 15:43, Simone <simone.gallo1990@...> wrote:
Richard, Josh,

thank you so much for the explanation.

With OPRA subscription (using the R code I posted in the first mail) I get the last values for few option strikes/expirations only (maybe for those contracts that are traded).

Now, I've just made the subscription to US Equity and Options Add-On Streaming Bundle.

If I run the R code with the new subscription I do not get the following message anymore:
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"

However, like before the code does not stop running and does not create objects (it's 15 min I run the code, I'll wait), I don't know if this is a problem related to the R code I'm using.

Can I overcome the limitation of the last price getting only bid/ask prices for a particular day? I just need to bring in R an option chain.

Thanks
Simone

Il giorno ven 31 ago 2018 alle ore 16:14 Josh via Groups.Io <jb201448=[email protected]> ha scritto:
Simone,
If you have the OPRA subscription it covers the options but not the underlying SPX index. For the index you would need CBOE Streaming Market Indexes or US Securities Snapshot and Futures Value Bundle.?
?
So with only the options subscription TWS can return some tick types such as bid/ask/last, but can't return the greek values which require the real time index price to calculate. Thats why it returns the message:?
?
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"
?
However you can still receive the bid/ask/last/ values for the options. I would double-check if those are being returned. Please note there won't be a 'last' price in the current day if the option hasn't traded.
Josh (IBKR)




--
(T) +44 (0) 131 2202413 ?? (M) +44 (0) 7528 551604 ? (W)
Gulfstream Software - Winner Risk Management Awards 2010
This message is subject to :


Simone
 

I found the functions eWrapper.data.Last and snapShot?on web so I'm not truly able to understand correctly what is the problem.
For the moment, I solved using the basic functions:

? ? opt=twsOption(local="",symbol="SPX",expiry=exp,strike=k,right="P")
? ? optPrice=reqMktData(tws,opt,snapshot = T)

It takes some time to get option prices (about 10-15 seconds for each contract) but at least works.
I'll try to find an alternative way to speeding up the process.?


Il giorno ven 31 ago 2018 alle ore 17:23 mark collins <mark.collins@...> ha scritto:
Well, your R code does have a while(TRUE) loop control so I'd not expect it to stop unless something hits the conditions for breaking the loop.... it's probably stuck there or somewhere similar. Are you printing out the process steps as it goes?

Best wishes,

M

On 31 August 2018 at 15:43, Simone <simone.gallo1990@...> wrote:
Richard, Josh,

thank you so much for the explanation.

With OPRA subscription (using the R code I posted in the first mail) I get the last values for few option strikes/expirations only (maybe for those contracts that are traded).

Now, I've just made the subscription to US Equity and Options Add-On Streaming Bundle.

If I run the R code with the new subscription I do not get the following message anymore:
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"

However, like before the code does not stop running and does not create objects (it's 15 min I run the code, I'll wait), I don't know if this is a problem related to the R code I'm using.

Can I overcome the limitation of the last price getting only bid/ask prices for a particular day? I just need to bring in R an option chain.

Thanks
Simone

Il giorno ven 31 ago 2018 alle ore 16:14 Josh via Groups.Io <jb201448=[email protected]> ha scritto:
Simone,
If you have the OPRA subscription it covers the options but not the underlying SPX index. For the index you would need CBOE Streaming Market Indexes or US Securities Snapshot and Futures Value Bundle.?
?
So with only the options subscription TWS can return some tick types such as bid/ask/last, but can't return the greek values which require the real time index price to calculate. Thats why it returns the message:?
?
"Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.SPX S&P 500 Stock Index/TOP/ALL"
?
However you can still receive the bid/ask/last/ values for the options. I would double-check if those are being returned. Please note there won't be a 'last' price in the current day if the option hasn't traded.
Josh (IBKR)




--
(T) +44 (0) 131 2202413 ?? (M) +44 (0) 7528 551604 ? (W)
Gulfstream Software - Winner Risk Management Awards 2010
This message is subject to :