In our previous post, we discussed how to backtest a simple crossover strategy for a single stock using Backtrader and Intrinio. In this post, we're going to build on this framework and graph the RSI of multiple securities. Before we can start graphing, we need to get the data.
If we haven't met yet, my name is Leo Smigel and I'm a quantamental investor that uses Backtrader and Intrinio to make money in the markets. You can find the code and associated files for this post in the Intrinio section on the Analyzing Alpha Github.
Let's start by activating our environment and opening the Python interpreter in the directory where we want to save our data files. If any of this is new to you, please read part one of this series.
Now that you're in the Python interpreter, let's import CSV and the Intrinio API so we can save the price data to comma-separated files.
With the Intrinio SDK imported, we need a list of stocks to work with. The Intrinio Developer Sandbox provides us with the Dow 30. Let's create a list with these stocks in alphabetical order so we can iterate through them and save the pricing data as CSV files.
With a list of the Dow 30, we can now iterate through them using code similar to part one of this series. Let's grab the security_api so we can do just that.
Now let's loop through our list to get each stock's OHLCV data, and then save it to a CSV file.
You should now have the Dow 30 price data as comma-separated-files in the directory you ran the Python interpreter from.
With the data in place, let's expand upon our previous backtesting code to include multiple securities. We'll first grab backtrader. We don't need to include intrinio_sdk or csv here unless you want to lump everything into one file.
Next up, we'll create a dictionary for the Dow 30. I copied and pasted from the dictionary above.
With a list of our stock tickers, we can now create our strategy. Backtrader uses an object-oriented approach. We create a class called MyStrategy and define a few params. Parameters are simply a dictionary to store values to make our lives easier. Backtrader does work behind the scenes to make these params available to us.
Without getting too technical, we use initialize to more efficiently calculate the RSI. We'll get into this later. __init__ loops through the data files provided from Intrinio and calculates an RSI.
Now that our strategy has been created, we can run our backtest. You'll notice I've added commissions and I'm looping through the CSV files and adding them as datas. Note how this time I've added a name to the data. We'll use this in the future. Also, to make this more readable, I'm only adding three stocks for our graph via slicing: for stock in STOCKS[:3].
When you execute the code, it'll create a graph that looks like the following.
Congratulations! In the next post in this series, we'll rank the Dow 30 by RSI and create a mean reversion strategy. For those of you that can't wait, try to see if you can do it on your own before next month's post. All of the code you'll need can be found on the stop-loss strategy backtest on Analyzing Alpha.
New to Intrinio? Visit intrinio.com to learn more.