[R] quantmod getOptionChain Not Work

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 10 02:40:38 CEST 2012


On a Unix-alike system, if you have an svn client installed, it
suffices to type

svn checkout svn://svn.r-forge.r-project.org/svnroot/quantmod/

at a command prompt -- then use R CMD install on the pkg/ directory to
install the new version. No idea how to do it on Windows....

Michael

On Sun, Apr 8, 2012 at 7:11 PM, Sparks, John James <jspark4 at uic.edu> wrote:
> Michael,
>
> I have not had time to look at this for a while but still wanted to say
> thanks for looking into it and sending this solution.
>
> By the way, Jeff mentioned that the version of quantmod on the SVN
> (0.3.18) works for this.  I tried to figure out how to download that
> version, but found the documentation on SVN's quite confusing.  Is there
> anyway that you could make that version available?
>
> Much appreciated.
> --John Sparks
>
>
>
> On Fri, March 23, 2012 5:55 pm, R. Michael Weylandt wrote:
>> Sorry about that: two small mistakes and I imagine there are a few
>> more I've missed.  This should actually work:
>>
>> ###########################################
>>
>>
>> library(XML)
>>
>> readYahooOptions <- function(Symbols, Exp, ...){
>>   parse.expiry <- function(x) {
>>     if(is.null(x))
>>       return(NULL)
>>
>>     if(inherits(x, "Date") || inherits(x, "POSIXt"))
>>       return(format(x, "%Y-%m"))
>>
>>     if (nchar(x) == 5L) {
>>       x <- sprintf(substring(x, 4, 5), match(substring(x,
>>                                                        1, 3),
>> month.abb), fmt = "20%s-%02i")
>>     }
>>     else if (nchar(x) == 6L) {
>>       x <- paste(substring(x, 1, 4), substring(x, 5, 6),
>>                  sep = "-")
>>     }
>>     return(x)
>>   }
>>
>>   clean.opt.table <- function(tableIn){
>>     tableOut <- sapply(tableIn[,-2], function(x)
>> as.numeric(gsub(",","",x)))
>>     rownames(tableOut) <- tableIn[,2]
>>     tableOut
>>   }
>>
>>   if(missing(Exp))
>>     optURL <-
>> paste(paste("http://finance.yahoo.com/q/op?s",Symbols,sep="="),"Options",sep="+")
>>   else
>>     optURL <-
>> paste(paste("http://finance.yahoo.com/q/op?s=",Symbols,"&m=",parse.expiry(Exp),sep=""),"Options",sep="+")
>>
>>   if(!missing(Exp) && is.null(Exp)) {
>>     optPage <- readLines(optURL)
>>     optPage <- optPage[grep("View By Expiration", optPage)]
>>     allExp <- gregexpr("m=", optPage)[[1]][-1] + 2
>>     allExp <- substring(optPage, allExp, allExp + 6)
>>     allExp <- allExp[seq_len(length(allExp)-1)] # Last one seems useless ?
>>     return(structure(lapply(allExp, readYahooOptions,
>> Symbols=Symbols), .Names=format(as.yearmon(allExp))))
>>   }
>>
>>   stopifnot(require("XML"))
>>
>>   optURL <- readHTMLTable(optURL)
>>
>>   # Not smart to hard code these but it's a 'good-enough' hack for now
>>   # Also, what is table 9 on this page?
>>
>>   list(calls = clean.opt.table(optURL[[10]]),
>>        puts = clean.opt.table(optURL[[14]]),
>>        symbol = Symbols)
>> }
>>
>>
>>
>> On Fri, Mar 23, 2012 at 6:44 PM, R. Michael Weylandt
>> <michael.weylandt at gmail.com> wrote:
>>> I just got around to taking a look at this, but below is a fix. It
>>> seems like yahoo finance redesigned the page and rather than reparsing
>>> all their HTML, I'll use Duncan TL's XML package to make life happier.
>>> (I loathe HTML parsing....)
>>>
>>> This isn't thoroughly tested and it'll break if yahoo redesigns things
>>> again (I hardcode the table numbers for now) but it seems to work well
>>> enough. Let me know if you have any errors with it. If Jeff likes it,
>>> it should be a drop-in replacement for the getOptionChain.yahoo for
>>> quantmod with a name change.
>>>
>>> Feedback welcome,
>>>
>>> Michael
>>>
>>> #############################################
>>>
>>> library(XML)
>>>
>>> readYahooOptions <- function(Symbols, Exp, ...){
>>> áparse.expiry <- function(x) {
>>> á áif(is.null(x))
>>> á á áreturn(NULL)
>>>
>>> á áif(inherits(x, "Date") || inherits(x, "POSIXt"))
>>> á á áreturn(format(x, "%Y-%m"))
>>>
>>> á áif (nchar(x) == 5L) {
>>> á á áx <- sprintf(substring(x, 4, 5), match(substring(x,
>>> á á á á á á á á á á á á á á á á á á á á á á á á á á á 1, 3),
>>> month.abb), fmt = "20%s-%02i")
>>> á á}
>>> á áelse if (nchar(x) == 6L) {
>>> á á áx <- paste(substring(x, 1, 4), substring(x, 5, 6),
>>> á á á á á á á á sep = "-")
>>> á á}
>>> á áreturn(x)
>>> á}
>>>
>>> áclean.opt.table <- function(tableIn){
>>> á átableOut <- lapply(tableIn[,-2], function(x)
>>> as.numeric(gsub(",","",x)))
>>> á árownames(tableOut) <- tableIn[,2]
>>> á}
>>>
>>> áif(missing(Exp))
>>> á áoptURL <-
>>> paste(paste("http://finance.yahoo.com/q/op?s",Symbols,sep="="),"Options",sep="+")
>>> áelse
>>> á áoptURL <-
>>> paste(paste("http://finance.yahoo.com/q/op?s=",Symbols,"&m=",parse.expiry(Exp),sep=""),"Options",sep="+")
>>>
>>> áif(!missing(Exp) && is.null(Exp)) {
>>> á áoptPage <- readLines(optURL)
>>> á áoptPage <- optPage[grep("View By Expiration", optPage)]
>>> á áallExp <- gregexpr("m=", optPage)[[1]][-1] + 2
>>> á áallExp <- substring(optPage, allExp, allExp + 6)
>>> á áallExp <- allExp[seq_len(length(allExp)-1)] # Last one seems
>>> useless ? Always true?
>>> á áreturn(structure(lapply(allExp, readYahooOptions,
>>> Symbols=Symbols), .Names=format(as.yearmon(allExp))))
>>> á}
>>>
>>> ástopifnot(require("XML"))
>>>
>>> áoptURL <- readHTMLTable(optURL)
>>>
>>> á# Not smart to hard code these but it's a 'good-enough' hack for now
>>> á# Also, what is table 9 on this page?
>>> áCALLS <- optURL[[10]]
>>> áPUTS <- optURL[[14]]
>>>
>>> álist(calls = CALLS, puts = PUTS, symbol = Symbols)
>>> }
>>>
>>>
>>> ###########################################################
>>>
>>> On Sun, Mar 4, 2012 at 2:18 PM, Sparks, John James <jspark4 at uic.edu>
>>> wrote:
>>>> Dear R Helpers,
>>>>
>>>> I am still having trouble with the getOptionChain command in quantmod.
>>>> áI
>>>> have the latest version of quantmod, etc. so I was under the impression
>>>> that the problem was solved with updates to the package.
>>>>
>>>> If someone could let me know what I need to install in order to make
>>>> this
>>>> work, I would really appreciate it.
>>>>
>>>> My error message as session info are shown below. áThanks a bunch.
>>>> --John Sparks
>>>>
>>>> R version 2.14.2 (2012-02-29)
>>>> Platform: i386-pc-mingw32/i386 (32-bit)
>>>>
>>>> locale:
>>>> [1] LC_COLLATE=English_United States.1252 áLC_CTYPE=English_United
>>>> States.1252 á áLC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>>> [5] LC_TIME=English_United States.1252
>>>>
>>>> attached base packages:
>>>> [1] stats á á graphics ágrDevices utils á á datasets ámethods á base
>>>>
>>>> other attached packages:
>>>> [1] pomp_0.40-2 á á ádeSolve_1.10-3 á subplex_1.1-3 á ámvtnorm_0.9-9992
>>>> quantmod_0.3-17 áTTR_0.21-0 á á á xts_0.8-2 á á á ázoo_1.7-7
>>>> Defaults_1.1-1
>>>>
>>>> loaded via a namespace (and not attached):
>>>> [1] grid_2.14.2 á álattice_0.20-0 tools_2.14.2
>>>>> AAPL.OPT<-getOptionChain("AAPL")
>>>> Error in puts[, 2] : incorrect number of dimensions
>>>>> AAPL.OPT<-getOptionChain("AAPL",NULL)
>>>> Error in puts[, 2] : incorrect number of dimensions
>>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>



More information about the R-help mailing list