"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(metrics): report profit factor as undefined when no trade lost
David Ariasf110ad6ca94d
diff --git a/agent/backtest/engines/options_portfolio.py b/agent/backtest/engines/options_portfolio.pyindex cdff18f23..5e35bf9e4 100644--- a/agent/backtest/engines/options_portfolio.py+++ b/agent/backtest/engines/options_portfolio.py@@ -838,8 +838,14 @@ def _calc_options_metrics(losses = [p for p in closed_pnl if p < 0]win_rate = len(wins) / len(closed_pnl) if closed_pnl else 0.0avg_win = np.mean(wins) if wins else 0.0- avg_loss = abs(np.mean(losses)) if losses else 1e-10- pl_ratio = avg_win / avg_loss if avg_loss > 1e-10 else 0.0+ avg_loss = abs(np.mean(losses)) if losses else 0.0+ # Closed trades with no loss leave the ratio without a denominator: None,+ # as in backtest.metrics.win_rate_and_stats, since 0.0 would rank the run+ # below every other one. No closed trade at all keeps 0.0.+ if avg_loss > 1e-10:+ pl_ratio = avg_win / avg_loss+ else:+ pl_ratio = None if closed_pnl else 0.0return {"final_value": final_value,@@ -853,6 +859,6 @@ def _calc_options_metrics(# options_rejected_opens and must not inflate the trade count."trade_count": sum(1 for t in trades if t.get("side") != "reject"),"win_rate": round(win_rate, 4),- "profit_loss_ratio": round(pl_ratio, 4),+ "profit_loss_ratio": round(pl_ratio, 4) if pl_ratio is not None else None,"warnings": warnings,}diff --git a/agent/backtest/metrics.py b/agent/backtest/metrics.pyindex 9f50d2ffa..79f85409a 100644--- a/agent/backtest/metrics.py+++ b/agent/backtest/metrics.py@@ -318,7 +318,7 @@ def buy_and_hold_return(close: Any) -> Optional[float]:return last / first - 1.0-def win_rate_and_stats(trades: List[TradeRecord]) -> Dict[str, float]:+def win_rate_and_stats(trades: List[TradeRecord]) -> Dict[str, Optional[float]]:"""Win rate and P&L statistics from completed trades.Args:@@ -326,7 +326,8 @@ def win_rate_and_stats(trades: List[TradeRecord]) -> Dict[str, float]:Returns:Dict with win_rate, profit_loss_ratio, max_consecutive_loss,- avg_holding_bars, profit_factor.+ avg_holding_bars, profit_factor. profit_loss_ratio and profit_factor+ are ``None`` when no trade lost, since both divide by the losses."""if not trades:return {@@ -342,13 +343,16 @@ def win_rate_and_stats(trades: List[TradeRecord]) -> Dict[str, float]:win_rate = len(wins) / len(trades)+ # With no losing trade both ratios have no denominator. 0.0 would read as+ # "no profit at all" and rank a run that never lost below every other run,+ # so they are None: an empty metrics.csv cell and null in JSON.avg_win = float(np.mean(wins)) if wins else 0.0- avg_loss = abs(float(np.mean(losses))) if losses else 1e-10- profit_loss_ratio = avg_win / avg_loss if avg_loss > 1e-10 else 0.0+ avg_loss = abs(float(np.mean(losses))) if losses else 0.0+ profit_loss_ratio = avg_win / avg_loss if avg_loss > 1e-10 else Nonegross_profit = sum(wins) if wins else 0.0- gross_loss = abs(sum(losses)) if losses else 1e-10- profit_factor = gross_profit / gross_loss if gross_loss > 1e-10 else 0.0+ gross_loss = abs(sum(losses)) if losses else 0.0+ profit_factor = gross_profit / gross_loss if gross_loss > 1e-10 else Nonemax_consec = 0cur_consec = 0@@ -364,10 +368,12 @@ def win_rate_and_stats(trades: List[TradeRecord]) -> Dict[str, float]:return {"win_rate": win_rate,- "profit_loss_ratio": round(profit_loss_ratio, 4),+ "profit_loss_ratio": (+ round(profit_loss_ratio, 4) if profit_loss_ratio is not None else None+ ),"max_consecutive_loss": max_consec,"avg_holding_bars": round(avg_holding, 1),- "profit_factor": round(profit_factor, 4),+ "profit_factor": round(profit_factor, 4) if profit_factor is not None else None,}diff --git a/agent/tests/test_metrics.py b/agent/tests/test_metrics.pyindex 7733eb6cf..f4c4d7ffa 100644--- a/agent/tests/test_metrics.py+++ b/agent/tests/test_metrics.py@@ -9,6 +9,9 @@ Validates:from __future__ import annotations+import csv+import io+import jsonimport mathimport numpy as np@@ -249,6 +252,23 @@ class TestWinRateAndStats:stats = win_rate_and_stats(trades)assert stats["profit_loss_ratio"] == pytest.approx(2.0)+ def test_ratios_undefined_when_no_trade_lost(self) -> None:+ # Both ratios divide by the losses. 0.0 here used to rank a run that+ # never lost below one that lost money.+ stats = win_rate_and_stats([_trade(pnl=100), _trade(pnl=200), _trade(pnl=50)])+ assert stats["profit_factor"] is None+ assert stats["profit_loss_ratio"] is None++ def test_ratios_undefined_for_breakeven_trades(self) -> None:+ stats = win_rate_and_stats([_trade(pnl=0.0), _trade(pnl=0.0)])+ assert stats["profit_factor"] is None+ assert stats["profit_loss_ratio"] is None++ def test_ratios_zero_when_no_trade_won(self) -> None:+ stats = win_rate_and_stats([_trade(pnl=-100), _trade(pnl=-200)])+ assert stats["profit_factor"] == 0.0+ assert stats["profit_loss_ratio"] == 0.0+def test_empty_trades(self) -> None:stats = win_rate_and_stats([])assert stats["win_rate"] == 0.0@@ -375,6 +395,18 @@ class TestCalcMetrics:assert m["trade_count"] == 2assert m["win_rate"] == 0.5+ def test_ratios_undefined_when_no_trade_lost(self) -> None:+ eq = self._growing_equity()+ m = calc_metrics(eq, [_trade(pnl=100), _trade(pnl=200)], 1_000_000, 252)+ assert m["win_rate"] == 1.0+ assert m["profit_factor"] is None+ assert m["profit_loss_ratio"] is None+ # null in the runner's strict JSON, an empty cell in metrics.csv+ json.dumps(m, allow_nan=False)+ row = next(csv.DictReader(io.StringIO(pd.DataFrame([m]).to_csv(index=False))))+ assert row["profit_factor"] == ""+ assert row["profit_loss_ratio"] == ""+def test_benchmark_comparison(self) -> None:eq = self._growing_equity()dates = eq.indexdiff --git a/agent/tests/test_options_portfolio_correctness.py b/agent/tests/test_options_portfolio_correctness.pyindex 5be56d2c8..5e9d97f2c 100644--- a/agent/tests/test_options_portfolio_correctness.py+++ b/agent/tests/test_options_portfolio_correctness.py@@ -177,3 +177,17 @@ def test_bars_per_year_none_on_degenerate_span_falls_back_to_default() -> None:metrics = _calc_options_metrics(equity, 100.0, [], bars_per_year=None)assert metrics["final_value"] == 99.0json.dumps(metrics, allow_nan=False)+++def test_profit_loss_ratio_undefined_when_no_closed_trade_lost() -> None:+ """No losing close: the ratio has no denominator, and 0.0 would rank the run last."""+ trades = [{"pnl": 100.0}, {"pnl": 50.0}]+ metrics = _calc_options_metrics(pd.Series([100.0, 250.0]), 100.0, trades)+ assert metrics["win_rate"] == 1.0+ assert metrics["profit_loss_ratio"] is None+ json.dumps(metrics, allow_nan=False)+++def test_profit_loss_ratio_zero_without_closed_trades() -> None:+ metrics = _calc_options_metrics(pd.Series([100.0, 100.0]), 100.0, [])+ assert metrics["profit_loss_ratio"] == 0.0