Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(quantlib): accrued_interest rejects an unknown day count on coupon dates (#1509)

Haozhe Wu7b73da56f4b2
diff --git a/agent/src/quantlib/fixedincome.py b/agent/src/quantlib/fixedincome.py
index 3af99b504..94e2a730f 100644
--- a/agent/src/quantlib/fixedincome.py
+++ b/agent/src/quantlib/fixedincome.py
@@ -159,7 +159,8 @@ def accrued_interest(
Raises:
ValueError: If ``freq`` is not positive, if the coupon dates are not
- ordered, or if ``settlement`` falls outside the coupon period.
+ ordered, if ``settlement`` falls outside the coupon period, or if
+ ``day_count`` is not a recognised convention.
"""
if freq <= 0:
raise ValueError(f"freq must be positive, got {freq}")
@@ -167,6 +168,12 @@ def accrued_interest(
raise ValueError("last_coupon must precede next_coupon")
if not last_coupon <= settlement <= next_coupon:
raise ValueError("settlement must fall inside [last_coupon, next_coupon]")
+ # Checked here as well as in year_fraction: the endpoint returns below never
+ # reach year_fraction, so an unknown convention would otherwise pass there.
+ if day_count not in DAY_COUNT_CONVENTIONS:
+ raise ValueError(
+ f"unknown day_count {day_count!r}; expected one of {DAY_COUNT_CONVENTIONS}"
+ )
# 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 <
diff --git a/agent/tests/quantlib/test_fixedincome.py b/agent/tests/quantlib/test_fixedincome.py
index 0be53c431..79254a45f 100644
--- a/agent/tests/quantlib/test_fixedincome.py
+++ b/agent/tests/quantlib/test_fixedincome.py
@@ -338,6 +338,15 @@ def test_accrued_interest_rejects_settlement_outside_the_period():
accrued_interest(100, 0.05, 2, last, dt.date(2024, 8, 1), nxt)
[email protected]("settlement", [dt.date(2024, 1, 15), dt.date(2024, 4, 1), dt.date(2024, 7, 15)])
+def test_accrued_interest_rejects_an_unknown_day_count_at_every_settlement(settlement):
+ # The coupon-date shortcuts return before year_fraction; they must not
+ # accept a convention year_fraction would reject mid-period (#1509).
+ last, nxt = dt.date(2024, 1, 15), dt.date(2024, 7, 15)
+ with pytest.raises(ValueError, match="unknown day_count"):
+ accrued_interest(100, 0.05, 2, last, settlement, nxt, day_count="bogus")
+
+
def test_accrued_interest_rejects_unordered_coupon_dates():
with pytest.raises(ValueError, match="last_coupon must precede next_coupon"):
accrued_interest(