Predict recessions with the yield curve

February 4, 2023
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

Predict recessions with the yield curve

Whether you trade your own small account, or manage hundreds of billions of dollars of investor money, the economy matters. The economy helps you know which way to be biased—bullish or bearish.

How do you form a view?

  • Buy an analyst report
  • Read “expert” commentary
  • Do what most people do, guess

None of these options sounds really great.

Unfortunately, small investors don’t have teams of economists to research and give guidance on the economic picture.

Today, I’m going to show you how to build an animated chart of the yield curve to form a view of state of the economy.

The yield curve is a plot of the yield of different key US treasuries against their maturity. The yield curve provides important insight into investors’ predictions about the economy. “Inverted” yield curves have been shown to reliably predict recessions.

Normally, longer-dated treasuries yield more than shorter-dated ones. This is because there is more risk in lending money for longer. When the yield curve inverts, longer-dated treasuries yield less than shorter-dated ones. This happens when the market expects the Fed to lower interest rates in the future in anticipation of a recession.

Investors use an inverted yield curve as a sign to reduce risk in their portfolios.

You can build an animated chart of the US yield curve to form a view of the macroeconomic state.

All without paying a thing.

Imports and set up

Start with importing NumPy, Matplotlib, and the OpenBB SDK. You can use the OpenBB SDK to get yield curve data for the St. Louis Fed’s data portal (FRED). To use the data, you need free FRED API keys.

1%matplotlib notebook
2
3import numpy as np
4import matplotlib.pyplot as plt
5import matplotlib.animation as animation
6
7from openbb_terminal.sdk import openbb
8
9font = {
10    "family": "normal",
11    "weight": "normal",
12    "size": 12
13}
14
15plt.rc('font', **font)
16
17openbb.keys.fred(
18    key="inert_your_api_key_here",
19    persist=True,
20)

Set up the bond maturities you want to use to build the yield curve. There are conventions about which maturities to use, but there is no “right” answer. The point is to get a good representation across the maturities.

1maturities = ['3m', '6m', '1y', '2y', '3y', '5y', '7y', '10y', '30y']
2
3data = openbb.economy.treasury(
4    instruments=["nominal"],
5    maturities=maturities,
6    start_date="1985-01-01"
7)
8data.columns = maturities
9
10data["inverted"] = data["30y"] < data["3m"]

Use the OpenBB SDK to acquire the rate data for all the maturities at once. Using the nominal instrument type returns the coupon rate (non-inflation adjusted) on the bond. Mark the yield curve as inverted if the yield on the 30-year maturity is less than the 3-month maturity. Some people use the 30-year and the 10-year, or the 10-year and the 3-month. Use what makes sense to you based on your analysis.

Build the animated chart

With Matplotlib, you can animate a chart. Animated charts offer a great way to visualize how data changes through time. First, set up the plots. This boilerplate code sets up the figure, creates the tick ranges and labels, and sets the axis labels. 

1# Initialize figure
2fig = plt.figure()
3ax = fig.add_subplot(1, 1, 1)
4line, = ax.plot([], [])
5
6# Set the range of ticks
7ax.set_xlim(0, 7)
8ax.set_ylim(0, 20)
9
10# Set the tick locations
11ax.set_xticks(range(8))
12ax.set_yticks([2, 4, 6, 8, 10, 12, 14, 16, 18])
13
14# Set the axis labels
15ax.set_xticklabels(["1m","3m","6m","1y","5y","10y","20y","30y"])
16ax.set_yticklabels([2, 4, 6, 8, 10, 12, 14, 16, 18])
17
18# Foce the y-axis labels to the left
19ax.yaxis.set_label_position("left")
20ax.yaxis.tick_left()
21
22# Add the axis lables
23plt.ylabel("Yield (%)")
24plt.xlabel("Time to maturty")

Next, build the animation functions.

1def init_func():
2    line.set_data([], [])
3    plt.title("U.S. Treasury Bond Yield Curve")
4    return line
5
6def animate(i):
7    x = range(0, len(maturities))
8    y = data[maturities].iloc[i]
9    dt_ = data.index[i].strftime("%Y-%m-%d")
10    
11    if data.inverted.iloc[i]:
12        line.set_color("r")
13    else:
14        line.set_color("y")
15    
16    line.set_data(x, y)
17    
18    plt.title(f"U.S. Treasury Bond Yield Curve ({dt_})")
19    return line,
20
21ani = animation.FuncAnimation(
22    fig, 
23    animate,
24    init_func=init_func,
25    frames=len(data.index),
26    interval=5,
27    blit=True
28)

The first function sets the initial state of the chart. The animate function grabs the data to plot, changes the curve color to red when the yield curve is inverted, and sets the data. The FuncAnimation function then does the work of looping through each row of data, creating the plot, and displaying it to you.

When you run the code, you'll see how the yield curve evolves over time. When it flashes red, it's inverted. You can also see the overall level of interest rates and how they rose and fell over the same time period.

Skip the paid research reports. Animate the yield curve with Python and build your own analysis of the macroeconomic situation to improve your investing.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.