QuantMod getOptionChain“下标越界"错误 [英] QuantMod getOptionChain "subscript out of bounds" error
问题描述
我正在尝试使用 QuantMod 库中的函数 getOptionChain() 下载 VIX、SP500 和 Eurostoxx 50 的期权链,但以下方法不起作用:
I am trying to use the function getOptionChain() from the QuantMod library to download option chains for VIX, SP500 and Eurostoxx 50 but the following doesn't work:
library(quantmod)
VIX.OPT <- getOptionChain("^VIX")
我收到此错误:
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 I fix this?
推荐答案
我正在为此开发补丁.到目前为止,这是有效的,但您必须准确指定运营支出日期(每月的第三个星期五).
I'm working on a patch for this. So far this works, but you must specify the opex date exactly (3rd friday of month).
示例调用:
getOptionChain("^VIX")
$calls
Strike Bid Ask Last 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 is NOT 12/19, weird
$calls
Strike Bid Ask Last 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 the old version (I THINK).
此补丁引入了对 rjson 包的新依赖.
This patch introduces a new dependency on the rjson package.
要使用补丁,请安装 rjson (install.packages("rjson")),然后在加载 quantmod 后从 R 控制台运行以下命令:
To use the patch, install rjson (install.packages("rjson")) and then run the following from your R console AFTER loading quantmod:
getOptionChain.yahoo.patch <- function(Symbols, Exp, ...)
{
library("XML")
library("rjson")
millisToDate <- function(x)
{
return (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)
price <- 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
return (list(calls=chainToDf(calls), puts=chainToDf(puts), price = 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")
这篇关于QuantMod getOptionChain“下标越界"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!