From 9d88c4984f6599af8b97a4f550f333ed620b4af4 Mon Sep 17 00:00:00 2001 From: QuantIntellect <195442302+QuantIntellect@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:48:28 +0000 Subject: [PATCH] Value only the coupons the buyer is owed in Bond.asset_swap_spread asset_swap_spread valued the bond on the Ibor curve with every coupon paid after settlement. The bond price it compares with uses the accrued interest, which leaves out coupons the buyer doesn't get: - When the bond settles ex-dividend, the accrued interest is negative because the buyer doesn't get the next coupon. - When it settles on or after a coupon date whose payment rolls to a later business day, that coupon belongs to the seller and accrual has restarted. In both cases the spread included the coupon, and it jumped by about the coupon over the PV01 of the floating leg, for a gilt about 26 bp. Value the bond with the flows of dirty_price_from_discount_curve: the coupons the buyer is owed, without the next one when the bond settles ex-dividend. --- financepy/products/bonds/bond.py | 16 ++++++++++------ tests/unit/test_bond_z_spread.py | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/financepy/products/bonds/bond.py b/financepy/products/bonds/bond.py index a41f9d185..9dfc7b78b 100644 --- a/financepy/products/bonds/bond.py +++ b/financepy/products/bonds/bond.py @@ -1381,15 +1381,19 @@ def asset_swap_spread( accrued_amount = self.accrued_int * self.par bond_price = clean_price + accrued_amount - # Calculate the price of the bond discounted on the Ibor curve + # Calculate the price of the bond discounted on the Ibor curve, with + # the flows of dirty_price_from_discount_curve: the coupons the buyer + # is owed, without the next one when the bond settles ex-dividend pv_ibor = 0.0 prev_dt = self._pcd - for dt in self.payment_dts[1:]: - # coupons paid on a settlement date are paid to the seller - if dt > settle_dt: - df = discount_curve.df(dt) - pv_ibor += df * self.cpn / self.freq + for cpn_dt, pmt_dt in zip(self.cpn_dts[1:], self.payment_dts[1:]): + if not self._is_owed(cpn_dt, settle_dt): + continue + if cpn_dt == self._ncd and settle_dt > self.ex_div_dt: + continue + df = discount_curve.df(pmt_dt) + pv_ibor += df * self.cpn / self.freq pv_ibor += discount_curve.df(self.payment_dts[-1]) diff --git a/tests/unit/test_bond_z_spread.py b/tests/unit/test_bond_z_spread.py index d1ef8bb21..1629cebbe 100644 --- a/tests/unit/test_bond_z_spread.py +++ b/tests/unit/test_bond_z_spread.py @@ -1,6 +1,7 @@ import os import pandas as pd import matplotlib.pyplot as plt +import pytest from financepy.utils.global_vars import G_PERCENT from financepy.utils.calendar import CalendarTypes @@ -111,6 +112,38 @@ def _test_z_spread_for_curve(base_curve: DiscountCurve): ######################################################################################## +@pytest.mark.parametrize( + "settle_dt", + [ + Date(26, 8, 2026), # the last day with the 7 Sep 2026 coupon + Date(27, 8, 2026), # ex-dividend + Date(4, 9, 2026), # ex-dividend + Date(7, 9, 2026), # coupon date + Date(7, 3, 2026), # coupon date on a Saturday, paid on Monday 9 Mar + ], +) +def test_asset_swap_spread_is_zero_at_the_curve_price(settle_dt): + # A bond priced off the curve of the asset swap has a spread of zero, also when + # it settles ex-dividend. The coupons go ex seven UK business days before the + # coupon date, so the 7 Sep 2026 coupon goes ex on 26 Aug 2026. + bond = Bond( + Date(7, 3, 2020), + Date(7, 3, 2035), + 0.04, + FrequencyTypes.SEMI_ANNUAL, + DayCountTypes.ACT_ACT_ICMA, + ex_div_days=7, + cal_type=CalendarTypes.UNITED_KINGDOM, + ) + curve = FlatDiscountCurve(settle_dt, 0.04) + dirty_price = bond.dirty_price_from_discount_curve(settle_dt, curve) + clean_price = dirty_price - bond.accrued_interest(settle_dt) + + asw = bond.asset_swap_spread(settle_dt, clean_price, curve) + + assert asw == pytest.approx(0.0, abs=1e-12) + + ######################################################################################## if __name__ == "__main__":