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

Written by Matt Dancho on September 3, 2017

Free Data Science Course: Jumpstart with R

We've just launched Jumpstart with R, the perfect course to jumpstart your skills!

Start FREE Course Now

Free Jumpstart with R Course

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:

# Or

Load the package.


Set your API key (get one from Alpha Vantage if you don’t already have one… it’s free).

## [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 ... 
## function (symbol = NULL, av_fun, ...) 

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
##                                  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


NEW - Data Science Fundamentals Newsletter

We just launched a new initiative to help you take your data science skills to the next level. Every Tuesday we send you new resources, tips, and advice to accelerate your learning.

Data Science Fundamentals

Sign Up For Data Science Fundamentals Newsletter

Data Science for Business Curriculum

Business Science University is an educational platform that teaches how to apply data science to business. Our offering includes of a fully integrated, project-based 3-Course R-Track.

BSU R-Track Course Curriculum

Each course takes the student through their progression in a data science journey. Begin your journey with DS4B 101-R which teaches foundations using the tidyverse. Next, master machine learning for business with DS4B 201-R, where you learn H2O and many advanced R packages. Finish with DS4B 301-R where you learn to develop high-performing web applications using Shiny, a powerful framework for productionizing R code.

R-Track Curriculum Summary

Business Analysis with R (Beginner) - Data Science Foundations 7-Week course 12 tidyverse Packages 2 business projects
Data Science For Business with R (Intermediate/Advanced) - Machine Learning + Business Consulting 10-Week course H2O, LIME, recipes, and 10 more packages 1 end-to-end business project
Web Apps for Business with Shiny (Advanced) - Web Frameworks (Bootstrap, HTML/CSS) and Shiny 6-Week course Shiny, shinytest, shinyloadtest, profvis, and more! Take machine learning model into production

Join Business Science University Today

Stay Connected, Get Updates, Learn Data Science

If you like our Business Science Software (anomalize, tidyquant, tibbletime, timetk, and sweep), our courses, and our company, you can connect with us:

Start learning today! Business Science University

Subscribe and we'll keep you updated.