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 account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
Search
Community Examples

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

Leo Smigel
Leo Smigel

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.

Quantamental: What It Is & Why It Works
Quantamental refers to an investment strategy that combines quantitative approaches using computers, mathematical models, and big data with fundamental methods that analyze individual company cash flows, growth, and risk to generate better risk-adjusted returns.

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.

Federal Reserve Economic Data | FRED | St. Louis Fed
Download, graph, and track 766,000 economic time series from 101 sources.

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.

Welcome - Backtrader
python backtesting trading algotrading algorithmic quant quantitative analysis

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.

mortada/fredapi
Python API for FRED (Federal Reserve Economic Data) and ALFRED (Archival FRED) - mortada/fredapi

Get the Data

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

Alpaca & Backtrader: Tools of the Trade (Part 1)
RSI stack strategy w/ Alpaca and Backtrader. In #1, we’ll cover connecting the Backtrader backtesting to Alpaca to load in data for multiple time frames.
Python` 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

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.

Time Series Analysis with Python Made Easy
A time series is a sequence of moments-in-time observations. The sequence of data is either uniformly spaced at a specific frequency such as hourly, or sporadically spaced in the case of a phone call log.
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.

Look-Ahead Bias: What It Is & How to Avoid
Look-ahead bias occurs by using information that is not available or known in the analysis period for a study or simulation, leading to inaccurate results.
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.

In the meantime, if you're interested in learning more about backtrader or trading, please visit analyzingalpha.com.

Analyzing Alpha
Analyzing what works in trading and investing using data science.

Follow @AlpacaHQ on Twitter!

Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.

Community Examples

Leo Smigel

Former tech entrepreneur turned algorithmic trader. I analyze what works in trading and investing using data science.