Backtrader: Stock Screener with Alpha Vantage

Backtrader, oh how we have missed you! It has been too long since the last article on this excellent platform. Hopefully today, we can make up for that neglect by using Backtrader as the engine for a stock screener. The platform is a perfect choice for a stock screener given how easy it is to create custom Indicators. Couple that with an already impressive library of built-in options and it becomes easy to screen for whether your favourite indicator is bullish or bearish.


Before we start, it is worth pointing out that this article builds on the shoulders of some other posts. More specifically, we will be using Alpha Vantage to provide data for the screener. As such, if you are interested in the mechanics of how we download the data, it would be worth looking at those articles first.

Even if not, at a bare minimum, you will need to sign up for an API key from Alpha Vantage. Again, if you are not sure how to do this, the same articles will cover those steps:

  1. Replacing Quandl Wiki Data with Alpha Vantage
  2. Backtrader: Alpha Vantage Data: Direct Ingest


Moving onto scope, the code should allow users to download daily data for up to 500 instruments. The data will then be fed into Backtrader which will, in turn, run through the data and calculating indicator values. At the end of the run, we will create a report based on the values of the last bar of data to show if the indicators are bullish or bearish.

The idea is that you could then follow the example to add to or replace indicators that interest you.

Before Running

Make sure you find the line the following line in the code example and insert your API key.

As you might imagine, the script will not work if you don’t!



We kick-off this script by downloading data for each ticker in our ticker list. For this version, we house them in a simple list. If your list of tickers is going to be large or maintained somewhere else, users might want to build on this by importing a ticker list from a file.

When we start downloading data, we loop through each item in the list and make a call to the API. Unfortunately, due to Alpha Vantage having strict API limits, we need to perform a long wait between each data download request. This results in the script waiting 12 seconds between download calls because we are limited to 5 API calls per minute.

Showing Backtrader Screener downloading data and sleeping

For this reason, this screener is not really intended to be used on intra-day timeframes. It would be too slow unless your ticker list was quite small. Having said that, if you wish to swap out the data part with another service, then the rest of the code should still work. Another limitation is that Alpha Vantage’s free API is limited to 500 calls a day. Therefore, if you are thinking of intra-day screening, you would need to avoid making too many sweeps.

After we have all the data, it is just a matter of adding our favorite indicators to a dictionary. The dictionary is structured in such a way that we can conveniently make a report from it later. During __init__()we create that dictionary and add the indicators and create some “data feeds”. These new feeds signal whether the indicator is bullish or bearish. Creating a feed is as simple as creating a condition which returns TrueorFalse. That is one of the great things about Backtrader, it is really easy to create new data feeds.

The RSI example shows this on the line:

As you can see, we are just checking if the RSI is over 50. If it is, then the data feed will be True, if not, it will be False.

For more information see:

Adding Extra Indicators

So if you want to add extra indicators, you simply need to follow the same format of the examples

That means:

  1. Create a new entry in self.indsand make it as a dict()type.
  2. As you loop through the data feeds, use the data feed name (d._name) to create another new entry in the entry you just made.
  3. Add the indicator at a ['value']key.
  4. Create a check for whether the indicator is bullish or bearish. This should be a boolean value.

As long as you follow the correct format when adding new indicators during __init__()then the stop()method will not require any updates.

Running the Code

After running the Backtrader screener, you should have a table output which looks like this:

Showing the final output of the backtrader screener

If you are checking a lot of instruments, you might want to consider running this as a nightly job after the markets have closed.