Skip to content
All library documents

Matching QuantLib Bond Yields by Setting the Settlement Date

Article Quant Q&A · Author: russell_i

Summary

The document explains why a bond yield calculated in QuantLib may differ from a spreadsheet result when the settlement date is omitted. In that case, the calculation uses QuantLib’s global evaluation date, which defaults to the system date. The proposed fix is to pass the intended settlement date to the yield calculation or set the global evaluation date before calculating.

The example concerns a fixed-rate bond and compares a Python result with a spreadsheet yield, but the text does not establish that the result generalizes to every bond setup. It also notes a constructor change in newer QuantLib versions: a schedule is required when creating the bond. Matching results still depends on using compatible conventions and inputs, including day count, compounding, frequency, and settlement assumptions.

Key ideas

  • An omitted settlement date can cause QuantLib to use its global evaluation date.
  • Pass the intended settlement date to the yield calculation or set the global evaluation date explicitly.
  • Bond conventions and input assumptions must align when comparing library and spreadsheet yields.
  • Newer QuantLib versions require a schedule in the fixed-rate bond constructor.

Tags

Full text
# QuantLib Bond Yield


# QuantLib Bond Yield












I think I have the same question as was asked here but I still haven't been able to resolve my issue:

Excel YIELD function equivalent in python Quantlib

I am trying to calculate the yield on a bond and match it to the results I am getting in Excel/MatLab.

In Excel and MatLab I can get the same results but I need to implement in Python. (0.75358%)

```
=YIELD("16/03/2020","21/11/2029",0.0275,118.607,100,2,3)
```

=YIELD(settlement,maturity,rate,pr,redemption,frequency,basis)

So I know I need to create a ql.FixedRateBond and then use the bondYield function.

I think frequency above is matched by ql.Seminannual and basis is my dayCount convention of ql.Actual365Fixed().

But I must be going wrong with some of the additional Python parameters because my Python answer is wayyy off.

Can someone help me with where I might be going wrong in my Python specification below?

```
settlement= ql.Date(16,3,2020)
maturity= ql.Date(21,11,2029)
bond = ql.FixedRateBond(0, ql.TARGET(), 100, start, maturity, ql.Period('6M'), [0.0275], ql.Actual365Fixed())
bond.bondYield(118.6070, ql.Actual365Fixed(), ql.Compounded, ql.Semiannual)
```

(Python results 0.6284% btw)

## Answer by David Duarte (score 4, accepted)

https://quant.stackexchange.com/a/59317

The issue here is that when you call the `bondYield` method, if you don't specify a settlement date, QuantLib will calculate the discount factors based on the global evaluation date. By default that will be the system date.

So either define the settlement date in the method, as the parameter after the frequency:

```
start = ql.Date(16,3,2020)
maturity = ql.Date(21,11,2029)
bond = ql.FixedRateBond(2, ql.TARGET(), 100, start, maturity, ql.Period('6M'), [0.0275], ql.Actual365Fixed())
bond.bondYield(118.607, ql.Actual365Fixed(), ql.Compounded, ql.Semiannual, start)
```

Or, change the global evaluation date. For example, you could insert this line before you run the `bondYield` method.

```
ql.Settings.instance().evaluationDate = ql.Date(16,3,2020)
```

Update for QuantLib >= 1.36:

`ql.FixedRateBond` constructor now needs a schedule to be passed, so the previous example would become:

```
schedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))
bond = ql.FixedRateBond(2, 100.0, schedule, [0.0275], ql.Actual365Fixed())
bond.bondYield(118.607, ql.Actual365Fixed(), ql.Compounded, ql.Semiannual, start)
```

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.