Skip to content

Commit

Permalink
Allow user to pass options to curl handle
Browse files Browse the repository at this point in the history
User's network settings may require specific curl settings to be able
to connect to the internet. Add curl.options argument to
getSymbols.yahoo(), with a default value the same as the default .list
argument value for the curl functions that accept curl options.

Fixes #177.
  • Loading branch information
joshuaulrich committed Oct 1, 2017
1 parent 4b3d495 commit f8f0b1a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ formals(loadSymbols) <- loadSymbols.formals
#"getSymbols.Bloomberg" <- getSymbols.Bloomberg
# }}}

.getHandle <- function(force.new = FALSE)
.getHandle <- function(curl.options = list(), force.new = FALSE)
{
h <- get0("_handle_", .quantmodEnv)

Expand All @@ -227,6 +227,8 @@ formals(loadSymbols) <- loadSymbols.formals

for (i in 1:5) {
h <- curl::new_handle()
curl::handle_setopt(h, .list = curl.options)

# random query to avoid cache
ru <- paste(sample(c(letters, 0:9), 4), collapse = "")
cu <- paste0("https://finance.yahoo.com?", ru)
Expand Down Expand Up @@ -278,7 +280,8 @@ function(Symbols,env,return.class='xts',index.class="Date",
from='2007-01-01',
to=Sys.Date(),
...,
periodicity="daily")
periodicity="daily",
curl.options=list())
{
if(getOption("getSymbols.yahoo.warning",TRUE)) {
# Warn about Yahoo Finance quality and stability
Expand Down Expand Up @@ -308,7 +311,7 @@ function(Symbols,env,return.class='xts',index.class="Date",
if(!hasArg("verbose")) verbose <- FALSE
if(!hasArg("auto.assign")) auto.assign <- TRUE

handle <- .getHandle()
handle <- .getHandle(curl.options)

tmp <- tempfile()
on.exit(unlink(tmp))
Expand Down Expand Up @@ -349,7 +352,7 @@ function(Symbols,env,return.class='xts',index.class="Date",
warning(Symbols.name, " download failed; trying again.",
call. = FALSE, immediate. = TRUE)
# re-create handle
handle <- .getHandle(force.new = TRUE)
handle <- .getHandle(curl.options, force.new = TRUE)
# try again
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
interval, "history", handle)
Expand Down
4 changes: 3 additions & 1 deletion man/getSymbols.yahoo.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ getSymbols.yahoo(Symbols,
from = "2007-01-01",
to = Sys.Date(),
...,
periodicity = "daily")
periodicity = "daily",
curl.options = list())
}
%- maybe also 'usage' for other objects documented here.
\arguments{
Expand All @@ -35,6 +36,7 @@ getSymbols.yahoo(Symbols,
\item{\dots}{ additional parameters }
\item{periodicity}{ periodicity of data to query and return. Must be
one of "daily", "weekly", "monthly". ("daily") }
\item{curl.options}{ options passed to \code{curl::handle_setopt} }
}
\details{
Meant to be called internally by \code{getSymbols} (see also).
Expand Down

0 comments on commit f8f0b1a

Please sign in to comment.