Backtrader: Alpha Vantage Data – Direct Ingest

As noted back in April last year, many of the good quality, free equity data sources from the likes of Yahoo and Quandl are disappearing. This is why companies such as Alpha Vantage are an absolute godsend for the tinkerer and cash-challenged retail trader!

In that first post last year, we created a simple script to download and save EOD (End Of Day) data to a CSV File. Now, as a follow up to that script, we will take things a step further. Rather than saving the data to a CSV file, the example code in this post will download the data and directly ingest it into backtrader as a Pandas data feed. This will be useful for those who do not wish to store and manage a large library of CSV files or just want to test some random tickers from time to time.

Getting an API Key

Before we dive into the code. In order to download the data from Alpha Vantage, you must register for an API key. It is free and will grant you lifetime access. If you don’t have one already, head over to the following link and sign up before continuing.



Once we have an API key, we also need to install a python package to interact with the API. Romel Torres has created an excellent python wrapper for the API. To install it simply type:

pip install alpha_vantage

Remember to replace pipwithpip3if you already have python2 installed.

alpha_vantagerequires a number of dependencies such as pandas. However, Pip should handle it, download and install all required packages for you.

The Code

Code Commentary

Before trying to run the code, be sure to update the API key:

Most of the magic in this script happens within the alpha_vantage_eod()function. It is here that we download the data and massage it into a format that we can easily pass to Backtrader.  As such, it makes sense to go over the function parameters:

  • symbol_list: Is a simple list of symbols/tickers that we will download. If you just want to download a single instrument then simply place a single ticker inside a list like so: symbol_list = ['AAPL']
  • compact: This is useful for testing. It will download only the last 100 data points and as such data download speeds are much quicker. Set this parameter to Trueif you wish to speed things up.
  • debug: Will just provide some extra prints to the terminal in case you are getting some weird results. Using this option may allow you to see which symbol/ticker is giving you pain.

The alpha_vantage_eod()function will return a nested list of data feeds. In other words, each entry in the returned list is another list. The nested (second) lists contain two items. The first item (at index position [0]) is the pandas dataframe and the second item (at index position [1]) is the symbol string for that dataframe. Having  easy access to the symbol allows us to add it as the name of the datafeed in backtrader.

Regarding the strategy, it was only designed for testing / checking the feeds are added correctly. However, one thing that can be noted is that the next()method (function) has been coded in a way to support multiple data feeds.  As such, the strategy can support one or more data feeds and handle them appropriately to print out their OHLCVvalues. For more information on how to create a more complete strategy that supports multiple data feeds, see this tutorial.

For those interested, further API information can be found here:



Running the script you should see some output that looks like this:

Image showing the terminal output from the code example

Notes and Further Improvements:

  1. Alpha Vantage has API call limits. When using their free API, you are limited to 5 API calls per minute and a total of 500 a day. Therefore, if you have a large symbol list (over 5) then you will need to add a sleep function to wait 12 seconds between downloads. This is obviously slow down the initialization of the backtest too. So if you plan on making a large number of API calls, you may want to consider saving the output to CSV as shown in the previous post.
  2. The example code above downloads unadjusted data. If you require adjusted data, Alpha Vantage does provide this. However, they only provide an adjusted close value. Therefore, some extra work is required to massage the open, high and low values if you still want to work with OHLCdata. A future post may cover this!
  3. Alpha Vantage also provide Intra-day Equity, FX and Crypto data! Readers may want to use this code example as a base to access these other feeds.