# 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

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

• Thx David for ur help, just one more question i forgot, if i want to do it in a specific settlement date, lets say today? how could it be done? – Jose Andres Riveros Jun 4 '20 at 23:27
• The evaluation date is set with ql.Settings.instance().evaluationDate. If you don't change it, it will be today, but you can set a different date. I updated the answer to also price the bond on your picture (not sure all the conventions are correct...) – David Duarte Jun 4 '20 at 23:38
• David, don´t have words for all your help, this helped me a lot, just wondering when in the code u add 2 and 100 for default, what are those conventions and in the other hand i see that inside this code: bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, issueDate, maturity, ql.Period(frequency), [coupon], dayCount) there is no reference to the evaluation date, so it doesnt need it, or is the ql.TARGET() command the one that is doing the job. im willing for some classes David for quantlib, if we can arrange something i would apprecciate it – Jose Andres Riveros Jun 4 '20 at 23:52
• In the FixedRateBond constructor, 2 would be the settlement days and 100 would be the face value. There is no evaluation date on the constructor because it is set in the global reference date in ql.Settings.instance().evaluation date. You can change it's value and recompute your yield or price, and you will see they change accordingly. – David Duarte Jun 4 '20 at 23:59
• i promise this would be the last question, im trying to create a generic calculator similar to the one from bloomberg and the last diffculty im encountering is the day count conventions: 3 30/360 methods 3.1 30/360 Bond Basis 3.2 30/360 US 3.3 30E/360 3.4 30E/360 ISDA 4 Actual methods 4.1 Actual/Actual ICMA 4.2 Actual/Actual ISDA 4.3 Actual/365 Fixed 4.4 Actual/360 4.5 Actual/364 4.6 Actual/365L 4.7 Actual/Actual AFB what are the reciprocals for quantlib, i imagine the don have them all, but just to have an idea do add a search and try to identify which one i should use – Jose Andres Riveros Jun 5 '20 at 0:08