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)