Download Stocks quotes using R and AlphaVantage

Let´s start by covering the basics.

What you need:


  • In case you are installing for the first time or doing a fresh install, I would recommend to install both R and RStudio in different folders inside the “C:\” directory.
  • Another recommendation, (not required) install everything using Anaconda. Anaconda is a built in suite for Quant and data analysis. It´s easy to use, install and maintain. From the Anaconda ecosystem you can install python, orange and others awesome tools.

Now to the subject at hand. There are a few different ways to retrieve stock data. Some paid, some free, some fast, some slow. Today we will focus on AlphaVantage (

Let´s start by claiming your free API Key:

You will have to fill a small form and no payment is required, It´s free:

AlphaVantage API Form

As all things free, it comes with limitations.

API free restrictions

Now Let´s bring up our Rstudio

To run our code, we will need to basic packages:

  • alphavantager to connect to AlphaVantage API
  • xlsx to export our data to excel/csv

Let´s start by installing the packages:



RStudio packages installation

Now we are set to go, let´s go right into the code:

Let´s start by initializing the libraries



Replace “YOUR_API_KEY” with the API key you registered above


We can retrieve data from AlphaVantage with different timeframes:

#Daily data
quoteData <- av_get(
  symbol = "AAPL",
  av_fun = "TIME_SERIES_DAILY",
  outputsize = "full"

#Hourly data
quoteData <- av_get(
  symbol = "AAPL",
  interval = "60min",
  outputsize = "full"

The Symbol is the ticker you want to retrieve. If you have any doubts or don´t know the ticker for the company, just try searching for it on yahoo finance ( the ticker is exactly the same.

Finally, in order to export to excel you need to define where you want to save it and how you want to name the file

write.xlsx(quoteData, "C:\\Folder\\SymbolName.xlsx",sheetName = "Daily data")

Here is the full code snippet

If you have any question, just leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *