2012-11-08 26 views
1

我想通過下載此頁面上的所有CSV文件(http://cfe.cboe.com/Products/historicalVIX.aspx)獲取VIX期貨的歷史價格。下面是我使用這樣做代碼:從CBOE下載VIX期貨價格

library(XML) 

#Extract all links for url 
url <- "http://cfe.cboe.com/Products/historicalVIX.aspx" 
doc <- htmlParse(url) 
links <- xpathSApply(doc, "//a/@href") 
free(doc) 

#Filter out URLs ending with csv and complete the link. 
links <- links[substr(links, nchar(links) - 2, nchar(links)) == "csv"] 
links <- paste("http://cfe.cboe.com", links, sep="") 

#Peform read.csv on each url in links, skipping the first two URLs as they are not relevant. 
c <- lapply(links[-(1:2)], read.csv, header = TRUE) 

我得到的錯誤:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : 
    more columns than column names 

經進一步調查,我意識到這是因爲有些CSV文件的格式不同。如果我手動加載URL links[9],我看到第一排有此免責聲明:

CFE data is compiled for the .......use of CFE data is subject to the Terms and Conditions of CBOE's Websites. 

大多數其他文件(例如links[8]links[10])的都很好,因此這似乎已隨機插入。是否有一些R魔法可以解決這個問題?

謝謝。

回答

3

我在我的qmao程序包(程序包中的getSymbols函數)中有一個getSymbols.cfe方法,這將使這更容易。

#install.packages('qmao', repos='http://r-forge.r-project.org') 
library(qmao) 

這是從?getSymbols.cfe的例子部分(請閱讀幫助頁面的函數,你可能想比默認不同的幾個參數)

getSymbols(c("VX_U11", "VX_V11"),src='cfe') 
#all contracts expiring in 2010 and 2011. 
getSymbols("VX",Months=1:12,Years=2010:2011,src='cfe') 
#getSymbols("VX",Months=1:12,Years=10:11,src='cfe') #same 

而且它不只是對於VIX

getSymbols(c("VM","GV"),src='cfe') #The mini-VIX and Gold vol contracts expiring this month 

如果你不熟悉的getSymbols,默認情況下它存儲在您的.GlobalEnv數據並返回的名稱被保存的對象。

> getSymbols("VX_Z12", src='cfe') 
[1] "VX_Z12" 

> tail(VX_Z12) 
      VX_Z12.Open VX_Z12.High VX_Z12.Low VX_Z12.Close VX_Z12.Settle VX_Z12.Change VX_Z12.Volume VX_Z12.EFP VX_Z12.OpInt 
2012-10-26  19.20  19.35  18.62  18.87   18.9   0.0   22043   15  71114 
2012-10-31  18.55  19.50  18.51  19.46   19.5   0.6   46405  319  89674 
2012-11-01  19.35  19.35  17.75  17.87   17.9   -1.6   40609  2046  95720 
2012-11-02  17.90  18.65  17.55  18.57   18.6   0.7   42592  1155  100691 
2012-11-05  18.60  20.15  18.43  18.86   18.9   0.3   28136  110  102746 
2012-11-06  18.70  18.85  17.75  18.06   18.1   -0.8   35599  851  110638 

編輯

我現在明白,我沒有回答你的問題,而是指出你另一種方式來獲得同樣的錯誤!使代碼正常工作的一種簡單方法是製作read.csv的包裝,它使用readLines來查看第一行是否包含免責聲明;如果是,則跳過第一行,否則照常使用read.csv

myRead.csv <- function(x, ...) { 
    if (grepl("Terms and Conditions", readLines(x, 1))) { #is the first row the disclaimer? 
    read.csv(x, skip=1, ...) 
    } else read.csv(x, ...) 
} 
L <- lapply(links[-(1:2)], myRead.csv, header = TRUE) 

我也將該補丁應用於getSymbols.cfe。你可以使用svn checkout獲得最新版本的qmao(1.3.11)(如果你需要幫助,請參閱this post),或者,你可以等到R-Forge爲你建立它,這通常很快發生,但可能會佔用幾天。

+0

謝謝。我不需要重新發明輪子。 – mchangun

+0

感謝您指出VX_N13在第一行有免責聲明。我會盡力修補,以儘快處理。 – GSee

+0

@GSee有沒有像getQuote.cfe可用的東西。我環顧四周,無法找到它。我在問,因爲使用getSmbols.cfe提供的數據有點晚了「請注意:CFE數據直到上午10:00左右C.T.在下一個工作日纔可用。」問題在於何處獲得(接近)實時數據。一個明顯的地方是盈透證券......任何其他想法(谷歌,雅虎,...)?日Thnx。 – Samo