Last Updated on September 21, 2023
Finding reliable sources of data to backtest your trading strategies can be difficult sometimes. Luckily, today we will show you a website to download historical economic data for free using Python.
We show you how to download FRED data using Python. The FRED website is an economic and financial database run by the FED of St. Louis. It is very popular among traders and investors to follow economic news and develop and backtest trading strategies.
In this article, we are going to look at how to download data from the FRED website, develop a trading strategy using economic indicators, backtest the strategy, and plot the returns using Python.
What is the FRED website, and how to get the data in Python
The Federal Reserve Economic Data (FRED) website provides historical US economic and financial data.
It includes data points of different economic indicators like GDP, interest rates, unemployment, and many others. Navigating is very intuitive, and data can be downloaded in several formats. It’s a great resource for traders and investors.
What’s best is that we can download the data from the FRED website using Python to create an economic dashboard or build a trading strategy. Moreover, the data gets automatically updated in our code as soon as it changes on the website.
In order to download economic data in Python, we need to first create a new notebook to write our code and then import the pandas_datareader library.
Once we have done this, we will need to find the “code name” of the economic indicator we want to download on the FRED website. For example, the code name for the CPI index is USACPIALLMINMEI.
For the strategy we will use as a trading strategy and backtest today, we will use the Chicago Fed National Financial Condition Index (NFCI).
Besides this, there is not much more to it. We are going to define a variable, which we will name df, and use the pdr.DataReader() function. Inside the parentheses will go the code name of the economic indicator, followed by ‘fred’ to let them know the website we will be downloading the data from, and the start and end date we will use. In this case, we will start the backtest in 2010.
Creating a trading strategy based on FRED data in Python
We are going to develop a simple strategy using the NFCI and the SPY. The strategy’s trading rules are as follows:
- Buying the SPY when the NFCI is under its 14-day moving average
- Selling and holding cash when the NFCI is over its 14-day moving average
Please keep in mind that the strategy is only for informational purposes and serves as an example.
First, we are going to import the pandas, finance and matplotlib.pyplot. These are the libraries we will be using to backtest the trading strategy.
After we are done with that, we are going to calculate the NFCI 14-day moving average. This is done by using the .rolling() pandas function and adding a .mean() at the end.
Next, we will download the SPY historical data from Yahoo Finance using the finance library. We have another post explaining in detail how to do this, but in this particular case, we have to use the .resample().last() function to change the time frame from daily to weekly (This is because the NFCI is released every Friday).
Last, we will create one last column to calculate the weekly change in the SPY using the pct.change() function and adding +1 in the end.
Now it only rests on calculating the trading signals. We will create a new column called ‘regime’ and fill it with 1.
Then, using the for function we are going to do a loop through the data frame and put a 0 in the row where the NFCI is greater than its 14-day moving average.
In other words, when the value in the regime column is 1, it means that we are long SPY, and when it is 0, we sell and hold cash.
If we print df, here is the output:
Calculating the returns of the strategy
Calculating the returns of the strategy is very easy.
We are going to create a column in df called ‘returns’ and put the weekly change of the SPY when the last week’s signal was buy and a 1 when the last week’s signal was 0.
After doing this, we effectively finished the backtest. Now we have to plot the returns and see the performance.
Plotting the returns of the trading strategy using Python
To plot the returns of the strategy we are going to use matplotlib.pyplot. These are what each of the following lines of code does:
- Creates a blank chart of 11 inches in width and 7 inches in length
- Plot the cumulative returns of the strategy and add a label called ‘Strategy’
- Same but with the cumulative returns of the SPY and the label called ‘Buy and Hold’. Also we change the color of the line to ‘gold’.
- Shows the labels in the chart
- Creates a title at the top of the chart
And here is the output:
The strategy returned 447% since 2010 while buy and hold returned 406%. Furthermore, the strategy was invested just over half the time – 55%.
How to build a trading strategy from FRED data in Python – conclusion
To sum up, you learned how to download data from the FRED website in Python and use it for backtesting a trading strategy. However, there are many other projects you can build using this database. Backtesting a trading strategy is just one of them!