Skip to content

"Vibe-Trading: Your Personal Trading Agent"

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.py
index 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 + _TOL
avail = 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 w
w[below] = self.floor
diff --git a/agent/tests/test_constraints.py b/agent/tests/test_constraints.py
index 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 weight
w = MinWeight(0.2).apply(np.array([0.9, 0.1, 0.0]), ["A", "B", "C"])