Calculating Fixed-Rate Bond Prices and Yields with QLNet
Summary
The document describes a QLNet workflow for converting a fixed-rate bond’s observed price into a yield, then using that yield to calculate clean and dirty prices. It constructs a coupon schedule and bond, sets an evaluation date relative to settlement, and calls the bond’s yield calculation with a day-count convention, compounding method, and payment frequency. It then creates a flat discount curve from the yield and attaches a discounting pricing engine.
The author reports that calculated yields sometimes differ from Bloomberg by one or two basis points, prices are harder to match, and results diverge when redemption differs from face value. The post does not give a diagnosis or confirmed fix, so it mainly serves as an example of the inputs and conventions involved. Matching external quotes may depend on details such as settlement timing, accrued interest, day count, schedule conventions, and redemption assumptions; the supplied example alone does not establish which setting explains the discrepancies.
Key ideas
- Bond yield calculations depend on settlement date, day-count convention, compounding, and payment frequency.
- A bond schedule and its coupon and redemption terms are inputs to QLNet valuation.
- A flat yield curve and discounting engine can be used to calculate clean and dirty prices.
- The example reports mismatches with Bloomberg but does not identify their cause.
Tags
Full text
# Calculating price -> yield and yield -> price for fixed rate bonds using QLNet
# Calculating price -> yield and yield -> price for fixed rate bonds using QLNet
I'm trying to implement a simple calculation module to calculate fixed rate bond yield given the price (and then price based on yield to verify the calculation). I've looked at a number of examples (including Bonds example) but in some cases I'm seeing difference from the numbers future users currently retrieve from Bloomberg. Here's my code (BondData class defines bond attributes that I load from a `.csv` file). In most examples I'm matching yield numbers (in some cases I'm off by 1-2bps) but having hard time matching price. Also, yields don't match when redemption value is different from face value. Is there something obvious that I'm missing?
Thank you,
Jim
```
int fixingDays = 1;
Date todaysDate = calendar.advance(bond.SettlementDate, -fixingDays, TimeUnit.Days);
Settings.setEvaluationDate(todaysDate);
// Rate
Schedule fixedBondSchedule = new Schedule(
bond.IssueDate,
bond.Maturity,
new Period(bond.Frequency),
new UnitedStates(UnitedStates.Market.GovernmentBond),
BusinessDayConvention.ModifiedFollowing,
BusinessDayConvention.Unadjusted,
DateGeneration.Rule.Backward,
false);
FixedRateBond fixedRateBond = new FixedRateBond(
1,
bond.FaceAmount,
fixedBondSchedule,
new List<double>() { bond.CouponRate }, // { 1, couponRate }
bond.DayCounter,
BusinessDayConvention.ModifiedFollowing,
bond.Redemption,
bond.IssueDate);
bond.YieldCalc = fixedRateBond.yield(
bond.Price,
bond.DayCounter,
Compounding.Compounded,
bond.Frequency,
bond.SettlementDate);
RelinkableHandle<FlatTermStructure> discountingTermStructure = new RelinkableHandle<FlatTermStructure>();
Handle<FlatTermStructure> flatTermStructure = new Handle<FlatTermStructure>(
new FlatForward(
bond.SettlementDate,
bond.Yield,
bond.DayCounter,
Compounding.Compounded,
bond.Frequency));
discountingTermStructure.linkTo(flatTermStructure);
// Pricing engine
IPricingEngine bondEngine = new DiscountingBondEngine(discountingTermStructure);
fixedRateBond.setPricingEngine(bondEngine);
bond.CleanPriceCalc = fixedRateBond.cleanPrice();
bond.DirtyPriceCalc = fixedRateBond.dirtyPrice();
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.