Json 使用R从Google Finance抓取期权数据
我最近看到了一个函数,它将 它工作得很好,但我正在尝试使用该函数调用20种不同的股票(尽管其中一些股票没有期权数据),并将它们保存为列表 我已修改了Json 使用R从Google Finance抓取期权数据,json,r,options,scrape,Json,R,Options,Scrape,我最近看到了一个函数,它将 它工作得很好,但我正在尝试使用该函数调用20种不同的股票(尽管其中一些股票没有期权数据),并将它们保存为列表 我已修改了getOptionQuote功能以满足我的需要: 上述站点提供的功能: # installs RCurl and jsonlite packages. will prompt you to select mirror for download install.packages("RCurl");library("RCurl") install.pac
getOptionQuote
功能以满足我的需要:
上述站点提供的功能:
# installs RCurl and jsonlite packages. will prompt you to select mirror for download
install.packages("RCurl");library("RCurl")
install.packages("jsonlite");library("jsonlite")
# ******************************************************************
getOC2 <- function(symbol){
output = list()
url = paste('http://www.google.com/finance/option_chain?q=', symbol, '&output=json', sep = "")
x = getURL(url)
fix = fixJSON(x)
json = fromJSON(fix)
numExp = dim(json$expirations)[1]
if(length(numExp)!= 0){
for(i in 1:numExp){
# download each expirations data
y = json$expirations[i,]$y
m = json$expirations[i,]$m
d = json$expirations[i,]$d
expName = paste(y, m, d, sep = "_")
if (i > 1){
url = paste('http://www.google.com/finance/option_chain?q=', symbol, '&output=json&expy=', y, '&expm=', m, '&expd=', d, sep = "")
json = fromJSON(fixJSON(getURL(url)))
}
output[[paste(expName, "calls", sep = "_")]] = json$calls
output[[paste(expName, "puts", sep = "_")]] = json$puts
}}
assign(paste(symbol),do.call(rbind,output))
}
fixJSON <- function(json_str){
stuff = c('cid','cp','s','cs','vol','expiry','underlying_id','underlying_price',
'p','c','oi','e','b','strike','a','name','puts','calls','expirations',
'y','m','d')
for(i in 1:length(stuff)){
replacement1 = paste(',"', stuff[i], '":', sep = "")
replacement2 = paste('\\{"', stuff[i], '":', sep = "")
regex1 = paste(',', stuff[i], ':', sep = "")
regex2 = paste('\\{', stuff[i], ':', sep = "")
json_str = gsub(regex1, replacement1, json_str)
json_str = gsub(regex2, replacement2, json_str)
}
return(json_str)
}
我没有收到任何错误,但是OC
的长度应该是20,但是它的长度只有1,并且生成:list(NULL)
会话信息:
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_1.0.0 plyr_1.8.3 doMC_1.3.3 doParallel_1.0.8 iterators_1.0.7 foreach_1.4.2 pbapply_1.1-1 jsonlite_0.9.17
[9] RCurl_1.95-4.7 bitops_1.0-6
loaded via a namespace (and not attached):
[1] Rcpp_0.12.0 lattice_0.20-33 codetools_0.2-14 zoo_1.7-12
[5] grid_3.2.2 magrittr_1.5 PerformanceAnalytics_1.4.3541 stringi_0.5-5
[9] PortfolioAnalytics_1.0.3636 xts_0.9-7 blotter_0.8.19 tools_3.2.2
[13] compiler_3.2.2 quantstrat_0.8.2
最近的函数是通过Quantmod,但当前仅支持来自yahoo@hrbrmstr的调用选项数据最近的函数是通过Quantmod,但当前仅支持来自yahoo@hrbrmstr的调用选项数据
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_1.0.0 plyr_1.8.3 doMC_1.3.3 doParallel_1.0.8 iterators_1.0.7 foreach_1.4.2 pbapply_1.1-1 jsonlite_0.9.17
[9] RCurl_1.95-4.7 bitops_1.0-6
loaded via a namespace (and not attached):
[1] Rcpp_0.12.0 lattice_0.20-33 codetools_0.2-14 zoo_1.7-12
[5] grid_3.2.2 magrittr_1.5 PerformanceAnalytics_1.4.3541 stringi_0.5-5
[9] PortfolioAnalytics_1.0.3636 xts_0.9-7 blotter_0.8.19 tools_3.2.2
[13] compiler_3.2.2 quantstrat_0.8.2