[R-SIG-Finance] Interaction with Alpha Vantage?
Dirk Eddelbuettel
edd at debian.org
Mon Nov 6 16:41:52 CET 2017
Credit where credit is due---the 'tidyquant' folks first mentioned it, but it
in the fullest and most glorious tradition of the tibbliesverse require half
a dozen or more other packages for not apparent reason. So I followed up
with a quick tweet on Sep 5 about a one-liner not needing anything else
besides data.table:
https://twitter.com/eddelbuettel/status/905066349294219264
and cooked up a helper function in a so-far-unreleased package of personal
functions (this one is below) which I shared with at least Josh. The larger
function added to quantmod is AFAIK contributed by Paul.
Now, as for interchaning with them: Nope. I too need ETFs, Canadian stocks
and whatnot for the little personal finance app I have had as a daily cronjob
since the 1990s (and been meaning to rewrite in R since then too as it is,
gasp, Perl -- see eg https://github.com/eddelbuettel/beancounter and other
online resources). It may now be time to rewrite this as the underlying
(Perl) data grabber Finance::YahooQuote is now dead due to Yahoo! walking
away from that API. I have an unpublished R-based drop-in replacement for
just the data gathering ...
Anyway, alphavantage looks good. We should test it some more.
Dirk
##' Fetch a real-time market data series from AlphaVantage
##'
##' Several optional parameters could be set, but are not currently.
##' @title Retrieve real-time data from AlphaVantage
##' @param sym Character string value for the ticker
##' @param datatype Character string value for the supported type of data, currently one of
##' \dQuote{intraday}, \dQuote{daily}, \dQuote{adjdaily}, \dQuote{weekly}, \dQuote{monthly}.
##' @param outputsize Character string value, one of \dQuote{compact} or \dQuote{full}. Applies
##' only daily or intraday data.
##' @return A data.table object
##' @author Dirk Eddelbuettel
alphavantage <- function(sym,
datatype=c("intraday", "daily", "adjdaily", "weekly", "monthly"),
outputsize=c("compact", "full")) {
datatype <- match.arg(datatype)
outputsize <- match.arg(outputsize)
datatypeArg <- switch(datatype,
intraday = "TIME_SERIES_INTRADAY",
daily = "TIME_SERIES_DAILY",
adjdaily = "TIME_SERIES_DAILY_ADJUSTED",
weekly = "TIME_SERIES_WEEKLY",
monthly = "TIME_SERIES_MONTHLY")
cmd <- paste0("https://www.alphavantage.co/query?",
"function=", datatypeArg, "&",
"symbol=", sym, "&",
"interval=1min&",
"apikey=", getOption("alphavantageKey", "demo"), "&",
"datatype=csv&")
if (datatype %in% c("intraday", "daily", "adjdaily")) {
cmd <- paste0(cmd, "outputsize=", outputsize)
}
#print(cmd)
data <- data.table::fread(cmd, showProgress=FALSE)
}
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the R-SIG-Finance
mailing list