Skip to content
All library documents

Calculating Fixed-Rate Bond Price and Yield with QuantLib

Article Quant Q&A · Author: Jose Andres Riveros

Summary

The document shows how to use QuantLib to calculate a fixed-rate bond’s yield from its clean price, then calculate the clean price from that yield. The example specifies maturity, issue date, coupon, payment frequency, day-count convention, face value, and annual compounding. It also demonstrates setting the evaluation date for a bond valued in the past.

For the stated example, the reported yield is about 2.49%, and converting it back produces a clean price close to 104.5. These results illustrate the two-way calculation, while the historical example shows that valuation date can matter. The examples are specific to their chosen bond conventions and QuantLib setup; users need to match the day-count, settlement, calendar, compounding, and evaluation-date assumptions to the instrument they are pricing. The discussion does not compare alternative conventions or explain how to handle every bond type.

Key ideas

  • QuantLib can derive a fixed-rate bond’s yield from its clean price and specified conventions.
  • The clean price can be recalculated from the resulting yield as a consistency check.
  • Bond inputs include issue and maturity dates, coupon, payment frequency, and day-count convention.
  • Set the evaluation date when valuing a bond at a historical date.
  • Results depend on the pricing conventions and setup supplied to QuantLib.

Tags

Full text
# How to get the price of a bond if the yield is given or viceversa in QuantLib


# How to get the price of a bond if the yield is given or viceversa in QuantLib












For example

Can u provide with a detailed example please if i have ( maturity, issue date, coupon, frequency, days_countbase, (price or yield) what is the (yield or price given this information.

For example if i have

maturity = 30/january/2030 coupon 3% issue date = 30/januery/2019 (not sure if needed in quantlib) frequency = semiannual days_count base= isma-30/360 and a price= assuming quantlib uses cleanprice = 104.5

what woul be the yield to maturity of that given bond

or the other option same information but instead of price i have a yield of 2.45% what would be the clean_price?

apprecciate your help, and if u could provide me a code example i woul very much apprecciate it, i have seen examples and your youtubes videos, and it seems very simple my question but im having such a bada time trying to use quntlib correctly

Thx

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

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

To get the bond yield from the price:

```
import QuantLib as ql
maturity = ql.Date(30, 1, 2030)
coupon = 0.03
issueDate = ql.Date(30, 1, 2019)
frequency = ql.Semiannual
dayCount = ql.Thirty360()
price = 104.5

bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, issueDate, maturity, ql.Period(frequency), [coupon], dayCount)
yld = bond.bondYield(price, dayCount, ql.Compounded, ql.Annual)
print(yld)
```

0.02487635655403138

Then to do the opposite:

```
cleanPrice = bond.cleanPrice(yld, dayCount, ql.Compounded, ql.Annual)
print(cleanPrice)
```

104.50000186708574

To price the bond from your picture which is a valuation in the past:

```
ql.Settings.instance().evaluationDate = ql.Date(21,2,2012)
maturity = ql.Date(29, 4, 2016)
coupon = 0.038
issueDate = ql.Date(29, 4, 2006)
frequency = ql.Annual
dayCount = ql.Thirty360()
price = 98.847

bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, issueDate, maturity, ql.Period(frequency), [coupon], dayCount)
yld = bond.bondYield(price, dayCount, ql.Compounded, ql.Annual)
print(yld)
```

0.04102500295639039

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.