diff --git a/financepy/products/bonds/bond.py b/financepy/products/bonds/bond.py index a41f9d18..9dfc7b78 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 d1ef8bb2..1629cebb 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__":