# 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)

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)

• Hi David, thank you so much for your prompt response. I misunderstood the settlement parameter. This answer has resolved my question! Nov 12, 2020 at 9:27
• @russell_i You can accept the answer if it solved your problem. Nov 12, 2020 at 14:32