Skip to content

Bond pricer and YTM calculator

Price a bond from its yield or solve for the yield from a price, with real settlement and maturity dates.

How it works

A bond's dirty price is the present value of its remaining cash flows. Between coupon dates the first cash flow is a fraction ww of a period away, so each flow is discounted over k−1+wk - 1 + w periods (the street convention). The quoted clean price is the dirty price minus accrued interest, which is the part of the next coupon the seller has already earned.

To go from price to yield there is no closed form, so the tool solves the pricing equation numerically by bisection, which always converges for a normal bond.

Pdirty=∑k=1nCFk(1+y/f) k−1+w,Pclean=Pdirty−AIP_{\text{dirty}} = \sum_{k=1}^{n} \frac{CF_k}{(1 + y/f)^{\,k-1+w}}, \qquad P_{\text{clean}} = P_{\text{dirty}} - AIAI=c⋅Ff×days since last coupondays in period(ACT/ACT)AI = \frac{c \cdot F}{f} \times \frac{\text{days since last coupon}}{\text{days in period}} \quad (\text{ACT/ACT})

Assumptions

  • Coupon dates roll back from maturity by whole periods, keeping the maturity's day of month (clamped at month end).
  • Yield is compounded at the coupon frequency.
  • ACT/ACT is the ICMA convention (actual days over actual days in the period). 30/360 is the US bond basis. ACT/360 and ACT/365 accrue on a fixed-year basis.
  • No business-day adjustment, ex-dividend period or odd first/last coupon.
Show the codePython, runs in the playground
bond-price-ytm-calculator.py
from datetime import date

def months_back(d, m):
    y, mo = divmod(d.month - 1 - m, 12)
    import calendar
    year, month = d.year + y, mo + 1
    return date(year, month, min(d.day, calendar.monthrange(year, month)[1]))

def bond_price(settle, maturity, coupon, ytm, freq=2, face=100):
    step = 12 // freq
    coupons, k = [maturity], 1
    while True:
        d = months_back(maturity, step * k)
        if d <= settle:
            prev = d
            break
        coupons.append(d); k += 1
    coupons.reverse()
    nxt = coupons[0]
    period = (nxt - prev).days
    w = (nxt - settle).days / period                       # ACT/ACT
    cpn = face * coupon / freq
    accrued = cpn * (settle - prev).days / period
    dirty = sum((cpn + (face if i == len(coupons) - 1 else 0)) / (1 + ytm / freq) ** (i + w)
                for i in range(len(coupons)))
    return dirty - accrued, dirty, accrued

clean, dirty, accrued = bond_price(date(2026, 11, 16), date(2034, 5, 15), coupon=0.0425, ytm=0.045)
print(f"Clean price: {clean:.6f}")
print(f"Dirty price: {dirty:.6f}")
print(f"Accrued interest: {accrued:.9f}")

Questions

What is the difference between clean and dirty price?

The dirty (full) price is what you pay. The clean price strips out accrued interest so that quoted prices do not jump on coupon dates. Dirty equals clean plus accrued.

Which day count should I use?

It depends on the market: US Treasuries use ACT/ACT, US corporates and agencies 30/360, many money-market instruments ACT/360, and UK gilts ACT/ACT. Check the bond's prospectus.

Why does a bond trade above 100?

When its coupon rate is above the market yield, the extra coupons are worth something, so the price is above par. The reverse gives a discount bond.

How is the yield solved?

By bisection on the clean price between a yield of almost minus 100% and 1,000%. Price falls as yield rises, so there is exactly one answer.

Educational use only, not investment advice. Results depend on the model's assumptions listed above and on your inputs. Calculations run in your browser and nothing you enter is sent to a server.