r - QuantMod getOptionChain "subscript out of bounds" error -



r - QuantMod getOptionChain "subscript out of bounds" error -

i trying utilize function getoptionchain() quantmod library download alternative chains vix, sp500 , eurostoxx 50 next doesn't work:

library(quantmod) vix.opt <- getoptionchain("^vix")

i'm getting error:

error in lapply(strsplit(opt, "<tr>"), function(.) gsub(",", "", gsub("n/a", : subscript out of bounds in addition: warning message: in readlines(paste(paste("http://finance.yahoo.com/q/op?s", symbols, : incomplete final line found on 'http://finance.yahoo.com/q/op?s=^vix+options'

how can prepare this?

i'm working on patch this. far works, must specify opex date (3rd fri of month).

example calls:

getoptionchain("^vix") $calls strike bid inquire lastly vol oi vix141119c00010000 10.0 7.10 7.40 7.10 18 1974 vix141119c00010500 10.5 6.60 6.90 6.90 330 510 vix141119c00011000 11.0 6.10 6.40 6.10 108 1469 ... getoptionchain("^vix", '2014-12-16') #note vix dec expiry not 12/19, weird $calls strike bid inquire lastly vol oi vix141217c00010000 10.0 7.10 7.40 7.50 1 964 vix141217c00011000 11.0 6.20 6.40 6.53 100 673 vix141217c00012000 12.0 5.20 5.40 5.50 4 873 ...

format matches old version (i think).

this patch introduces new dependency on rjson package.

to utilize patch, install rjson (install.packages("rjson")) , run next r console after loading quantmod:

getoptionchain.yahoo.patch <- function(symbols, exp, ...) { library("xml") library("rjson") millistodate <- function(x) { homecoming (as.date(x / 86400000, origin = "1970-01-01")) } datetomillis <- function(x) { as.numeric(x+1) * 86400000 /1000 } parse.expiry <- function(x) { if(is.null(x)) return(null) if(is.character(x)) { x <- as.date(x) } if(inherits(x, "date") || inherits(x, "posixt")) return(datetomillis(x)) return(null) } getoptionchainjson <- function(sym, exp) { if(missing(exp)) { url <- paste("http://finance.yahoo.com/q/op?s",sym,sep="=") opt <- readlines(url) } else { url <- paste("http://finance.yahoo.com/q/op?s=",sym,"&date=",parse.expiry(exp),sep="") opt <- readlines(url) } opt <- opt[grep("percentchangeraw", opt)] opt <- unlist(strsplit(opt, "<script>")) json <- opt[3] json <- gsub("<script>", "", json) json <- gsub("</script>", "", json) json <- gsub(";", "", json) json <- unlist(strsplit(json, "="))[4] j <- fromjson(json) cost <- j$models$applet_model$data$optiondata$quote$regularmarketprice calls <- j$models$applet_model$data$optiondata$options$calls puts <- j$models$applet_model$data$optiondata$options$puts homecoming (list(calls=chaintodf(calls), puts=chaintodf(puts), cost = price, sym = sym)) } chaintodf <- function(thelist) { x <- do.call(rbind.data.frame, thelist) rownames(x) <- x$contractsymbol y <- x[,c("strike", "bid", "ask", "lastprice", "volume", "openinterest")] thenames <- c("strike", "bid", "ask", "last", "vol", "oi") names(y) <- thenames for(i in thenames) { y[,i] <- as.numeric(as.character(y[,i])) } #y$contractsymbol <- as.character(x$contractsymbol) #y$expiration <- millistodate(as.numeric(as.character(x$expiration)) * 1000) return(y) } getoptionchainjson(symbols, exp) } assigninnamespace("getoptionchain.yahoo", getoptionchain.yahoo.patch, "quantmod")

r yahoo quantmod

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -