getOptionChain() 不返回 R 中的索引选项数据
getOptionChain() not returning Index Options Data in R
我正在尝试通过 quantmod
:
调用所有 ^SPX
选项表
library("quantmod")
# Returns the front month
getOptionChain("^SPX")
# does not return all the Option Tables
getOptionChain("^SPX", Exp=NULL)
我得到的错误信息:
> getOptionChain("^SPX", NULL)
Error in (function (object = nm, nm) :
attempt to set an attribute on NULL
如何解决这个问题,以便调用所有选项表?
"^SPX"
的最后两个到期日分别没有看涨期权和看跌期权的数据。您可以应用以下补丁来解决此问题(以及数字数据中的逗号问题,这导致它们保留为字符):
diff --git a/R/getOptionChain.R b/R/getOptionChain.R
index fb490ef..b3cca51 100644
--- a/R/getOptionChain.R
+++ b/R/getOptionChain.R
@@ -20,13 +20,17 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)
XML::xmlValue(x)
}
}
- NewToOld <- function(x) {
+ NewToOld <- function(x, nm) {
+ if(is.null(x))
+ return(x)
# clean up colnames, in case there's weirdness in the HTML
- x <- setNames(x, make.names(names(x)))
+ x <- setNames(x, make.names(nm))
# set cleaned up colnames to current output colnames
d <- with(x, data.frame(Strike=strike, Last=last, Chg=change,
Bid=bid, Ask=ask, Vol=volume, OI=openinterest,
row.names=`contractname`, stringsAsFactors=FALSE))
+ # remove commas from the numeric data
+ d[] <- lapply(d, gsub, pattern=",", replacement="", fixed=TRUE)
d[] <- lapply(d, type.convert, as.is=TRUE)
d
}
@@ -100,8 +104,7 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)
dftables <- XML::xmlApply(XML::getNodeSet(tbl, xpaths$tables), XML::readHTMLTable, stringsAsFactors=FALSE)
names(dftables) <- table.names
- dftables <- mapply(setNames, dftables, table.headers, SIMPLIFY=FALSE)
- dftables <- lapply(dftables, NewToOld)
+ dftables <- mapply(NewToOld, x=dftables, nm=table.headers, SIMPLIFY=FALSE)
dftables
}
我正在尝试通过 quantmod
:
^SPX
选项表
library("quantmod")
# Returns the front month
getOptionChain("^SPX")
# does not return all the Option Tables
getOptionChain("^SPX", Exp=NULL)
我得到的错误信息:
> getOptionChain("^SPX", NULL)
Error in (function (object = nm, nm) :
attempt to set an attribute on NULL
如何解决这个问题,以便调用所有选项表?
"^SPX"
的最后两个到期日分别没有看涨期权和看跌期权的数据。您可以应用以下补丁来解决此问题(以及数字数据中的逗号问题,这导致它们保留为字符):
diff --git a/R/getOptionChain.R b/R/getOptionChain.R
index fb490ef..b3cca51 100644
--- a/R/getOptionChain.R
+++ b/R/getOptionChain.R
@@ -20,13 +20,17 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)
XML::xmlValue(x)
}
}
- NewToOld <- function(x) {
+ NewToOld <- function(x, nm) {
+ if(is.null(x))
+ return(x)
# clean up colnames, in case there's weirdness in the HTML
- x <- setNames(x, make.names(names(x)))
+ x <- setNames(x, make.names(nm))
# set cleaned up colnames to current output colnames
d <- with(x, data.frame(Strike=strike, Last=last, Chg=change,
Bid=bid, Ask=ask, Vol=volume, OI=openinterest,
row.names=`contractname`, stringsAsFactors=FALSE))
+ # remove commas from the numeric data
+ d[] <- lapply(d, gsub, pattern=",", replacement="", fixed=TRUE)
d[] <- lapply(d, type.convert, as.is=TRUE)
d
}
@@ -100,8 +104,7 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)
dftables <- XML::xmlApply(XML::getNodeSet(tbl, xpaths$tables), XML::readHTMLTable, stringsAsFactors=FALSE)
names(dftables) <- table.names
- dftables <- mapply(setNames, dftables, table.headers, SIMPLIFY=FALSE)
- dftables <- lapply(dftables, NewToOld)
+ dftables <- mapply(NewToOld, x=dftables, nm=table.headers, SIMPLIFY=FALSE)
dftables
}