gpt4 book ai didi

r - QuantMod getOptionChain "subscript out of bounds"错误

转载 作者:行者123 更新时间:2023-12-01 23:50:58 24 4
gpt4 key购买 nike

我正在尝试使用 QuantMod 库中的函数 getOptionChain() 来下载 VIX、SP500 和 Eurostoxx 50 的期权链,但以下方法不起作用:

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'

我该如何解决这个问题?

最佳答案

我正在为此开发一个补丁。到目前为止这是可行的,但您必须准确指定运营支出日期(每月的第三个星期五)。

调用示例:

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 ...

格式与旧版本匹配(我认为)。

这个补丁引入了对 rjson 的新依赖包裹。

要使用补丁,请安装 rjson (install.packages("rjson")),然后在加载 quantmod 后从 R 控制台运行以下命令:

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

关于r - QuantMod getOptionChain "subscript out of bounds"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com