"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(constraints): degrade to equal weights when a min_weight floor cannot be funded
Rudy Celekli4848e7a0eada
diff --git a/agent/backtest/constraints.py b/agent/backtest/constraints.pyindex e7a0e6ad4..53c25ed7d 100644--- a/agent/backtest/constraints.py+++ b/agent/backtest/constraints.py@@ -96,7 +96,10 @@ class MinWeight:need = float((self.floor - w[below]).sum())above = w > self.floor + _TOLavail = float((w[above] - self.floor).sum())- if avail <= _TOL:+ # Funding the lift needs more than the names above the floor can+ # give: the floor is infeasible for this book. Lifting anyway would+ # push a funder through zero, so degrade to equal weights.+ if avail <= _TOL or need > avail + _TOL:n = len(w)return np.full(n, w.sum() / n) if n else ww[below] = self.floordiff --git a/agent/tests/test_constraints.py b/agent/tests/test_constraints.pyindex 884703899..1f1f71474 100644--- a/agent/tests/test_constraints.py+++ b/agent/tests/test_constraints.py@@ -60,6 +60,13 @@ class TestMinWeight:w = MinWeight(0.4).apply(np.array([0.5, 0.3, 0.2]), ["A", "B", "C"])np.testing.assert_allclose(w, np.full(3, 1.0 / 3.0))+ def test_infeasible_floor_with_thin_funders_does_not_zero_a_name(self) -> None:+ # 3 names x 0.05 floor needs 0.15 but the book holds 0.10; the single+ # name above the floor can only give 0.01 toward the 0.06 lift.+ w = MinWeight(0.05).apply(np.array([0.02, 0.02, 0.06]), ["A", "B", "C"])+ np.testing.assert_allclose(w, np.full(3, 0.10 / 3.0))+ assert np.all(w > 0)+def test_zero_stays_zero(self) -> None:# handled at frame level, but the constraint itself must not invent weightw = MinWeight(0.2).apply(np.array([0.9, 0.1, 0.0]), ["A", "B", "C"])