A problem of calculating YTM of amortization fixed rate bonds in QuantLib Python
__I'm trying to calculate the ytm of bonds amortized in quantlib. The maturity of this bond is five years, starting from the second year to repay 25% of the face value until the last year.
The cash flow of bonds is correct. I want to calculate ytm based on the current net price, but I don't know why it is wrong.
I have the following bond:
Maturity Date:
24.07.2024
Coupon Frequency:
25 at 24.07.2021
25 at 24.07.2022
25 at 24.07.2023
25 at 24.07.2024
Day Count
Convention: ACT/365
Coupon rate: 6.5
Here is the python code
import QuantLib as ql
start_d = ql.Date(24, ql.July, 2019)
mat_d = ql.Date(24, ql.July, 2024)
calc_date = ql.Date(8, ql.December, 2022)
frequency = ql.Period(ql.Annual)
freq = ql.Annual
dayCounter = ql.Actual365Fixed(ql.ActualActual.Bond)
cleanprice = 45.83
ql.Settings.instance().evaluationDate = calc_date
schedule = ql.MakeSchedule(start_d, mat_d, frequency)
bond = ql.AmortizingFixedRateBond(0, [100, 100, 75, 50, 25, 0], schedule, [0.065], dayCounter)
for c in bond.cashflows():
print(f"{str(c.date()):20} => {c.amount():.4}")
ytm = bond.bondYield(float(cleanprice), dayCounter, ql.Compounded, freq)
print(f"{'YTM':20} => {round(ytm * 100, 8):.6}")
print(f"{'Accrued days':20} => {str(ql.BondFunctions.accruedDays(bond)):.4}")
print(f"{'Accrued amount':20} => {round(bond.accruedAmount(), 8):.4}")
The following is the printing of cash flow and ytm,I output accrued interest and interest days, so the accrued interest should be 138/365 * 6.5/2=1.2287, and the output result is 2.458. And the YTm finally calculated is also wrong, and the correct YTM should be 15.10%. I can't find the reason
July 24th, 2020 => 6.5
July 24th, 2021 => 6.5
July 24th, 2021 => 25.0
July 24th, 2022 => 4.875
July 24th, 2022 => 25.0
July 24th, 2023 => 3.25
July 24th, 2023 => 25.0
July 24th, 2024 => 1.625
July 24th, 2024 => 25.0
YTM => 125.183
Accrued days => 137
Accrued amount => 2.44
Thanks for posting! It might take a while before we look at your issue, so don't worry if there seems to be no feedback. We'll get to it.
Hi @bobinghua,
It seems that the issue with the accrued amount is due to the fact that the original function that calculates this amount BondFunctions::accruedAmount does the following thing:
return CashFlows::accruedAmount(bond.cashflows(),
false, settlement) *
100.0 / bond.notional(settlement);
This function is in bondfunctions.cpp
Since the bond.notional() at the calculation date is 50, the accrued amount returned is twice as much thus giving the wrong number.
The issue with the YTM seems to be also related to the notional function. In order to calculate the YTM the bond calls the BondFunctions::Yield which calls another function called Yield defined in bondfunctions.hpp . This function also uses the bond.notional() method for adjusting the dirty price as well as the incorrect accrued amount:
`
if (priceType == Bond::Price::Clean)
dirtyPrice += bond.accruedAmount(settlementDate);
dirtyPrice /= 100.0 / bond.notional(settlementDate);
return CashFlows::yield<Solver>(solver, bond.cashflows(),
dirtyPrice, dayCounter, compounding,
frequency, false, settlementDate,
settlementDate, accuracy, guess);`
A solution would be to override the bond::notional() method in amortizing bonds and return the original notional so that the cashflows are not doubled. I also noticed that there were no tests for this class (at least in the bonds tests).
Let's see what more experienced developers like @lballabio say about this possible solution. If they are Ok I can work on that and creating new tests for it.
Since the bond.notional() at the calculation date is 50, the accrued amount returned is twice as much thus giving the wrong number.
That's on purpose. The usual convention is to rescale the price so that is quotes around 100, even when the notional has decreased. The accrued amount is rescaled this way to be consistent with the quoted price.
Okay, then when the intention is to calculate the real accrued quantity (in terms of the real coupon to be received/paid) one would do:
real_accrued_amount = bond.accruedAmount() * bond.notional(calc_date)/100
The problem with the YTM would be adjusting then the clean price to be expressed in terms of a notional of a 100, so we could do the following workaround?
ytm = bond.bondYield(float(cleanprice)*bond.notional(calc_date)/100, dayCounter, ql.Compounded, freq, calc_date)
The solution that I mentioned above solve the issues mentioned by @bobinghua delivering the same accrued and ytm. @lballabio I think if @bobinghua does not say anything else we could close the issue with this workaround.
I would have expected float(cleanprice)*100/bond.notional(calc_date), since we want to scale a price around 45 to be in base 100.
Hi @lballabio you are right, I did the mistake when pasting the code. I have checked again and the solution you propose it delivers around 15 % YTM which we were expecting.
This issue was automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment, or this will be closed in two weeks.