feat(metrics): Sharpe + max drawdown + total return

Metriche base per valutazione strategie: Sharpe annualizzato
(default 8760 periodi/anno per dati orari), max drawdown
percentuale dalla curva equity, total return cumulativo.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-09 19:18:48 +02:00
parent 9301ab9051
commit 2687ce7dd2
3 changed files with 67 additions and 0 deletions
View File
+27
View File
@@ -0,0 +1,27 @@
from __future__ import annotations
import numpy as np
import pandas as pd # type: ignore[import-untyped]
def sharpe_ratio(returns: pd.Series, periods_per_year: int = 8760, rf: float = 0.0) -> float:
"""Sharpe annualizzato. periods_per_year=8760 per dati orari."""
excess = returns - rf / periods_per_year
std = excess.std(ddof=1)
if std == 0 or np.isnan(std):
return 0.0
return float(np.sqrt(periods_per_year) * excess.mean() / std)
def max_drawdown(equity: pd.Series) -> float:
"""Max drawdown percentuale (positivo)."""
peak = equity.cummax()
dd = (peak - equity) / peak.replace(0, np.nan)
dd = dd.fillna(0.0)
return float(dd.max())
def total_return(equity: pd.Series) -> float:
if equity.iloc[0] == 0:
return float(equity.iloc[-1])
return float(equity.iloc[-1] / equity.iloc[0] - 1.0)