Skip to content

Commit

Permalink
Add periodicity argument to getSymbols.yahoo()
Browse files Browse the repository at this point in the history
Kurt Hornik expressed an interest in using getSymbols.yahoo() as a
drop-in replacement for tseries::get.hist.quote(), because that would
create a single point of failure for any future changes to Yahoo
Finance.

But tseries::get.hist.quote() has a has a 'compression' argument that
allows users to pull daily, weekly, or monthly data as provided by
Yahoo. Add the periodicity argument to getSymbols.yahoo(), so
get.hist.quote() can maintain functionality for its 'compression'
argument.

Fixes #162.
  • Loading branch information
joshuaulrich committed May 26, 2017
1 parent aa19272 commit a39d6b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 16 additions & 3 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ function(symbol, from, to, period, type, handle)
function(Symbols,env,return.class='xts',index.class="Date",
from='2007-01-01',
to=Sys.Date(),
...)
...,
periodicity="daily")
{
if(getOption("getSymbols.yahoo.warning",TRUE)) {
# Warn about Yahoo Finance quality and stability
Expand All @@ -296,6 +297,9 @@ function(Symbols,env,return.class='xts',index.class="Date",
default.from <- from
default.to <- to

intervals <- c(daily = "1d", weekly = "1wk", monthly = "1mo")
default.periodicity <- match.arg(periodicity, names(intervals))

if(!hasArg(verbose)) verbose <- FALSE
if(!hasArg(auto.assign)) auto.assign <- TRUE

Expand All @@ -308,6 +312,15 @@ function(Symbols,env,return.class='xts',index.class="Date",
return.class <- getSymbolLookup()[[Symbols[[i]]]]$return.class
return.class <- ifelse(is.null(return.class),default.return.class,
return.class)
periodicity <- getSymbolLookup()[[Symbols[[i]]]]$periodicity
periodicity <- if(is.null(periodicity)) default.periodicity else periodicity

# ensure valid periodicity
p <- pmatch(periodicity, names(intervals))
if(is.na(p))
stop("periodicity must be one of: ", paste(intervals, collapse=", "))
interval <- intervals[p]

from <- getSymbolLookup()[[Symbols[[i]]]]$from
from <- if(is.null(from)) default.from else from
to <- getSymbolLookup()[[Symbols[[i]]]]$to
Expand All @@ -321,7 +334,7 @@ function(Symbols,env,return.class='xts',index.class="Date",
if(verbose) cat("downloading ",Symbols.name,".....\n\n")

yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
"1d", "history", handle)
interval, "history", handle)
dl <- try(curl::curl_download(yahoo.URL, destfile = tmp,
quiet = !verbose, handle = handle$ch),
silent = TRUE)
Expand All @@ -334,7 +347,7 @@ function(Symbols,env,return.class='xts',index.class="Date",
handle <- .getHandle(force.new = TRUE)
# try again
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
"1d", "history", handle)
interval, "history", handle)
dl <- try(curl::curl_download(yahoo.URL, destfile = tmp,
quiet = !verbose, handle = handle$ch),
silent = TRUE)
Expand Down
5 changes: 4 additions & 1 deletion man/getSymbols.yahoo.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ getSymbols.yahoo(Symbols,
index.class = 'Date',
from = "2007-01-01",
to = Sys.Date(),
...)
...,
periodicity = "daily")
}
%- maybe also 'usage' for other objects documented here.
\arguments{
Expand All @@ -32,6 +33,8 @@ getSymbols.yahoo(Symbols,
(2007-01-01)}
\item{to}{ Retrieve data through this date (Sys.Date())}
\item{\dots}{ additional parameters }
\item{periodicity}{ periodicity of data to query and return. Must be
one of "daily", "weekly", "monthly". ("daily") }
}
\details{
Meant to be called internally by \code{getSymbols} (see also).
Expand Down

0 comments on commit a39d6b5

Please sign in to comment.