Created
May 3, 2026 20:49
-
-
Save quantra-go-algo/9be47d9cff5afcebfdccbe87e8c902d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def max_drawdown(equity: pd.Series) -> float: | |
| peak = equity.cummax() | |
| dd = (equity / peak) - 1.0 | |
| return float(dd.min()) | |
| def qs_metrics(equity: pd.Series) -> dict: | |
| """ | |
| Quantstats-style metrics from an equity curve. | |
| """ | |
| r = equity.pct_change().fillna(0.0) | |
| n = len(r) | |
| years = n / 252.0 | |
| cagr = float(equity.iloc[-1] ** (1.0 / years) - 1.0) if years > 0 else float("nan") | |
| ann_vol = float(r.std(ddof=0) * math.sqrt(252)) | |
| ann_ret = float(r.mean() * 252) | |
| sharpe = float(ann_ret / (ann_vol + 1e-12)) | |
| downside = r.copy() | |
| downside[downside > 0] = 0.0 | |
| downside_dev = float(downside.std(ddof=0) * math.sqrt(252)) | |
| sortino = float(ann_ret / (downside_dev + 1e-12)) | |
| mdd = max_drawdown(equity) | |
| calmar = float(cagr / (abs(mdd) + 1e-12)) | |
| win_rate = float((r > 0).mean()) | |
| return { | |
| "CAGR": cagr, | |
| "AnnVol": ann_vol, | |
| "Sharpe": sharpe, | |
| "Sortino": sortino, | |
| "Calmar": calmar, | |
| "MaxDD": mdd, | |
| "WinRate": win_rate, | |
| } | |
| def print_metrics_table(non_m: dict, llm_m: dict | None) -> None: | |
| rows = {"Non-LLM": non_m} | |
| if llm_m is not None: | |
| rows["LLM"] = llm_m | |
| df = pd.DataFrame(rows).T | |
| for c in ["CAGR", "AnnVol", "MaxDD", "WinRate"]: | |
| df[c] = df[c] * 100.0 | |
| df = df[["CAGR", "AnnVol", "Sharpe", "Sortino", "Calmar", "MaxDD", "WinRate"]] | |
| print("\nQuantstats-style metrics (OOS 2023+):") | |
| print(df.to_string(float_format=lambda x: f"{x:,.3f}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment