From 020f22f0bcf25ae602bb22faceafb8257ce1c597 Mon Sep 17 00:00:00 2001 From: QuantIntellect <195442302+QuantIntellect@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:22:37 +0000 Subject: [PATCH] Choose the coupons in BondFuture.implied_repo_rate the way Bond.forward_price does implied_repo_rate counted the coupons dated after settlement and on or before delivery, by coupon date. The prices it starts from use the ex-dividend rule: a bond settled ex-dividend has negative accrued interest and its buyer doesn't get the next coupon, and a bond delivered ex-dividend keeps the next coupon for the seller. So with ex-dividend days the implied repo rate jumped by about the coupon over the holding period when settlement or delivery moved into the ex-dividend window, and it no longer inverted forward_price, which uses Bond._cpns_held since the coupon fix there. Use Bond._cpns_held for the coupons and weight each from its payment date, as forward_price does. Bonds without ex-dividend days and with coupons paid on their coupon dates give the same results as before. --- financepy/products/bonds/bond_future.py | 13 +++++--- tests/unit/test_bond_futures.py | 44 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/financepy/products/bonds/bond_future.py b/financepy/products/bonds/bond_future.py index 6aa7e35f..53f8ba7e 100644 --- a/financepy/products/bonds/bond_future.py +++ b/financepy/products/bonds/bond_future.py @@ -332,13 +332,16 @@ def implied_repo_rate( ai_delivery = bond.accrued_interest(delivery_dt) full_price = clean_price + ai_settle + # The coupons the holder gets between settlement and delivery, chosen + # as in Bond.forward_price. A coupon kept in an ex-dividend period at + # delivery is paid after delivery, so its weight below is negative. fv_cpns = 0.0 weighted_days_sum = 0.0 - for dt, amt in zip(bond.cpn_dts[1:], bond.flow_amounts[1:]): - if settle_dt < dt <= delivery_dt: - fv_cpns += amt * bond.par - # Coupons reduce financing from receipt until delivery. - weighted_days_sum += (delivery_dt - dt) * amt * bond.par + for pmt_dt, flow in bond._cpns_held(settle_dt, delivery_dt): + amt = flow * bond.par + fv_cpns += amt + # Coupons reduce financing from receipt until delivery. + weighted_days_sum += (delivery_dt - pmt_dt) * amt if fv_cpns > 0: avg_coupon_days = weighted_days_sum / fv_cpns diff --git a/tests/unit/test_bond_futures.py b/tests/unit/test_bond_futures.py index fdb40da1..f1595e69 100644 --- a/tests/unit/test_bond_futures.py +++ b/tests/unit/test_bond_futures.py @@ -255,3 +255,47 @@ def test_implied_repo_balances_coupon_reinvestment_cashflows(repo_bond, repo_rat implied = future.implied_repo_rate(repo_bond, settle_dt, clean_price, futures_price) assert implied == pytest.approx(repo_rate, abs=1e-12, rel=0.0) + + +@pytest.fixture +def ex_div_bond(): + # Like a gilt: coupons go ex-dividend seven UK business days before the + # coupon date. The 7 Sep 2026 coupon goes ex on 26 Aug 2026, and the + # 7 Mar 2027 coupon falls on a Sunday and is paid on 8 Mar 2027. + return 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, + ) + + +@pytest.mark.parametrize("repo_rate", [0.01, 0.05]) +@pytest.mark.parametrize( + "settle_dt,delivery_dt", + [ + (Date(27, 8, 2026), Date(29, 9, 2026)), # settles ex-dividend + (Date(1, 6, 2026), Date(27, 8, 2026)), # delivered ex-dividend + (Date(20, 8, 2026), Date(1, 9, 2026)), # coupon paid after delivery + (Date(27, 8, 2026), Date(4, 9, 2026)), # ex-dividend at both dates + (Date(1, 2, 2027), Date(15, 3, 2027)), # coupon paid after its date + ], +) +def test_implied_repo_inverts_forward_price_around_ex_dividend_dates( + ex_div_bond, settle_dt, delivery_dt, repo_rate +): + future = BondFuture("TEST", delivery_dt, delivery_dt, 100_000, 0.04) + clean_price = 99.0 + forward_price = ex_div_bond.forward_price( + settle_dt, delivery_dt, clean_price, repo_rate + ) + futures_price = forward_price / future.conversion_factor(ex_div_bond) + + implied = future.implied_repo_rate( + ex_div_bond, settle_dt, clean_price, futures_price + ) + + assert implied == pytest.approx(repo_rate, abs=1e-12, rel=0.0)