QuantLib Bond Yield: Coupon, Price Convention, and Valuation Date
Summary
This discussion examines a negative yield returned by QuantLib for a fixed-rate bond when the user expected a small positive yield. The replies identify several input and convention issues to check: the coupon is divided by 100 after already being described as 2.75, the supplied price may be a dirty price even though the yield method expects a clean price, and the evaluation date affects accrued interest and the resulting yield calculation.
A quick approximation using the stated cash flow and yield shows why a yield near −12% can be consistent with the provided inputs. Another reply suggests setting the evaluation date to the bond’s start date to align the calculation with other engines. The discussion is a troubleshooting example rather than a complete treatment of bond yield conventions; it does not establish which price convention or evaluation date is appropriate for every use case.
Key ideas
- Check whether the coupon rate is scaled correctly before constructing the bond.
- Confirm whether the price supplied to the yield calculation is clean or dirty.
- The evaluation date affects accrued interest and can change the calculated yield.
- A negative yield may be consistent with the inputs even when total undiscounted cash flows exceed the quoted price.
Tags
Full text
# Problem with bond.bondYield Quantlib
# Problem with bond.bondYield Quantlib
I'm having issues with a simple FixedRateBond bond yield calculation using QuantLib:
```
import QuantLib as ql
start = ql.Date(9,9,2020)
end = ql.Date(23,7,2021)
period = ql.Period(int(2))
coupon = 2.75
face = 100
ql.Settings.instance().evaluationDate = ql.Date(31,5,2021)
given = 102.23
schedule = ql.Schedule(start, end,
period, ql.UnitedStates(),
ql.Unadjusted, ql.Unadjusted,
ql.DateGeneration.Backward, True)
day_counter = ql.Thirty360()
bond = ql.FixedRateBond(1, face, schedule, [coupon/100], day_counter)
bond_price = bond.bondYield(given, day_counter, ql.Compounded, 2)
```
The code above returns -0.12041547246813772 as the yield of the bond. I know the yield to be around 0.00190424005054490 since i'm replicating an excercise previously done on matlab.
The program seems to be failing since, when i call ` bond.cashflows()` i get three cashflows that add to about 102.391304, which is higher that the given price and therefore discount factors should be less than 1, and the yield should not be negative.
Thanks in advance for your help.
## Answer by David Duarte (score 2)
https://quant.stackexchange.com/a/65562
First, I don't think the coupon is correct. You have `coupon = 0.0275` but then you input the parameter as `[coupon/100]`. That would be 2.75 bps.
Also, could 102.23 be the dirty price? QuantLib expects the clean price in the `.bondYield` method...
In any case, the yield of -12% looks about right with those inputs, because as a quick approximation, the price would be:
`(100 + 2.75 * 53/360) / (1+ (-0.12)*53/360) = 102.21`
## Answer by Elvis Espinal (score 1)
https://quant.stackexchange.com/a/65565
My mistake was with the `ql.Settings.instance().evaluationDate = ql.Date(31,5,2021)` line, it should be set as `ql.Settings.instance().evaluationDate = start` to obtain a yield calculation similar to other engines.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.