Market Data & Web Scraping Beginner

Fetch US Treasury Yield Curve (10Y-2Y Recession Spread) & VIX in Python

Track macro recession signals in Python by fetching 10-Year vs 2-Year Treasury yields, yield curve inversion spreads, and CBOE VIX via yfinance/FRED.

#pandas#yfinance

Problem Statement

Macro quants and long-term equity allocators monitor the 10Y–3M and 10Y–2Y Treasury yield spreads alongside VIX to scale back equity exposure before credit cycle contractions.

Formula / Core Logic

Term_Spread = Yield_10Y (^TNX) - Yield_13W (^IRX) Inversion_Flag = True when Term_Spread < 0

Installation

pip install pandas yfinance
us-treasury-yield-curve-10y-2y-inversion-fetcher-python.py
import yfinance as yf
import pandas as pd

def fetch_macro_yield_regime(period='1y'):
    tickers = {'US_10Y': '^TNX', 'US_3M': '^IRX', 'VIX': '^VIX'}
    data = yf.download(list(tickers.values()), period=period, progress=False)['Close']
    df = pd.DataFrame()
    df['Yield_10Y'] = data['^TNX']
    df['Yield_3M'] = data['^IRX']
    df['VIX'] = data['^VIX']
    df['Spread_10Y_3M'] = (df['Yield_10Y'] - df['Yield_3M']).round(3)
    df['Yield_Curve_Inverted'] = df['Spread_10Y_3M'] < 0
    return df.dropna()

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why does yield curve un-inversion after an inversion matter?

Historically, equity drawdowns often begin when the yield curve steepens back above zero after a prolonged inversion as central banks cut short-term rates.