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)