From c6e8f0816472dcbcb10e9fee92e6d76793f14b97 Mon Sep 17 00:00:00 2001 From: anor Date: Sun, 8 Mar 2026 12:35:42 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BF=D0=BE=D1=80=D1=8F=D0=B4=D0=BE?= =?UTF-8?q?=D0=BA=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B2=20calc=5Fmetrics=20(UnboundLocalError)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 --- improvements.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/improvements.py b/improvements.py index e808671..c42cd63 100644 --- a/improvements.py +++ b/improvements.py @@ -447,18 +447,17 @@ def calc_metrics(trades, label=""): dd = (t["equity"] - peak) / peak max_dd = min(max_dd, dd) - # Sharpe: annualize based on actual trading frequency, not assumed weekly + first_date = trades[0]["date"] + last_date = trades[-1]["date"] + years = (last_date - first_date).days / 365.25 + + # Sharpe: annualize based on actual trading frequency trade_rets = [t["pnl_pct"] / 100 for t in trades] if len(trade_rets) > 2 and np.std(trade_rets) > 0: - years = (last_date - first_date).days / 365.25 trades_per_year = len(trade_rets) / years if years > 0 else 52 sharpe = np.mean(trade_rets) / np.std(trade_rets) * np.sqrt(trades_per_year) else: sharpe = 0 - - first_date = trades[0]["date"] - last_date = trades[-1]["date"] - years = (last_date - first_date).days / 365.25 annual_ret = ((final_eq / INITIAL_EQUITY) ** (1 / years) - 1) * 100 if years > 0 else 0 return {