fix: Sharpe ratio — daily returns вместо trade count + memory leak fix

Sharpe считался через sqrt(trades_per_year), что завышало метрику в ~2x.
Исправлено на daily returns + sqrt(252) во всех бэктест скриптах.
Утечка памяти: добавлен del tensors в model.train() и evaluate.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-12 13:39:50 +10:00
co-authored by Claude Opus 4.6
parent 81ba7541d3
commit 2601152afe
7 changed files with 1442 additions and 5 deletions
+21 -5
View File
@@ -24,11 +24,27 @@ def compute_metrics(trades: list[dict], initial_capital: float,
wr = len(wins) / len(trades) * 100
if len(trades) > 1 and years > 0:
returns = np.array([t["pnl_net"] / t["equity_at_entry"] for t in trades])
tpy = len(trades) / years
sharpe = float(
np.mean(returns) / np.std(returns) * np.sqrt(tpy)
) if np.std(returns) > 0 else 0.0
# Daily returns method (proper annualization)
if trades[0].get("entry_idx") is not None:
first_idx = min(t["entry_idx"] for t in trades)
last_idx = max(t["entry_idx"] for t in trades)
bars_per_day = 6 # 4h bars
n_days = max((last_idx - first_idx) // bars_per_day, 1)
daily_pnl = np.zeros(n_days)
for t in trades:
day = min((t["entry_idx"] - first_idx) // bars_per_day, n_days - 1)
daily_pnl[day] += t["pnl_net"]
daily_ret = daily_pnl / initial_capital
sharpe = float(
np.mean(daily_ret) / np.std(daily_ret) * np.sqrt(252)
) if np.std(daily_ret) > 0 else 0.0
else:
# Fallback: per-trade returns (less accurate)
returns = np.array([t["pnl_net"] / t["equity_at_entry"] for t in trades])
tpy = len(trades) / years
sharpe = float(
np.mean(returns) / np.std(returns) * np.sqrt(tpy)
) if np.std(returns) > 0 else 0.0
else:
sharpe = 0.0