File size: 11,536 Bytes
3339913 3d257f3 3339913 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | """The Lab: one call that runs a backtest and then tries to disprove it.
This is the module both the Gradio Space and the CLI drive. Keeping the whole
pipeline here means the app and the command line can never disagree about what
a Reality Score means.
"""
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from typing import Callable, Dict, List, Optional
import numpy as np
import pandas as pd
from .data import load_ohlcv
from .engine import run_backtest
from .metrics import infer_periods_per_year
from .strategies import Strategy, get_strategy, list_strategies
from .types import BacktestResult, CostModel, MarketData
from .validation.deflated_sharpe import deflated_sharpe_ratio, min_track_record_length
from .validation.pbo import probability_of_backtest_overfitting
from .validation.permutation import PermutationResult, permutation_test
from .validation.walkforward import walk_forward
from .verdict import reality_score
logger = logging.getLogger(__name__)
__all__ = ["LabConfig", "LabReport", "run_lab", "run_arena"]
ProgressFn = Optional[Callable[[float, str], None]]
@dataclass
class LabConfig:
symbol: str = "SPY"
start: str = "2015-01-01"
end: Optional[str] = None
interval: str = "1d"
source: str = "yahoo"
strategy: str = "sma_cross"
params: Dict[str, float] = field(default_factory=dict)
commission_bps: float = 1.0
slippage_bps: float = 2.0
short_borrow_bps: float = 50.0
lag: int = 1
allow_short: bool = True
max_leverage: float = 1.0
capital: float = 100_000.0
n_permutations: int = 250
permutation_method: str = "permute"
block_size: int = 20
wf_folds: int = 5
pbo_splits: int = 8
grid_limit: int = 40
seed: int = 0
def costs(self, multiplier: float = 1.0) -> CostModel:
return CostModel(
commission_bps=self.commission_bps * multiplier,
slippage_bps=self.slippage_bps * multiplier,
short_borrow_bps=self.short_borrow_bps * multiplier,
)
@dataclass
class LabReport:
config: LabConfig
market: MarketData
strategy: Strategy
params: Dict[str, float]
backtest: BacktestResult
permutation: Optional[PermutationResult] = None
dsr: Dict[str, float] = field(default_factory=dict)
pbo: Dict[str, object] = field(default_factory=dict)
walkforward: Dict[str, object] = field(default_factory=dict)
trials: Dict[str, object] = field(default_factory=dict)
verdict: Dict[str, object] = field(default_factory=dict)
cost_stress: Dict[str, float] = field(default_factory=dict)
benchmark_correlation: float = float("nan")
def _trial_matrix(
df: pd.DataFrame,
strategy: Strategy,
cfg: LabConfig,
progress: ProgressFn = None,
) -> tuple[np.ndarray, List[float], List[str]]:
"""Backtest every parameter combination a researcher would plausibly try.
The resulting ``T x N`` return matrix feeds both the Deflated Sharpe (how
many variants were tried, and how spread out were they) and PBO.
"""
grid = strategy.grid(limit=cfg.grid_limit)
costs = cfg.costs()
columns, sharpes, labels = [], [], []
for i, params in enumerate(grid):
target = strategy.generate(df, params)
result = run_backtest(
df, target, costs=costs, lag=cfg.lag,
max_leverage=cfg.max_leverage, allow_short=cfg.allow_short,
)
columns.append(result.returns.to_numpy(dtype=float))
sharpes.append(result.sharpe)
labels.append(", ".join(f"{k}={v}" for k, v in params.items()) or "default")
if progress is not None and i % 5 == 0:
progress((i + 1) / max(len(grid), 1), f"Variant {i + 1}/{len(grid)}")
matrix = np.column_stack(columns) if columns else np.zeros((len(df), 0))
return matrix, sharpes, labels
def run_lab(cfg: LabConfig, progress: ProgressFn = None) -> LabReport:
"""Run the full honesty pipeline for one strategy on one symbol."""
def step(fraction: float, message: str) -> None:
if progress is not None:
progress(min(max(fraction, 0.0), 1.0), message)
step(0.02, "Loading market data")
market = load_ohlcv(cfg.symbol, cfg.start, cfg.end, cfg.interval, cfg.source)
df = market.df
if len(df) < 120:
raise ValueError(
f"Only {len(df)} bars available for {cfg.symbol}. "
"Widen the date range — anything shorter cannot be validated."
)
strategy = get_strategy(cfg.strategy)
params = strategy.clean(cfg.params)
ppy = infer_periods_per_year(df.index)
step(0.10, "Running the backtest")
target = strategy.generate(df, params)
backtest = run_backtest(
df,
target,
costs=cfg.costs(),
lag=cfg.lag,
max_leverage=cfg.max_leverage,
allow_short=cfg.allow_short,
initial_capital=cfg.capital,
periods_per_year=ppy,
meta={"symbol": market.symbol, "strategy": strategy.key, "params": params},
)
step(0.16, "Stress-testing costs")
stressed = run_backtest(
df, target, costs=cfg.costs(3.0), lag=cfg.lag,
max_leverage=cfg.max_leverage, allow_short=cfg.allow_short,
periods_per_year=ppy,
)
base_sharpe = backtest.sharpe
cost_stress_ratio = float(stressed.sharpe / base_sharpe) if base_sharpe > 1e-9 else 0.0
cost_stress = {
"sharpe_1x": base_sharpe,
"sharpe_3x": stressed.sharpe,
"ratio": cost_stress_ratio,
"return_3x": float(stressed.metrics.get("total_return", 0.0)),
}
step(0.22, "Backtesting every parameter variant")
matrix, trial_sharpes, labels = _trial_matrix(
df, strategy, cfg, lambda f, m: step(0.22 + 0.18 * f, m)
)
n_trials = max(len(trial_sharpes), 1)
step(0.42, "Deflating the Sharpe ratio for selection bias")
dsr = deflated_sharpe_ratio(
backtest.returns.to_numpy(dtype=float),
sharpe_annual=base_sharpe,
periods_per_year=ppy,
n_trials=n_trials,
trial_sharpes=trial_sharpes if n_trials > 1 else None,
)
mtrl = min_track_record_length(
dsr["sr_per_period"], dsr["n_obs"], dsr["skew"], dsr["kurtosis"],
benchmark=dsr["threshold_sr_per_period"],
)
dsr["min_track_record_bars"] = mtrl
dsr["min_track_record_years"] = float(mtrl / ppy) if np.isfinite(mtrl) else float("inf")
step(0.46, "Measuring backtest overfitting")
pbo = probability_of_backtest_overfitting(matrix, n_splits=cfg.pbo_splits, labels=labels)
step(0.50, "Shuffling the market")
permutation = None
if cfg.n_permutations > 0:
permutation = permutation_test(
df,
lambda frame: strategy.generate(frame, params),
n_permutations=cfg.n_permutations,
method=cfg.permutation_method,
block=cfg.block_size,
costs=cfg.costs(),
lag=cfg.lag,
max_leverage=cfg.max_leverage,
allow_short=cfg.allow_short,
seed=cfg.seed,
observed=base_sharpe,
progress=lambda f, m: step(0.50 + 0.32 * f, m),
)
step(0.84, "Walking the strategy forward")
wf = walk_forward(
df, strategy, n_folds=cfg.wf_folds, costs=cfg.costs(), lag=cfg.lag,
max_leverage=cfg.max_leverage, allow_short=cfg.allow_short,
grid_limit=min(cfg.grid_limit, 24),
progress=lambda f, m: step(0.84 + 0.12 * f, m),
)
bench_corr = float(
pd.Series(backtest.returns).corr(backtest.benchmark_equity.pct_change().fillna(0.0))
)
step(0.98, "Grading")
verdict = reality_score(
metrics=backtest.metrics,
benchmark_metrics=backtest.benchmark_metrics,
p_value=permutation.p_value if permutation else None,
dsr=dsr.get("dsr"),
pbo=pbo.get("pbo"),
wf_efficiency=wf.get("efficiency"),
wf_win_rate=wf.get("oos_win_rate"),
cost_stress_ratio=cost_stress_ratio,
benchmark_correlation=bench_corr,
)
step(1.0, "Done")
return LabReport(
config=cfg,
market=market,
strategy=strategy,
params=params,
backtest=backtest,
permutation=permutation,
dsr=dsr,
pbo=pbo,
walkforward=wf,
trials={"n": n_trials, "sharpes": trial_sharpes, "labels": labels, "matrix_shape": matrix.shape},
verdict=verdict,
cost_stress=cost_stress,
benchmark_correlation=bench_corr,
)
def run_arena(
cfg: LabConfig,
strategy_keys: Optional[List[str]] = None,
n_permutations: int = 120,
progress: ProgressFn = None,
) -> tuple[pd.DataFrame, MarketData, Dict[str, BacktestResult]]:
"""Race every strategy on the same market, ranked by evidence not returns.
Buy & hold and the coin flip stay in the field on purpose: a leaderboard
without a control group is marketing, not measurement.
"""
market = load_ohlcv(cfg.symbol, cfg.start, cfg.end, cfg.interval, cfg.source)
df = market.df
ppy = infer_periods_per_year(df.index)
costs = cfg.costs()
keys = strategy_keys or [s.key for s in list_strategies()]
rows, curves = [], {}
for i, key in enumerate(keys):
strategy = get_strategy(key)
params = strategy.defaults()
target = strategy.generate(df, params)
result = run_backtest(
df, target, costs=costs, lag=cfg.lag, max_leverage=cfg.max_leverage,
allow_short=cfg.allow_short, initial_capital=cfg.capital, periods_per_year=ppy,
)
curves[key] = result
p_value = None
if n_permutations > 0:
p_value = permutation_test(
df,
lambda frame, s=strategy, p=params: s.generate(frame, p),
n_permutations=n_permutations,
method=cfg.permutation_method,
block=cfg.block_size,
costs=costs,
lag=cfg.lag,
max_leverage=cfg.max_leverage,
allow_short=cfg.allow_short,
seed=cfg.seed,
observed=result.sharpe,
).p_value
grid_size = len(strategy.grid(limit=cfg.grid_limit))
dsr = deflated_sharpe_ratio(
result.returns.to_numpy(dtype=float),
sharpe_annual=result.sharpe,
periods_per_year=ppy,
n_trials=grid_size,
)
rows.append(
{
"Strategy": strategy.name,
"key": key,
"Family": strategy.family,
"Return": result.metrics.get("total_return", 0.0),
"CAGR": result.metrics.get("cagr", 0.0),
"Sharpe": result.sharpe,
"MaxDD": result.metrics.get("max_drawdown", 0.0),
"Trades": int(result.metrics.get("n_trades", 0)),
"p-value": p_value if p_value is not None else float("nan"),
"DSR": dsr["dsr"],
}
)
if progress is not None:
progress((i + 1) / len(keys), f"{strategy.name} ({i + 1}/{len(keys)})")
table = pd.DataFrame(rows)
if not table.empty:
# Rank by evidence: a high Sharpe with a p-value of 0.4 is not a win.
table["Evidence"] = (1.0 - table["p-value"].fillna(0.5)) * table["DSR"]
table = table.sort_values("Evidence", ascending=False).reset_index(drop=True)
table.insert(0, "#", table.index + 1)
return table, market, curves
|