"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): fix accrued interest on a degenerate 30/360 period
Lanre Shittu0b951959dbf6
diff --git a/agent/src/quantlib/fixedincome.py b/agent/src/quantlib/fixedincome.pyindex f3dbd0bce..3af99b504 100644--- a/agent/src/quantlib/fixedincome.py+++ b/agent/src/quantlib/fixedincome.py@@ -168,6 +168,16 @@ def accrued_interest(if not last_coupon <= settlement <= next_coupon:raise ValueError("settlement must fall inside [last_coupon, next_coupon]")+ # 30/360 and 30E/360 clip the day-of-month, so distinct calendar dates+ # (e.g. Jan 30 -> Jan 31) can still year_fraction to 0.0. last_coupon <+ # next_coupon is already guaranteed above, so that can never mean a+ # true zero-length period -- checking settlement's own position against+ # the coupon dates keeps the endpoints correct regardless.+ if settlement == last_coupon:+ return 0.0+ if settlement == next_coupon:+ return face * coupon_rate / freq+period = year_fraction(last_coupon, next_coupon, day_count)if period == 0.0:return 0.0diff --git a/agent/tests/quantlib/test_fixedincome.py b/agent/tests/quantlib/test_fixedincome.pyindex 4edd96710..0be53c431 100644--- a/agent/tests/quantlib/test_fixedincome.py+++ b/agent/tests/quantlib/test_fixedincome.py@@ -322,6 +322,16 @@ def test_accrued_interest_is_linear_in_elapsed_time_under_30_360():) == pytest.approx(1.25)+def test_accrued_interest_full_at_next_coupon_despite_30_360_day_of_month_clipping():+ # Jan 30 -> Jan 31 clips to the same 30/360 day-of-month, so+ # year_fraction is 0.0 even though the dates are distinct and ordered.+ # Settlement at next_coupon must still return the full period coupon.+ last, nxt = dt.date(2024, 1, 30), dt.date(2024, 1, 31)+ assert year_fraction(last, nxt, "30/360") == 0.0+ assert accrued_interest(100, 0.06, 2, last, nxt, nxt, day_count="30/360") == pytest.approx(3.0)+ assert accrued_interest(100, 0.06, 2, last, last, nxt, day_count="30/360") == pytest.approx(0.0)++def test_accrued_interest_rejects_settlement_outside_the_period():last, nxt = dt.date(2024, 1, 15), dt.date(2024, 7, 15)with pytest.raises(ValueError, match="settlement must fall inside"):