我想从谷歌财经下载选项链数据,在这里我找到一个R脚本下载,它很有用,我已经改变它能够下载多个到期日期选项价格,但原点json链接似乎只有调用选项数据,如何获取选项数据。
library(rjson)
getOptionChain <- function (symbol,exp) {
# symbol = "WMT"
url <- "https://www.google.com/finance/option_chain?q="
# url <- paste(url, symbol, "&expd=15&expm=01&expy=2016&output=json", sep="")
url <- paste(url, symbol, "&output=json", sep="")
google.options.json <- readLines(url, warn = FALSE)
options.json <- google.options.json
options.json <- gsub("[{]", "{\"", options.json)
options.json <- gsub("[:]", "\":", options.json)
options.json <- gsub("[,] ", "$$$", options.json)
options.json <- gsub("[,]", ",\"", options.json)
options.json <- gsub("[,]\"[{]", ",{", options.json)
options.json <- gsub("[$][$][$]", ", ", options.json)
options.list <- fromJSON(options.json)
#get the options chain without an expiry date and then determine longest option
last.expiration <- length(options.list[["expirations"]])
if ( exp>0 && exp< last.expiration) {
last.expiration <-exp
}
month <- sprintf("%02d", options.list[["expirations"]][[last.expiration]]$m)
day <- sprintf("%02d", options.list[["expirations"]][[last.expiration]]$d )
year <- options.list[["expirations"]][[last.expiration]]$y
#now request option chain for the longest expiry
url <- "https://www.google.com/finance/option_chain?q="
url <- paste(url, symbol, "&expd=", day, "&expm=", month, "&expy=", year, "&output=json", sep="")
google.options.json <- readLines(url, warn = FALSE)
options.json <- google.options.json
options.json <- gsub("[{]", "{\"", options.json)
options.json <- gsub("[:]", "\":", options.json)
options.json <- gsub("[,] ", "$$$", options.json)
options.json <- gsub("[,]", ",\"", options.json)
options.json <- gsub("[,]\"[{]", ",{", options.json)
options.json <- gsub("[$][$][$]", ", ", options.json)
options.list <- fromJSON(options.json)
options <- ldply (options.list[["calls"]], data.frame)
options <- rename(options, c("s" = "contract.name",
"p" = "price",
"b" = "bid",
"a" = "ask",
"c" = "change",
"cp" = "change.percentage",
"oi" = "open.interest",
"vol" = "volume"))
options <- options[c( "contract.name",
"strike",
"price",
"change",
"change.percentage",
"bid",
"ask",
"volume",
"open.interest")]
options$expiry <- paste(options.list[["expiry"]]$m, options.list[["expiry"]]$d, options.list[["expiry"]]$y, sep = "/")
last.expiration <- length(options.list[["expirations"]])
options$longest.available.expiry <- paste(options.list[["expirations"]][[last.expiration]]$m,
options.list[["expirations"]][[last.expiration]]$d,
options.list[["expirations"]][[last.expiration]]$y, sep = "/")
options$underlying.price <- options.list[["underlying_price"]]
return(options)
}
答案 0 :(得分:1)
如果您想要一个快速而肮脏的解决方案,请替换此行
options <- ldply (options.list[["calls"]], data.frame)
带
options <- ldply (options.list[["puts"]], data.frame)
在这两种情况下,该函数都将返回一个包含48行的数据框。 但是你会注意到返回的数据是不同的。 电话有'GOOG170120C00250000'这样的赌注,里面有一个“P”:'GOOG170120P00250000'
出于个人兴趣,我稍微改写了你的例子,这对我有用。这是代码:
library(rjson)
library(plyr)
getOptionChain <- function (symbol,exp = 14, type = "calls") {
# symbol = "WMT"
if( ! any(grepl(type, c("puts", "calls") ))){
stop("ERROR: Third argument must be either 'calls' or 'puts'. Defaults to 'calls'.")
}
url <- "https://www.google.com/finance/option_chain?q="
# url <- paste(url, symbol, "&expd=15&expm=01&expy=2016&output=json", sep="")
url <- paste(url, symbol, "&output=json", sep="")
#google.options.json <- readLines(url, warn = FALSE, )
outfile = paste0(symbol, ".json")
rv <- download.file(url, destfile = outfile, method="curl")
warning(paste0("fetching url 1: ", url))
google.options.json <- readLines(outfile, warn = FALSE)
options.json <- google.options.json
options.json <- gsub("[{]", "{\"", options.json)
options.json <- gsub("[:]", "\":", options.json)
options.json <- gsub("[,] ", "$$$", options.json)
options.json <- gsub("[,]", ",\"", options.json)
options.json <- gsub("[,]\"[{]", ",{", options.json)
options.json <- gsub("[$][$][$]", ", ", options.json)
options.list <- fromJSON(options.json)
#get the options chain without an expiry date and then determine longest option
last.expiration <- length(options.list[["expirations"]])
if ( exp>0 && exp< last.expiration) {
last.expiration <-exp
}
month <- sprintf("%02d", options.list[["expirations"]][[last.expiration]]$m)
day <- sprintf("%02d", options.list[["expirations"]][[last.expiration]]$d )
year <- options.list[["expirations"]][[last.expiration]]$y
#now request option chain for the longest expiry
url <- "https://www.google.com/finance/option_chain?q="
url <- paste(url, symbol, "&expd=", day, "&expm=", month, "&expy=", year, "&output=json", sep="")
warning(paste0("fetching url 2: ", url))
outfile2 = paste0(symbol, ".longest-expiry.json")
rv <- download.file(url, destfile = outfile2, method="curl")
google.options.json <- readLines(outfile2, warn = FALSE)
options.json <- google.options.json
options.json <- gsub("[{]", "{\"", options.json)
options.json <- gsub("[:]", "\":", options.json)
options.json <- gsub("[,] ", "$$$", options.json)
options.json <- gsub("[,]", ",\"", options.json)
options.json <- gsub("[,]\"[{]", ",{", options.json)
options.json <- gsub("[$][$][$]", ", ", options.json)
options.list <- fromJSON(options.json)
options <- ldply (options.list[[type]], data.frame)
options <- rename(options, c("s" = "contract.name",
"p" = "price",
"b" = "bid",
"a" = "ask",
"c" = "change",
"cp" = "change.percentage",
"oi" = "open.interest",
"vol" = "volume"))
options <- options[c( "contract.name",
"strike",
"price",
"change",
"change.percentage",
"bid",
"ask",
"volume",
"open.interest")]
options$expiry <- paste(options.list[["expiry"]]$m, options.list[["expiry"]]$d, options.list[["expiry"]]$y, sep = "/")
last.expiration <- length(options.list[["expirations"]])
options$longest.available.expiry <- paste(options.list[["expirations"]][[last.expiration]]$m,
options.list[["expirations"]][[last.expiration]]$d,
options.list[["expirations"]][[last.expiration]]$y, sep = "/")
options$underlying.price <- options.list[["underlying_price"]]
return(options)
}
示例函数调用:
calls <- getOptionChain("GOOG", 12, "calls")
puts <- getOptionChain("GOOG", 12, "puts")
“exp”参数设置为任意值“12”。我不明白它的用途,但没关系。