QuantLib Bond Accrued Interest and Transaction Dates
Summary
The document asks how to price a fixed-rate bond in QuantLib when the holder’s purchase date predates the most recent coupon date. It describes a semiannual bond valued between coupon dates and shows that QuantLib reports accrued interest based on the current accrual period, even though the investor bought the bond earlier. The code also prints accrual dates and accrued days to investigate the result.
The central issue is the distinction between a bond’s accrued interest at valuation and the interest accrued over a particular investor’s holding period. The question asks whether the transaction date can be incorporated into the bond engine so accrued interest becomes zero. It does not include an answer or establish that zero is the correct measure for standard bond pricing; accrued interest is generally tied to the coupon period and settlement conventions, not an individual purchase history. The example therefore illustrates a modeling question, not a resolved QuantLib technique.
Key ideas
- QuantLib calculates accrued interest from the bond’s coupon schedule and valuation or settlement date.
- A holder’s transaction date does not automatically change the bond’s standard accrued-interest calculation.
- The example uses a semiannual fixed coupon and values the bond between coupon dates.
- Distinguish coupon-period accrued interest from interest accumulated since an investor purchased the bond.
Tags
Full text
# Incorporate Transaction Date to Price Fixed Rate Bond using Quantlib Python
# Incorporate Transaction Date to Price Fixed Rate Bond using Quantlib Python
I am trying to use QuantLib Python to price a fixed rate bond, based on the following data:
Issue date is 28 September 2017 (`issueDate`), maturity date is 28 September 2024 (`maturityDate`), the bond is being valued as at 31 July 2020 (`valuationDate`), and coupons of 5% of face value of $1000 are payable on a semiannual basis (`couponFrequency`). However, the bond has been purchased on 05 May 2019 (`transactionDate`).
From my understanding, there should be no accrued interest as at the valuation date, since the bond has been purchased prior to the last coupon payment date before the valuation date, which is 28 March 2020.
However, QuantLib is still returning a figure for accrued interest, as if the bond has been purchased between the last coupon payment date before the valuation date and the valuation date.
Please see my codes below:
```
import QuantLib as ql
valuationDate = ql.Date(31, 7, 2020)
ql.Settings.instance().evaluationDate = valuationDate
compounding = ql.Compounded
calendar = ql.NullCalendar()
coupon = 0.05
couponFrequency = ql.Semiannual
issueDate = ql.Date(28, 9, 2017)
maturityDate = ql.Date(28, 9, 2024)
businessConvention = ql.Unadjusted
transactionDate = ql.Date(5, 5, 2019)
settlementDays = 0
schedule = ql.Schedule(issueDate, maturityDate, ql.Period(couponFrequency), calendar, businessConvention, businessConvention, ql.DateGeneration.Backward, True)
dayCount = ql.ActualActual(ql.ActualActual.ISMA, schedule)
faceValue = 100
redemptionValue = 100
fixedRateBond = ql.FixedRateBond(settlementDays, faceValue, schedule, [coupon], dayCount, businessConvention, redemptionValue)
curve = ql.ZeroCurve([ql.Date(31, 7, 2020), ql.Date(1, 1, 2027)], [0.01, 0.02], ql.ActualActual(), calendar, ql.Linear(), compounding, ql.Annual)
handle = ql.YieldTermStructureHandle(curve)
bondEngine = ql.DiscountingBondEngine(handle)
fixedRateBond.setPricingEngine(bondEngine)
print('Accrual start date:', ql.BondFunctions.accrualStartDate(fixedRateBond))
print('Accrual end date:', ql.BondFunctions.accrualStartDate(fixedRateBond))
print('Accrued days:', ql.BondFunctions.accruedDays(fixedRateBond))
print('Number of days between accrual start date and accrual end date:', ql.ActualActual().dayCount(ql.BondFunctions.accrualStartDate(fixedRateBond), ql.BondFunctions.accrualEndDate(fixedRateBond)))
print("QuantLib's accrued interest:", fixedRateBond.accruedAmount())
```
The results are as follows:
> Accrual start date: March 28th, 2020
> Accrual end date: March 28th, 2020
> Accrued days: 125
> Number of days between accrual start date and accrual end date: 184
> QuantLib's accrued interest: 1.6983695652173836
I understand that the computation has been done by QuantLib using the following formula:
However, the accrued interest should be 0 since the transaction date is prior to 28 March 2020.
Is there a way to incorporate the transaction date in QuantLib's `FixedRateBond` engine, without using an `if` condition, to return an accrued interest of 0 in this case, relative to the valuation date?
Any help is most welcomed, thanks!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.