Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alphavantage getQuote batch discontinued #296

Closed
helgasoft opened this issue Feb 7, 2020 · 0 comments
Closed

Alphavantage getQuote batch discontinued #296

helgasoft opened this issue Feb 7, 2020 · 0 comments
Assignees
Labels

Comments

@helgasoft
Copy link

Description

It seems Alphavantage's API has changed and they do not have batch quotes anymore. Only single quote, like this.     So current getQuote.av does not work anymore. Related to #213

Expected behavior

Being able to get quotes from Alphavantage anytime with personal API key.

Minimal, reproducible example

getQuote(c('IBM','F'), src='av', api.key='yourOwn')   # No data for symbols: IBM, F

Proposed patch

getQuote.av <- function (Symbols='A;B;F', api.key, verbose=FALSE) {
  # get latest daily quotes from AV, they dont have batch quotes anymore as of Feb 2020
  importDefaults("getQuote.av")
  if (!hasArg("api.key")) {
    stop("getQuote.av: An API key is required (api.key). Free registration,", 
         " at https://www.alphavantage.co/.", call. = FALSE)
  }
  if (length(Symbols) > 1 && is.character(Symbols))  # accept vector too
    Symbols <- paste(Symbols, collapse = ";")
  Symbols <- unique(unlist(strsplit(Symbols, ";")))
  length.of.symbols <- length(unlist(strsplit(Symbols, ";")))
  if (length.of.symbols==0) 
    stop('no symbols provided', call. = FALSE)
  if (verbose) cat('\n getting quotes: ')
  qdf <- data.frame()
  for (i in 1:length.of.symbols) {
    Sys.sleep(0.5)
    if (verbose) cat(Symbols[i], " ")
    url <- paste0('https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=',Symbols[i],'&apikey=',api.key)
    dd <- jsonlite::fromJSON(url)
    if (names(dd[1])=="Error Message") next   # skip invalid symbols
    dd <- dd[[1]]  # dd$`Global Quote`
    dd[2:6] <- sapply(dd[2:6], as.numeric)
    tmpdf <- data.frame(symbol = toupper(Symbols[i]), 
                        date = as.Date(dd$`07. latest trading day`),
                        open = if (is.null(dd$`02. open`)) 0 else dd$`02. open`, 
                        high = if (is.null(dd$`03. high`)) 0 else dd$`03. high`, 
                        low =  if (is.null(dd$`04. low`))  0 else dd$`04. low`, 
                        close = dd$`05. price`,
                        volume=if (is.null(dd$`06. volume`)) 0 else dd$`06. volume` )
    qdf <- rbind(qdf, tmpdf)
  }
  if (verbose) cat('done.\n')
  rownames(qdf) <- qdf$symbol
  qdf$symbol <- NULL
  qdf
}
@joshuaulrich joshuaulrich self-assigned this Feb 19, 2020
@joshuaulrich joshuaulrich added this to the Release 0.4-16 milestone Feb 23, 2020
joshuaulrich added a commit that referenced this issue Mar 8, 2020
This dependency was added in f2abeb8.

See #296.
joshuaulrich added a commit that referenced this issue Dec 8, 2020
Alphavantage returns NULL for all the columns of data for requests on
invalid symbols. We need to set the Symbol column to the value of the
symbol input in order to return a row of NA for the symbol in the
results, as we do for other getQuote() methods.

See #296.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants