3

I use R to download option chains, via the quantmod package. My goal is to download and export option chains in order to be used on other software.

If I download only the front month expiry, I am able to correctly export to a .txt file, using these lines:

library(quantmod)
aapl_front <- getOptionChain ('AAPL')
front <- do.call('rbind', aapl_front)
write.table (front, 'data_front.txt')

Problems appear when I download all the expiries. Here the rbind function fails to work as I think it should, and I export a table that is useless; these are the lines:

aapl_total <- getOptionChain('AAPL', NULL)
total <- do.call('rbind', aapl_total)
write.table(total, 'data_total.txt')

I guess that in the second case aapl_total is a list of lists, that contains all the expiries, and I'm not able to correctly split them.

Any suggestions?

4

3 回答 3

4

您可以循环遍历每个到期以及rbind调用和放置。

lapply(aapl_total, function(x) do.call(rbind, x))

然后,您将有一个可以使用的列表do.call(rbind())

一步:

do.call(rbind, lapply(aapl_total, function(x) do.call(rbind, x)))
于 2013-07-21T17:55:17.477 回答
1

这些不再适用于期权数据,因为雅虎改变了它的网站,请使用谷歌金融作为期权数据源,这里是一个示例

示例:getOptionChain('GOOG',3) 表示谷歌期权链的下一个 3 到期日期

library(rjson)
library(plyr)
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)
}
于 2015-02-11T00:48:55.410 回答
0

这里有另一种方法。我恢复列表结构:

ni <- seq_along(aapl_total)
nj <- seq_along(aapl_total[[1]])
nij <- as.matrix(expand.grid( ni=ni,  nj=nj))
data <- apply(nij, 1, function(ij) aapl_total[[ij]])

然后我把它放在一个单独的data.frame中:

res <- do.call(rbind,data)

编辑添加列类型,因为行名不重要。

labels <- as.vector(outer(c('call','put'),
                         names(aapl_total),paste0))
data <- lapply(seq(nrow(nij)), function(i) {
  data.frame(aapl_total[[nij[i,]]],type=labels[i])
})
res <- do.call(rbind,data)
于 2013-07-21T18:16:29.357 回答