Skip to content

Commit

Permalink
Add 'type' argument to .yahooURL()
Browse files Browse the repository at this point in the history
This allows the .yahooURL() function to be used in getDividends and
getSplits(), which reduces duplicated code. Also use the .dateToUNIX()
function to calculate the from and to POSIX dates in both functions.

See #157.
  • Loading branch information
joshuaulrich committed May 26, 2017
1 parent cefea29 commit 5cb5b8f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
16 changes: 5 additions & 11 deletions R/getDividends.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
deparse(substitute(Symbol)),
as.character(Symbol))

yahoo.URL <- "https://query1.finance.yahoo.com/v7/finance/download/"

from.posix <- as.integer(as.POSIXct(as.Date(from, origin = "1970-01-01")))
to.posix <- as.integer(as.POSIXct(as.Date(to, origin = "1970-01-01")))
from.posix <- .dateToUNIX(from)
to.posix <- .dateToUNIX(to)

tmp <- tempfile()
on.exit(unlink(tmp))

handle <- .getHandle()
curl::curl_download(paste0(yahoo.URL, Symbol.name,
"?period1=", from.posix,
"&period2=", to.posix,
"&interval=1d",
"&events=div",
"&crumb=", handle$cb),
destfile=tmp, quiet=!verbose, handle=handle$ch)
yahoo.URL <- .yahooURL(Symbol.name, from.posix, to.posix,
"1d", "div", handle)
curl::curl_download(yahoo.URL, destfile=tmp, quiet=!verbose, handle=handle$ch)

fr <- read.csv(tmp)
fr <- xts(fr[,2],as.Date(fr[,1]))
Expand Down
16 changes: 5 additions & 11 deletions R/getSplits.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
deparse(substitute(Symbol)),
as.character(Symbol))

yahoo.URL <- "https://query1.finance.yahoo.com/v7/finance/download/"

from.posix <- as.integer(as.POSIXct(as.Date(from, origin = "1970-01-01")))
to.posix <- as.integer(as.POSIXct(as.Date(to, origin = "1970-01-01")))
from.posix <- .dateToUNIX(from)
to.posix <- .dateToUNIX(to)

tmp <- tempfile()
on.exit(unlink(tmp))

handle <- .getHandle()
curl::curl_download(paste0(yahoo.URL, Symbol.name,
"?period1=", from.posix,
"&period2=", to.posix,
"&interval=1d",
"&events=split",
"&crumb=", handle$cb),
destfile=tmp, quiet=!verbose, handle=handle$ch)
yahoo.URL <- .yahooURL(Symbol.name, from.posix, to.posix,
"1d", "split", handle)
curl::curl_download(yahoo.URL, destfile=tmp, quiet=!verbose, handle=handle$ch)

fr <- read.csv(tmp, as.is=TRUE)

Expand Down
11 changes: 7 additions & 4 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,14 @@ formals(loadSymbols) <- loadSymbols.formals
}

.yahooURL <-
function(symbol, from, to, period, handle)
function(symbol, from, to, period, type, handle)
{
p <- match.arg(period, c("1d", "1wk", "1mo"))
e <- match.arg(type, c("history", "div", "split"))
n <- if (unclass(Sys.time()) %% 1L >= 0.5) 1L else 2L
u <- paste0("https://query", n, ".finance.yahoo.com/v7/finance/download/",
symbol, "?period1=", from, "&period2=", to, "&interval=", p,
"&events=history&crumb=", handle$cb)
"&events=", e, "&crumb=", handle$cb)
return(u)
}

Expand Down Expand Up @@ -312,7 +313,8 @@ function(Symbols,env,return.class='xts',index.class="Date",
Symbols.name <- ifelse(is.null(Symbols.name),Symbols[[i]],Symbols.name)
if(verbose) cat("downloading ",Symbols.name,".....\n\n")

yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix, "1d", handle)
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
"1d", "history", handle)
dl <- try(curl::curl_download(yahoo.URL, destfile = tmp,
quiet = !verbose, handle = handle$ch),
silent = TRUE)
Expand All @@ -324,7 +326,8 @@ function(Symbols,env,return.class='xts',index.class="Date",
# re-create handle
handle <- .getHandle(force.new = TRUE)
# try again
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix, "1d", handle)
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
"1d", "history", handle)
dl <- try(curl::curl_download(yahoo.URL, destfile = tmp,
quiet = !verbose, handle = handle$ch),
silent = TRUE)
Expand Down

0 comments on commit 5cb5b8f

Please sign in to comment.