alphavantager: An R interface to the Free Alpha Vantage Financial Data API

[This article was first published on business-science.io - Articles, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We’re excited to announce the alphavantager package, a lightweight R interface to the Alpha Vantage API! Alpha Vantage is a FREE API for retreiving real-time and historical financial data. It’s very easy to use, and, with the recent glitch with the Yahoo Finance API, Alpha Vantage is a solid alternative for retrieving financial data for FREE! It’s definitely worth checking out if you are interested in financial analysis. We’ll go through the alphavantager R interface in this post to show you how easy it is to get real-time and historical financial data. In the near future, we have plans to incorporate the alphavantager into tidyquant to enable scaling from one equity to many.

If you like what you read, please follow us on social media to stay up on the latest Business Science news, events and information! As always, we are interested in both expanding our network of data scientists and seeking new clients interested in applying data science to business and finance. If interested, contact us.

Alpha Vantage

Alpha Vantage is a free service that enables users to get real-time and historical equity data. New users will need to visit Alpha Vantage and obtain an API key.

Alpha Vantage

R Interface: Getting Started

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
devtools::install_github("business-science/alphavantager")

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"

Getting Financial Data from Alpha Vantage

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 = NULL, av_fun, ...) 
## NULL

Let’s take av_get() for a test spin!

Time Series Data

We can get real-time intraday stock data by setting av_fun = "TIME_SERIES_INTRADAY" and the interval to one of the available resolutions (“1min”, “5min”, “15min”, “30min” or “60min”). We can also get daily, daily adjusted, weekly and monthly time series. Note that only 100 rows are returned by default, add the parameter outputsize = "full" to get the full number of rows.

av_get(symbol = "MSFT", av_fun = "TIME_SERIES_INTRADAY", interval = "15min", outputsize = "compact")
## # A tibble: 100 x 6
##              timestamp   open    high    low  close volume
##                 <dttm>  <dbl>   <dbl>  <dbl>  <dbl>  <int>
##  1 2017-08-29 11:30:00 72.800 72.8601 72.710 72.725 250459
##  2 2017-08-29 11:45:00 72.730 72.8600 72.680 72.850 354759
##  3 2017-08-29 12:00:00 72.850 72.9400 72.835 72.870 328545
##  4 2017-08-29 12:15:00 72.870 72.9300 72.870 72.890 157598
##  5 2017-08-29 12:30:00 72.900 72.9300 72.860 72.890 193864
##  6 2017-08-29 12:45:00 72.890 72.9300 72.860 72.920 140217
##  7 2017-08-29 13:00:00 72.923 73.0000 72.900 72.910 163803
##  8 2017-08-29 13:15:00 72.910 72.9600 72.880 72.890 132402
##  9 2017-08-29 13:30:00 72.890 73.0100 72.860 72.985 470568
## 10 2017-08-29 13:45:00 72.985 73.0199 72.920 72.980 292979
## # ... with 90 more rows

Technical Indicators

A full suite of real-time and historical technical indicators are available. The “SMA” (Simple Moving Average) is shown below.

av_get(symbol = "MSFT", av_fun = "SMA", interval = "monthly", time_period = 60, series_type = "close")
## # A tibble: 153 x 2
##              timestamp     sma
##                 <dttm>   <dbl>
##  1 2005-01-31 05:00:00 47.5243
##  2 2005-02-28 05:00:00 46.4542
##  3 2005-03-31 05:00:00 45.0862
##  4 2005-04-29 04:00:00 44.3453
##  5 2005-05-31 04:00:00 43.7327
##  6 2005-06-30 04:00:00 42.8133
##  7 2005-07-29 04:00:00 42.0767
##  8 2005-08-31 04:00:00 41.3695
##  9 2005-09-30 04:00:00 40.7932
## 10 2005-10-31 05:00:00 40.0737
## # ... with 143 more rows

Sector Performances

Various real-time and historical sector performances are available.

av_get(av_fun = "SECTOR")
## # A tibble: 106 x 3
##                       rank.group                     sector value
##                            <chr>                      <chr> <dbl>
##  1 Rank A: Real-Time Performance                     Energy  0.81
##  2 Rank A: Real-Time Performance                  Materials  0.63
##  3 Rank A: Real-Time Performance     Consumer Discretionary  0.46
##  4 Rank A: Real-Time Performance                 Financials  0.44
##  5 Rank A: Real-Time Performance           Consumer Staples  0.42
##  6 Rank A: Real-Time Performance                Industrials  0.15
##  7 Rank A: Real-Time Performance Telecommunication Services  0.04
##  8 Rank A: Real-Time Performance     Information Technology -0.05
##  9 Rank A: Real-Time Performance                Real Estate -0.08
## 10 Rank A: Real-Time Performance                Health Care -0.08
## # ... with 96 more rows

Important Notes: av_get()

For the most part, the av_get() function works the same as the Alpha Vantage API Parameters. However, users will want to understand a few important aspects to the R interface:

  1. 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.

  2. 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").

  3. There is no need to specify the datatype parameter as an argument to av_get(). The function will return a tibble data frame.

  4. Some data sets only return 100 rows by default. Change the parameter outputsize = "full" to get the full dataset.

Next Steps

We have plans to integrate alphavantager into tidyquant, which will enable scaling from one equity to many! The change will be incorporated into tq_get(), the one-stop shop for getting financial data. It’s coming soon!!!

About Business Science

We have a full suite of data science services to supercharge your organizations financial and business performance! For example, our experienced data scientists reduced a manufacturer’s sales forecasting error by 50%, which led to improved personnel planning, material purchasing and inventory management.

How do we do it? With team-based data science: Using our network of data science consultants with expertise in Marketing, Forecasting, Finance, Human Resources and more, we pull together the right team to get custom projects done on time, within budget, and of the highest quality. Learn about our data science services or contact us!

We are growing! Let us know if you are interested in joining our network of data scientist consultants. If you have expertise in Marketing Analytics, Data Science for Business, Financial Analytics, Forecasting or data science in general, we’d love to talk. Contact us!

Follow Business Science on Social Media

To leave a comment for the author, please follow the link and comment on their blog: business-science.io - Articles.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)