Skip to content

Commit

Permalink
Remove Yahoo Finance v7 endpoint
Browse files Browse the repository at this point in the history
The v7 endpoint seems to be rate-limited, and the v8 endpoint includes
intra-day data.

See #360. See #362.
  • Loading branch information
joshuaulrich committed May 28, 2022
1 parent db68b21 commit e5fdd6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 87 deletions.
67 changes: 15 additions & 52 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,6 @@ formals(loadSymbols) <- loadSymbols.formals
return(h)
}

.yahooURL <-
function(symbol, from, to, period, type)
{
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, sprintf("?period1=%.0f&period2=%.0f", from, to),
"&interval=", p, "&events=", e)
return(u)
}

.yahooJsonURL <-
function(symbol, from, to, period, type)
{
Expand All @@ -250,8 +238,7 @@ function(Symbols,env,return.class='xts',index.class="Date",
to=Sys.Date(),
...,
periodicity="daily",
curl.options=list(),
use.json.api=FALSE)
curl.options=list())
{
importDefaults("getSymbols.yahoo")
this.env <- environment()
Expand Down Expand Up @@ -302,41 +289,21 @@ 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")

if(use.json.api) {
yahoo.URL <- .yahooJsonURL(Symbols.name, from.posix, to.posix, interval)
conn <- curl::curl(yahoo.URL, handle = handle)
y <- try(jsonlite::fromJSON(conn)$chart$result, silent = TRUE)

ohlcv <- unlist(y$indicators$quote[[1]], recursive = FALSE)
idx <- as.Date(.POSIXct(y$timestamp[[1]]))
x <- xts(do.call(cbind, ohlcv), idx,
src='yahoo', updated=Sys.time())

fr <- merge(OHLCV(x), adjusted = unlist(y$indicators$adjclose))

# convert column names to Initial Capitalization
cn <- colnames(fr)
substring(cn, 1, 1) <- toupper(substring(cn, 1, 1))
colnames(fr) <- cn

} else {
yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix,
interval, "history")
conn <- curl::curl(yahoo.URL, handle = handle)
fr <- try(read.csv(conn, na.strings="null"), silent = TRUE)

if (inherits(fr, "try-error")) {
fr <- retry.yahoo(Symbols.name, from.posix, to.posix, interval,
"history", conn, curl.options = curl.options,
na.strings = NULL)
}
yahoo.URL <- .yahooJsonURL(Symbols.name, from.posix, to.posix, interval)
conn <- curl::curl(yahoo.URL, handle = handle)
y <- try(jsonlite::fromJSON(conn)$chart$result, silent = TRUE)

if(verbose) cat("done.\n")
fr <- xts(as.matrix(fr[,-1]),
as.Date(fr[,1]),
#as.POSIXct(fr[,1], tz=Sys.getenv("TZ")),
src='yahoo',updated=Sys.time())
}
ohlcv <- unlist(y$indicators$quote[[1]], recursive = FALSE)
idx <- as.Date(.POSIXct(y$timestamp[[1]]))
x <- xts(do.call(cbind, ohlcv), idx,
src='yahoo', updated=Sys.time())

fr <- merge(OHLCV(x), adjusted = unlist(y$indicators$adjclose))

# convert column names to Initial Capitalization
cn <- colnames(fr)
substring(cn, 1, 1) <- toupper(substring(cn, 1, 1))
colnames(fr) <- cn

# warn about missing values
if (any(is.na(fr))) {
Expand Down Expand Up @@ -364,10 +331,6 @@ function(Symbols,env,return.class='xts',index.class="Date",
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
if(!use.json.api && i >= 5 && length(Symbols) > 5) {
message("pausing 1 second between requests for more than 5 symbols")
Sys.sleep(1)
}
}, silent = TRUE)
if (inherits(test, "try-error")) {
msg <- paste0("Unable to import ", dQuote(returnSym[[i]]),
Expand Down
35 changes: 0 additions & 35 deletions R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,3 @@ function(url, destfile, method, quiet = FALSE, mode = "w", cacheOK = TRUE,
}
}
}

retry.yahoo <-
function(symbol,
from,
to,
interval,
type,
conn,
...,
curl.options = list())
{
warning(symbol, " download failed; trying again.",
call. = FALSE, immediate. = TRUE)

# re-create handle
handle <- .getHandle(curl.options, force.new = TRUE)

# try again. must rebuild url with crumbs
yahoo.URL <- .yahooURL(symbol, from, to, interval, type)

close(conn)
conn <- curl::curl(yahoo.URL, handle = handle)

fr <- try(read.csv(conn, ..., as.is = TRUE), silent = TRUE)

# error if second attempt also failed
if (inherits(fr, "try-error")) {
close(conn)
stop(symbol, " download failed after two attempts. Error",
" message:\n", attr(fr, "condition")$message, call. = FALSE)
}

# return data
return(fr)
}

0 comments on commit e5fdd6c

Please sign in to comment.