You've successfully subscribed to Alpaca Learn | Developer-First API for Crypto and Stocks
Great! Next, complete checkout for full access to Alpaca Learn | Developer-First API for Crypto and Stocks
Welcome back! You've successfully signed in.
Success! Your billing info is updated.
Billing info update failed.
Community Examples

# How to Use Unemployment Figures to Build a Trading Algorithm - Part 1

An asset's intrinsic value is the sum of the expected discounted cash flows over its life. With fundamentals being the primary determinant of value, I often wonder why more systematic traders don't incorporate such valuable information into their algorithms.

This article shows you how to connect to the economic data from The Federal Reserve Economic Data of St. Louis ("FRED") using their Python API to get the fundamentals data such as monthly unemployment figures.

Hello world, my name is Leo Smigel. I combine investing, trading, and data science to make more money in the markets. You can check out my website if you're interested in a data-driven approach to quantamental investing.

In this two-part series, I will show you how to combine a standard trend indicator with economic data from one of my favorite sources: The Federal Reserve Economic Data (FRED) of St. Louis.

In this article, I will show you how to connect to FRED using their Python API. We'll get the monthly unemployment figures and use both price and year-over-year unemployment as our market regime filter. Systems such as dual-momentum use the 200-period SMA to transition from stocks into bonds.

In the next post, we'll cover how to build the algorithm with backtrader and Alpaca.

## Get a FRED API Key

First, you'll have to create an account with the FRED. This is a relatively straightforward process that you can follow here. Once you have your API key, we'll want to install fredapi.

## Get the Data

Let's use the environment we created for the Tools of the Trade series and open our Python interpreter.

``````Python` 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
>>> ``````

Using fredapi is easy and also well documented. We'll go over a simple example in this post, but if you're looking for revisions and other data, I suggest reading the documentation above.

``````from fredapi import Fred
fred = Fred(api_key='YOUR_API_KEY_HERE')
unrate = fred.get_series('unrate')``````

This returns a pandas timeseries. Now let's compare with the prior year's numbers to see if our unemployment trends up or down.

``unrate_trending_higher = unrate > unrate.shift(12)``

We can see that we would have turned our regime filter shows the unemployment rate rising in March, which we would get in April. Don't accidentally introduce look-ahead bias.

``````2019-12-01 False
2020-01-01 False
2020-02-01 False
2020-03-01 True
2020-04-01 True
2020-05-01 True
2020-06-01 True
2020-07-01 True
2020-08-01 True
2020-09-01 True``````

I would advise using multiple economic factors, but for our example, this will suffice.

## So What's Next?

In the next post, we will add these economic data to Backtrader and run everything through Alpaca. If you haven't yet, read the Backtrader series and try to create the regime filter before next month.