Fixed income is the topic many CFA Level I candidates fear most. The formulas look heavy on paper, but in code they're just loops over cash flows. Writing them yourself is one of the fastest ways to actually understand them.

In this post we'll build three core CFA Level I tools in Python: bond price, yield to maturity (YTM) and duration.

The example bond

A 5-year bond with a 7% annual coupon paid semi-annually, face value ₹1,000, and a market yield of 8%.

Because the coupon (7%) is below the market yield (8%), the bond should trade at a discount, below ₹1,000. Let's check.

1. Bond price

A bond's price is the present value of its coupons plus the present value of its face value.

def bond_price(face, coupon_rate, ytm, years, freq=2): c = face * coupon_rate / freq # coupon per period r = ytm / freq # yield per period n = int(years * freq) # number of periods pv_coupons = sum(c / (1 + r) ** t for t in range(1, n + 1)) pv_face = face / (1 + r) ** n return pv_coupons + pv_face print(bond_price(1000, 0.07, 0.08, 5)) # 959.45 

₹959.45, a discount, as expected.

2. Yield to maturity

YTM is the reverse problem: given the price, find the yield. There's no closed-form formula, which is why the exam has you use a financial calculator. In Python we use a root finder.

from scipy.optimize import brentq def ytm(price, face, coupon_rate, years, freq=2): return brentq( lambda y: bond_price(face, coupon_rate, y, years, freq) - price, -0.99, 1.0, ) print(ytm(980, 1000, 0.07, 5)) # 0.074869 → 7.49% 

If the bond trades at ₹980, its YTM is about 7.49%. That's higher than the 7% coupon because you're buying below face value.

3. Duration

Macaulay duration is the weighted-average time until you receive the bond's cash flows. Modified duration turns that into a price sensitivity: the approximate % change in price for a 1% change in yield.

def macaulay_duration(face, coupon_rate, y, years, freq=2): c = face * coupon_rate / freq r = y / freq n = int(years * freq) price = bond_price(face, coupon_rate, y, years, freq) weighted = sum((t / freq) * (c / (1 + r) ** t) for t in range(1, n + 1)) weighted += (n / freq) * face / (1 + r) ** n return weighted / price def modified_duration(face, coupon_rate, y, years, freq=2): return macaulay_duration(face, coupon_rate, y, years, freq) / (1 + y / freq) 

Output for our bond:

Macaulay duration: 4.287 years Modified duration: 4.122 

Duration vs reality

Let's test the duration estimate against the real price change when yields rise by 1%:

p = bond_price(1000, 0.07, 0.08, 5) est = -modified_duration(1000, 0.07, 0.08, 5) * 0.01 * p actual = bond_price(1000, 0.07, 0.09, 5) - p 
Duration estimate +1%: INR -39.55 Actual change +1%: INR -38.57 

Duration overstates the loss by about ₹1. That gap is convexity: the price–yield curve bends, and duration only sees a straight line. It's exactly why the CFA curriculum adds a convexity adjustment for larger yield changes.

Why code it if the exam uses a calculator?

  • You see the mechanics. Price is just discounted cash flows; YTM is just the yield that makes them match the price.
  • You can test intuition fast. Change the coupon, maturity or frequency and watch duration move.
  • It's job-ready. Analysts in fixed income and risk teams do this in Python and Excel every day.

Try extending it: add a convexity function, price a zero-coupon bond, or build a yield curve from several bonds.

I teach fixed income and the rest of the CFA curriculum at RBei Classes. If you're preparing for CFA Level I, you'll find more resources at rbeiset.com.

Deepak Goyal, CFA, FRM — Founder, RBei Classes, Noida