Back to Projects
Bond Pricing and Analysis Tool

Bond Pricing and Analysis Tool

PythonFRED APIPlotlySciPy

Problem and Motivation

Bonds are one of the most important instruments in finance and one of the least talked about. They fund governments and corporations, anchor institutional portfolios, and sit at the center of how the Federal Reserve moves money through the economy. Yet in most finance courses, bonds get a week of coverage before the syllabus moves on to equities.

After a conversation with an analyst at a well-respected hedge fund who told me that bonds are often considered the more abstract instrument compared to stocks, I realized how much I had been underestimating them.

My goal was simple: build something that forced me to understand bond pricing from the ground up, using real market data, not textbook assumptions.

Approach and System Design

The tool is built in Python across five modules and pulls live data from two sources: the Federal Reserve Economic Data API (FRED) for treasury yields and macroeconomic indicators, and Yahoo Finance for SPY price history.

On the pricing side, the tool implements the standard discounted cash flow formula for fixed-coupon bonds, interpolating discount rates from the live yield curve using NumPy for maturities where FRED does not publish a direct series. Beyond price, the tool computes Macaulay duration, modified duration, and convexity from the full cash flow schedule, and solves for yield-to-maturity numerically using Brent's method via SciPy.

The visualization layer generates four outputs: a live yield curve, an interactive Plotly bond pricing chart with a coupon rate slider, a decade of historical treasury yield data, and a 2x2 panel correlating treasury yields against unemployment, inflation, and the S&P 500. All macroeconomic series in the correlation panels are normalized to z-scores via scikit-learn's StandardScaler so that variables with very different raw scales can be compared directionally on the same chart.

Challenges and Tradeoffs

The hardest piece was sourcing the discount rate correctly. FRED provides yield data only at benchmark maturities, so pricing a bond at any other maturity requires interpolation. Getting that interpolation to behave realistically, particularly when the yield curve is inverted or humped as it was at the time this project was built, took more iteration than expected.

The YTM calculation presented a similar challenge: there is no closed-form solution, so the tool uses Brent's method with a yield bracket of 0.0001 to 10.0. Understanding why that bracket needs to be set that way, and what happens at the boundaries, was a meaningful exercise in numerical methods.

The biggest scoping tradeoff was restricting the project to U.S. Treasury bonds. Corporate and municipal bonds introduce credit risk, liquidity premiums, and issuer-specific factors that would have required a fundamentally different data pipeline and pricing model. Keeping the scope to Treasuries made the analysis cleaner and kept the focus on the macroeconomic dynamics that were the real point of the project.

Outcomes and Impact

Delivered a fully functional bond pricing engine using live FRED data, computing price, duration, convexity, and YTM across any Treasury maturity from 1 to 10 years.

Turned a personally confusing subject into something I can reason about from first principles, through building rather than reading.

Limitations and Next Steps

The correlation analysis raised more questions than it answered. The relationships between macroeconomic variables and treasury yields are real, but they are regime-dependent and heavily distorted by structural breaks like 2020, making it difficult to draw conclusions that generalize. A rigorous next step would be a time-varying correlation model or a regime-switching framework that can account for how these relationships change across economic cycles.

On the pricing side, the natural extension is portfolio-level analysis: aggregate duration and convexity across a set of positions, and scenario analysis showing how the portfolio's value changes under parallel and non-parallel yield curve shifts.