A lightweight R interface to the Alpha Vantage API
Alpha Vantage is a free service that enables users to get real-time and historical financial data. New users will need to visit Alpha Vantage and obtain an API key.
The alphavantager
package provides a convenient and
lightweight interface to the Alpha Vantage API.
To get started, install the package from CRAN or from GitHub:
install.packages("alphavantager")
# Or
::install_github("business-science/alphavantager") devtools
Load the package.
library(alphavantager)
Set your API key (get one from Alpha Vantage if you don’t already have one… it’s free).
av_api_key("YOUR_API_KEY")
print(av_api_key())
#> [1] "YOUR_API_KEY"
Now, you’re ready to get financial data via av_get()
,
which accepts the same1 arguments as the API Documentation
parameters. The function is setup with two primary arguments,
symbol
and av_fun
, which accepts an equity and
one of the API “function” parameters. You can pass additional API
parameters via the ...
.
# Function is streamlined and user adds additional parameters via ...
args(av_get)
#> function (symbol, av_fun, ...)
#> NULL
Here are a few examples of retrieving real-time and historical financial data!
av_get(symbol = "MSFT",
av_fun = "TIME_SERIES_INTRADAY",
interval = "15min",
outputsize = "full")
# REAL-TIME QUOTE
av_get("EUR/USD", av_fun = "CURRENCY_EXCHANGE_RATE")
# TIME SERIES
av_get("EUR/USD", av_fun = "FX_DAILY", outputsize = "full")
av_get(symbol = "MSFT",
av_fun = "AROON",
interval = "monthly",
time_period = 60,
outputsize = "full")
av_get(av_fun = "SECTOR")
av_get(av_fun = "OVERVIEW")
The av_fun
argument replaces the API parameter
“function” because function is a reserved name in R. All other arguments
match the Alpha Vantage API parameters.
There is no need to specify the apikey
parameter as
an argument to av_get()
. The required method is to set the
API key using av_api_key("YOUR_API_KEY")
.
There is no need to specify the datatype
parameter
as an argument to av_get()
. The function will return a
tibble data frame.
Some data sets only return 100 rows by default. Change the
parameter outputsize = "full"
to get the full
dataset.