Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): validate validation-config knobs instead of crashing

Santh2c72ff8f3189
diff --git a/agent/backtest/validation.py b/agent/backtest/validation.py
index 95fdb7fd1..33a69b3ba 100644
--- a/agent/backtest/validation.py
+++ b/agent/backtest/validation.py
@@ -46,6 +46,13 @@ def monte_carlo_test(
Dict with actual_sharpe, p_value_sharpe, actual_max_dd,
p_value_max_dd, simulated_sharpes (percentiles).
"""
+ if n_simulations < 1:
+ return {
+ "error": f"n_simulations must be >= 1, got {n_simulations}",
+ "p_value_sharpe": 1.0,
+ }
+ if seed < 0:
+ return {"error": f"seed must be >= 0, got {seed}", "p_value_sharpe": 1.0}
if len(trades) < 3:
return {"error": "need at least 3 trades", "p_value_sharpe": 1.0}
@@ -116,6 +123,13 @@ def bootstrap_sharpe_ci(
Dict with observed_sharpe, ci_lower, ci_upper, median_sharpe,
prob_positive (fraction of samples with Sharpe > 0).
"""
+ if n_bootstrap < 1:
+ return {"error": f"n_bootstrap must be >= 1, got {n_bootstrap}"}
+ if not 0.0 < confidence < 1.0:
+ return {"error": f"confidence must be in (0, 1), got {confidence}"}
+ if seed < 0:
+ return {"error": f"seed must be >= 0, got {seed}"}
+
returns = equity_curve.pct_change().dropna().values
if len(returns) < 5:
return {"error": "need at least 5 return observations"}
@@ -172,6 +186,8 @@ def walk_forward_analysis(
Returns:
Dict with per_window stats, consistency metrics.
"""
+ if n_windows < 1:
+ return {"error": f"n_windows must be >= 1, got {n_windows}"}
if len(equity_curve) < n_windows * 2:
return {"error": f"need at least {n_windows * 2} bars for {n_windows} windows"}
diff --git a/agent/tests/test_validation.py b/agent/tests/test_validation.py
index 7fad148a0..340ca2a64 100644
--- a/agent/tests/test_validation.py
+++ b/agent/tests/test_validation.py
@@ -96,6 +96,20 @@ class TestMonteCarlo:
result = monte_carlo_test(trades, 1_000_000)
assert "error" in result
+ @pytest.mark.parametrize("n_simulations", [0, -1, -100])
+ def test_non_positive_simulations_errors(self, n_simulations: int) -> None:
+ """A non-positive n_simulations must not raise (was ZeroDivisionError)."""
+ trades = _make_trades([100, -50, 200, -30, 150])
+ result = monte_carlo_test(trades, 1_000_000, n_simulations=n_simulations)
+ assert "error" in result
+ assert result["p_value_sharpe"] == 1.0
+
+ def test_negative_seed_errors(self) -> None:
+ """A negative seed must not raise (default_rng rejects it)."""
+ trades = _make_trades([100, -50, 200, -30, 150])
+ result = monte_carlo_test(trades, 1_000_000, n_simulations=10, seed=-1)
+ assert "error" in result
+
def test_reproducibility(self) -> None:
trades = _make_trades([100, -50, 200, -30, 150, -80])
r1 = monte_carlo_test(trades, 1_000_000, n_simulations=100, seed=42)
@@ -137,6 +151,26 @@ class TestBootstrapSharpe:
result = bootstrap_sharpe_ci(eq, n_bootstrap=100)
assert "error" in result
+ @pytest.mark.parametrize("n_bootstrap", [0, -1, -50])
+ def test_non_positive_bootstrap_errors(self, n_bootstrap: int) -> None:
+ """A non-positive n_bootstrap must not raise (was IndexError from percentile)."""
+ eq = _make_equity(100)
+ result = bootstrap_sharpe_ci(eq, n_bootstrap=n_bootstrap)
+ assert "error" in result
+
+ @pytest.mark.parametrize("confidence", [0.0, 1.0, 1.5, -0.2])
+ def test_confidence_out_of_range_errors(self, confidence: float) -> None:
+ """A confidence outside (0, 1) must not raise (was percentile ValueError)."""
+ eq = _make_equity(100)
+ result = bootstrap_sharpe_ci(eq, confidence=confidence, n_bootstrap=100)
+ assert "error" in result
+
+ def test_negative_seed_errors(self) -> None:
+ """A negative seed must not raise (default_rng rejects it)."""
+ eq = _make_equity(100)
+ result = bootstrap_sharpe_ci(eq, n_bootstrap=10, seed=-1)
+ assert "error" in result
+
def test_reproducibility(self) -> None:
eq = _make_equity(50)
r1 = bootstrap_sharpe_ci(eq, n_bootstrap=100, seed=42)
@@ -204,6 +238,18 @@ class TestWalkForward:
result = walk_forward_analysis(eq, [], n_windows=5)
assert "error" in result
+ @pytest.mark.parametrize("n_windows", [0, -1, -3])
+ def test_non_positive_windows_errors(self, n_windows: int) -> None:
+ """A non-positive n_windows must not raise or silently return garbage.
+
+ n_windows=0 raised ZeroDivisionError; a negative value silently returned
+ an empty ``windows`` list with a NaN ``return_mean`` and a negative
+ ``consistency_rate``.
+ """
+ eq = _make_equity(100)
+ result = walk_forward_analysis(eq, [], n_windows=n_windows)
+ assert "error" in result
+
# ---------------------------------------------------------------------------
# run_validation dispatcher