top of page

Get auto trading tips and tricks from our experts. Join our newsletter now

Thanks for submitting!

Writer's pictureBryan Downing

How to Use Python to Demo the Steepening Yield Curve

Understanding the Yield Curve


A steepening yield curve is a graphical representation of the relationship between interest rates (or yields) on debt instruments with different maturities but similar credit quality. It plots the yields of bonds with varying maturities, from short-term to long-term, at a specific point in time. The shape of the yield curve provides valuable insights into market expectations about future interest rates, economic growth, and inflation.



steepening yield curve in python





Components of the Yield Curve


The yield curve is typically constructed using U.S. Treasury securities because they are considered risk-free. The maturities of these securities range from a few months (e.g., Treasury bills) to 30 years (e.g., Treasury bonds). The yield curve is plotted with the yield on the vertical axis and the time to maturity on the horizontal axis.

Get the code here


Types of Yield Curves


There are three main types of yield curves:


  1. Normal Yield Curve: This is the most common type, where short-term yields are lower than long-term yields. This upward-sloping curve indicates that investors expect stronger economic growth in the future, leading to higher inflation and consequently higher interest rates.

  2. Inverted Yield Curve: This occurs when short-term yields are higher than long-term yields. This downward-sloping curve is often seen as a predictor of an economic recession. High short-term rates can stifle economic activity, leading to lower inflation and lower long-term rates.

  3. Flat Yield Curve: This occurs when there is little difference between short-term and long-term yields. This curve suggests uncertainty about future economic growth and inflation.


Factors Affecting the Yield Curve


Several factors can influence the shape and movement of the yield curve:


  • Economic Growth: Strong economic growth typically leads to higher inflation expectations and higher long-term interest rates, resulting in a steeper yield curve.

  • Inflation: Higher inflation erodes the purchasing power of future cash flows, leading investors to demand higher yields on long-term bonds.

  • Monetary Policy: The Federal Reserve's monetary policy decisions, such as setting the federal funds rate, can significantly impact short-term interest rates and the shape of the yield curve.

  • Market Expectations: Investors' expectations about future economic conditions, inflation, and interest rates play a crucial role in determining the yield curve's shape.






Importance of the Yield Curve



The yield curve is a valuable tool for investors, economists, and policymakers:


  • Economic Forecasting: The shape of the yield curve can provide insights into future economic growth and potential recessions.

  • Investment Decisions: Investors use the yield curve to make informed decisions about bond investments, such as choosing appropriate maturities and assessing potential risks and returns.

  • Monetary Policy: The Federal Reserve monitors the yield curve to assess the impact of its monetary policy decisions on the economy.


Python Code Demo


Here's a Python code demo that fetches historical U.S. Treasury yield data using the yfinance library and plots the yield curve using matplotlib:



Python

import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt

# Fetch U.S. Treasury yield data
tickers = ['^IRX', '^FVX', '^TNX', '^TYX']  # 3-month, 5-year, 10-year, and 30-year Treasury yields
data = yf.download(tickers, start='2020-01-01', end='2023-01-01')['Adj Close']

# Rename columns for clarity
data.columns = ['3M', '5Y', '10Y', '30Y']

# Plot the yield curve for a specific date
date = '2022-01-03'
yields = data.loc[date]
maturities = [3/12, 5, 10, 30]  # Convert 3-month to years

plt.figure(figsize=(10, 6))
plt.plot(maturities, yields, marker='o')
plt.title(f'Yield Curve on {date}')
plt.xlabel('Maturity (Years)')
plt.ylabel('Yield (%)')
plt.xticks(maturities, ['3M', '5Y', '10Y', '30Y'])
plt.grid(True)
plt.show()

# Plot yield curves over time
plt.figure(figsize=(12, 8))
for ticker in tickers:
    plt.plot(data.index, data[ticker], label=ticker)
plt.title('Treasury Yields Over Time')
plt.xlabel('Date')
plt.ylabel('Yield (%)')
plt.legend()
plt.grid(True)
plt.show()

# Calculate and plot the 10-year minus 2-year spread
data['10Y-2Y'] = data['10Y'] - data['5Y']
plt.figure(figsize=(12, 6))
plt.plot(data.index, data['10Y-2Y'], label='10Y-2Y Spread')
plt.title('10-Year Minus 5-Year Treasury Yield Spread')
plt.xlabel('Date')
plt.ylabel('Yield Spread (%)')
plt.legend()
plt.grid(True)
plt.show()

This code fetches historical data for 3-month, 5-year, 10-year, and 30-year Treasury yields, plots the yield curve for a specific date, visualizes yield changes over time, and calculates the 10-year minus 5-year spread, which is a common indicator used to predict recessions.

This document provides a comprehensive overview of the yield curve, including its components, types, influencing factors, importance, and a Python code demo for visualizing yield curve data.

コメント


bottom of page