2015-09-05 15 views
1

返回指數期權數據我想打電話給所有的^SPX選項表並通過quantmodgetOptionChain()不是R中

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 

如何解決這個問題,所以我可以調用所有選項表?

+0

升級到CRAN上的最新quantmod。 –

+0

'> sessionInfo( 「quantmod」) ř版本3.1.2(2014年10月31日) 平臺:x86_64的-蘋果darwin13.4.0(64位) 區域設置: [1]的en_US.UTF -8- /en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 附鹼包: 字符(0) 其他附軟件包: [1] quantmod_0.4 -5' @JoshuaUlrich我認爲我是最新的? – Rime

回答

0

"^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 
} 
+0

工程就像一個魅力!謝謝@JoshuaUlrich – Rime

+0

我只是[把它推到了GitHub上的開發版本](https://github.com/joshuaulrich/quantmod/commit/77ac0949cd6ce941b35f8571fca84483ab5b9216)。它將在CRAN的下一個版本中發佈。 –