Bond price = present value of future cash flows (coupons + principal). When rates rise, prices fall—the inverse relationship is fundamental to fixed income.
P = Σ(C/(1+r)ᵗ) + F/(1+r)ⁿ
function bondPrice(face, couponRate, years, marketYield, freq) {
const r = marketYield / freq;
const periods = years * freq;
const coupon = face * couponRate / freq;
let pv = 0;
// PV of coupon payments
for (let t = 1; t <= periods; t++) {
pv += coupon / Math.pow(1 + r, t);
}
// PV of face value at maturity
pv += face / Math.pow(1 + r, periods);
return pv;
}