backtesting
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBacktesting — Full Backtesting Skill
回测 — 完整回测Skill
This skill implements the full 5-stage backtesting methodology from the course material:
Data → Research → Metrics → Parameterisation → Validation. It provides:
- 30+ risk/performance ratios (flat, numpy-vectorized, no classes)
- 10 classes of indicators following the course taxonomy (trend-following, oscillators, contrarians, flow, combined, discrete counts, seasonality, statistical, referential, fundamental)
- Event-driven backtesting engine with 8 built-in strategies
- Forward-looking simulation (Johnson SU marginals + t/Gaussian copula)
- Portfolio theory (Markowitz efficient frontier, portfolio-of-portfolios)
- Walk-forward cross-validation with IS/OOS split + gap
- Stress testing with parametric scenario shocks
- Fundamental analysis (Altman Z, Piotroski F, DuPont)
All scripts use only , , and . No heavy dependencies.
numpypandasscipyPart of the Gauss314 Skills Repository.
本Skill实现了课程资料中的完整五阶段回测方法论:数据→研究→指标→参数化→验证。它提供以下功能:
- 30+种风险/绩效比率(扁平化、numpy向量化实现,无类结构)
- 10类指标,遵循课程分类体系(趋势跟踪型、震荡型、逆向型、资金流型、组合型、离散计数型、季节性型、统计型、参考型、基本面型)
- 事件驱动回测引擎,内置8种策略
- 前瞻性模拟(Johnson SU边缘分布 + t/高斯Copula)
- 投资组合理论(Markowitz有效前沿、组合的组合)
- 滚动交叉验证(walk-forward CV),包含样本内/样本外拆分与间隔
- 压力测试,支持参数化场景冲击
- 基本面分析(Altman Z、Piotroski F、杜邦分析)
所有脚本仅使用、和,无重型依赖。
numpypandasscipy属于Gauss314 Skills Repository的一部分。
File Map
文件结构
skills/backtesting/
├── SKILL.md ← This file
├── references/
│ ├── BACKTESTING_THEORY.md ← Marco conceptual: GIGO, trilema, 5 etapas (ES)
│ ├── RATIOS.md ← Fórmulas, convención de retornos, advertencias (ES)
│ ├── FEATURES.md ← Taxonomía de 10 clases de indicadores con edges (ES)
│ ├── SIMULATIONS.md ← Pipeline Johnson SU + cópula (ES)
│ ├── VALIDATION.md ← Suite de validación de 4 niveles (ES)
│ └── OTHER_FEATURES.md ← Fundamental, Sentimiento, Exógenos (ES)
├── assets/
│ ├── sp500_returns.csv ← SPY benchmark daily returns (lin + log), 1980-today
│ ├── momentum_sma50_200_returns.csv ← SMA(50)/SMA(200) crossover strategy returns
│ ├── contrarian_bbands_returns.csv ← Bollinger Band contrarian strategy returns
│ ├── sample_portfolios.json ← Real investor portfolios (Buffett, Dalio, Ackman, 60/40)
│ ├── defaults.json ← Default parameters (VaR alpha, windows, etc.)
│ └── validation_cases.json ← 6 known cases for ratio validation
├── scripts/
│ ├── __init__.py
│ ├── ratios.py ← 30+ flat numpy functions for all risk/performance ratios
│ ├── indicators.py ← 10 classes of technical/statistical/fundamental indicators
│ ├── engine.py ← Event-driven BacktestEngine with 8 built-in strategies
│ ├── backtesting.py ← CLI: run, sweep, walkforward, montecarlo, optmpt, event, validate
│ ├── simulations.py ← CLI: marginal, copula, run, portfolio, scenarios
│ ├── forward.py ← CLI: project, risk, stress, summary
│ ├── distributions.py ← Fit + KS test for Normal/t/NCt/Laplace/JohnsonSU
│ ├── copulas.py ← t/Gaussian/Clayton/Gumbel/Frank copulas + sampling
│ ├── fundamental_ratios.py ← Income/balance/cashflow metrics, DuPont, Altman Z, Piotroski
│ └── validate.py ← 4-level validation: CLI modes, math consistency, edge cases, regression
└── tests/
└── test_ratios.py ← 18 pytest tests for core ratiosskills/backtesting/
├── SKILL.md ← This file
├── references/
│ ├── BACKTESTING_THEORY.md ← Marco conceptual: GIGO, trilema, 5 etapas (ES)
│ ├── RATIOS.md ← Fórmulas, convención de retornos, advertencias (ES)
│ ├── FEATURES.md ← Taxonomía de 10 clases de indicadores con edges (ES)
│ ├── SIMULATIONS.md ← Pipeline Johnson SU + cópula (ES)
│ ├── VALIDATION.md ← Suite de validación de 4 niveles (ES)
│ └── OTHER_FEATURES.md ← Fundamental, Sentimiento, Exógenos (ES)
├── assets/
│ ├── sp500_returns.csv ← SPY benchmark daily returns (lin + log), 1980-today
│ ├── momentum_sma50_200_returns.csv ← SMA(50)/SMA(200) crossover strategy returns
│ ├── contrarian_bbands_returns.csv ← Bollinger Band contrarian strategy returns
│ ├── sample_portfolios.json ← Real investor portfolios (Buffett, Dalio, Ackman, 60/40)
│ ├── defaults.json ← Default parameters (VaR alpha, windows, etc.)
│ └── validation_cases.json ← 6 known cases for ratio validation
├── scripts/
│ ├── __init__.py
│ ├── ratios.py ← 30+ flat numpy functions for all risk/performance ratios
│ ├── indicators.py ← 10 classes of technical/statistical/fundamental indicators
│ ├── engine.py ← Event-driven BacktestEngine with 8 built-in strategies
│ ├── backtesting.py ← CLI: run, sweep, walkforward, montecarlo, optmpt, event, validate
│ ├── simulations.py ← CLI: marginal, copula, run, portfolio, scenarios
│ ├── forward.py ← CLI: project, risk, stress, summary
│ ├── distributions.py ← Fit + KS test for Normal/t/NCt/Laplace/JohnsonSU
│ ├── copulas.py ← t/Gaussian/Clayton/Gumbel/Frank copulas + sampling
│ ├── fundamental_ratios.py ← Income/balance/cashflow metrics, DuPont, Altman Z, Piotroski
│ └── validate.py ← 4-level validation: CLI modes, math consistency, edge cases, regression
└── tests/
└── test_ratios.py ← 18 pytest tests for core ratiosWhat each file does
各文件功能
| File | Role | Key Functions / Modes |
|---|---|---|
| The core library. Every ratio is a flat function accepting 1-D arrays. | |
| 10 classes of indicators, covering all types from the course taxonomy. | |
| BacktestEngine class and 8 strategy functions. | |
| Main CLI. Run full backtests, walks, sweeps, optimization. | |
| Forward-looking simulation with Johnson SU + copula. | |
| Risk projection and stress testing. | |
| Distribution fitting and comparison. | |
| Copula fitting and sampling. | |
| Fundamental analysis ratios. | |
| 4-level integration testing suite. | 33 checks across CLI, math, edge cases, regression |
| 文件 | 作用 | 核心函数/模式 |
|---|---|---|
| 核心库。每个比率都是接受一维数组的扁平化函数。 | |
| 10类指标,覆盖课程分类体系中的所有类型。 | |
| BacktestEngine类与8种策略函数。 | |
| 主CLI工具。运行完整回测、滚动验证、参数扫描、优化等。 | |
| 基于Johnson SU + Copula的前瞻性模拟工具。 | |
| 风险预测与压力测试工具。 | |
| 分布拟合与对比工具。 | |
| Copula拟合与采样工具。 | |
| 基本面分析比率工具。 | |
| 四级集成测试套件。 | 覆盖CLI、数学逻辑、边缘场景、回归的33项检查 |
Quick Start
快速开始
Basic Ratios
基础比率计算
bash
undefinedbash
undefinedCompute all 30+ ratios on a CSV of prices
Compute all 30+ ratios on a CSV of prices
py scripts/backtesting.py run --prices assets/sp500_returns.csv
py scripts/backtesting.py run --prices assets/sp500_returns.csv
Compute with benchmark comparison
Compute with benchmark comparison
py scripts/backtesting.py run --prices assets/momentum_sma50_200_returns.csv --benchmark assets/sp500_returns.csv
undefinedpy scripts/backtesting.py run --prices assets/momentum_sma50_200_returns.csv --benchmark assets/sp500_returns.csv
undefinedValidate (4-level suite)
验证(四级测试套件)
bash
undefinedbash
undefinedFull validation (33 checks across 4 levels)
Full validation (33 checks across 4 levels)
py scripts/validate.py
py scripts/validate.py
Single level
Single level
py scripts/validate.py --nivel 1
Validates CLI modes, mathematical consistency of all ratios, edge case resilience, and post-fix regression. See [`references/VALIDATION.md`](./references/VALIDATION.md) for the detailed breakdown of all 33 checks.py scripts/validate.py --nivel 1
验证CLI模式、所有比率的数学一致性、边缘场景适应性以及修复后回归情况。查看[`references/VALIDATION.md`](./references/VALIDATION.md)了解33项检查的详细细分。Event-Driven Backtest
事件驱动回测
bash
undefinedbash
undefinedLoad a CSV with OHLCV data and run SMA crossover
Load a CSV with OHLCV data and run SMA crossover
py scripts/backtesting.py event --data my_stock.csv --strategy sma_crossover --fast 50 --slow 200 --commission 0.001
undefinedpy scripts/backtesting.py event --data my_stock.csv --strategy sma_crossover --fast 50 --slow 200 --commission 0.001
undefinedParameter Sweep
参数扫描
bash
undefinedbash
undefined2D sweep over fast/slow MA windows
2D sweep over fast/slow MA windows
py scripts/backtesting.py sweep --prices assets/sp500_returns.csv --p1-min 10 --p1-max 100 --p1-step 10
py scripts/backtesting.py sweep --prices assets/sp500_returns.csv --p1-min 10 --p1-max 100 --p1-step 10
2D over 2 parameters
2D over 2 parameters
py scripts/backtesting.py sweep --prices assets/sp500_returns.csv --p1-min 10 --p1-max 50 --p1-step 5 --p2-min 25 --p2-max 200 --p2-step 25
undefinedpy scripts/backtesting.py sweep --prices assets/sp500_returns.csv --p1-min 10 --p1-max 50 --p1-step 5 --p2-min 25 --p2-max 200 --p2-step 25
undefinedWalk-Forward
滚动交叉验证
bash
py scripts/backtesting.py walkforward --prices assets/sp500_returns.csv --splits 5 --gap 21bash
py scripts/backtesting.py walkforward --prices assets/sp500_returns.csv --splits 5 --gap 21Markowitz Optimization
Markowitz优化
bash
py scripts/backtesting.py optmpt --assets assets/sp500_returns.csv --iterations 5000bash
py scripts/backtesting.py optmpt --assets assets/sp500_returns.csv --iterations 5000Forward Simulation
前瞻性模拟
bash
py scripts/simulations.py marginal --returns assets/sp500_returns.csv
py scripts/simulations.py copula --returns assets/sp500_returns.csv --df 4
py scripts/forward.py project --returns assets/sp500_returns.csv --horizon 252 --paths 10000 --drift 0.08
py scripts/forward.py risk --returns assets/sp500_returns.csv --horizon 252 --paths 10000bash
py scripts/simulations.py marginal --returns assets/sp500_returns.csv
py scripts/simulations.py copula --returns assets/sp500_returns.csv --df 4
py scripts/forward.py project --returns assets/sp500_returns.csv --horizon 252 --paths 10000 --drift 0.08
py scripts/forward.py risk --returns assets/sp500_returns.csv --horizon 252 --paths 10000Portfolio Simulation
投资组合模拟
bash
py scripts/simulations.py portfolio --name warren_buffett
py scripts/simulations.py scenarios --name warren_buffett --cagr -0.3,-0.15,0,0.2,0.35,0.5bash
py scripts/simulations.py portfolio --name warren_buffett
py scripts/simulations.py scenarios --name warren_buffett --cagr -0.3,-0.15,0,0.2,0.35,0.5Using Ratios as a Library
将比率库用作模块
python
from scripts.ratios import *
prices = np.array([100, 105, 102, 110, 108, 115])
r = linear_returns(prices) # [0.05, -0.0286, 0.0784, -0.0182, 0.0648]
lr = log_returns(prices) # [0.0488, -0.0290, 0.0755, -0.0183, 0.0628]
sharpe_ratio(r) # 0.847
max_drawdown(prices) # -0.0370
kelly_fraction(lr) # 0.0793
var_all(r, alpha=0.05) # {'empirical': ..., 'normal': ..., 'johnsonsu': ...}
profit_factor(lr) # 2.314
payoff_ratio(lr) # 1.578
rachev_c(lr, alpha=0.05) # 1.234
common_sense_ratio(lr) # 2.856python
from scripts.ratios import *
prices = np.array([100, 105, 102, 110, 108, 115])
r = linear_returns(prices) # [0.05, -0.0286, 0.0784, -0.0182, 0.0648]
lr = log_returns(prices) # [0.0488, -0.0290, 0.0755, -0.0183, 0.0628]
sharpe_ratio(r) # 0.847
max_drawdown(prices) # -0.0370
kelly_fraction(lr) # 0.0793
var_all(r, alpha=0.05) # {'empirical': ..., 'normal': ..., 'johnsonsu': ...}
profit_factor(lr) # 2.314
payoff_ratio(lr) # 1.578
rachev_c(lr, alpha=0.05) # 1.234
common_sense_ratio(lr) # 2.856Using the Engine
使用回测引擎
python
from scripts.engine import BacktestEngine
eng = BacktestEngine(initial_capital=1.0, commission=0.001, slippage=0.0005)
eng.load_data(df_ohlcv)
result = eng.run(strategy='sma_crossover', strategy_params={'fast': 50, 'slow': 200})
print(result['metrics']['sharpe_ratio']) # 0.847
print(result['trades'])
print(result['metrics'])python
from scripts.engine import BacktestEngine
eng = BacktestEngine(initial_capital=1.0, commission=0.001, slippage=0.0005)
eng.load_data(df_ohlcv)
result = eng.run(strategy='sma_crossover', strategy_params={'fast': 50, 'slow': 200})
print(result['metrics']['sharpe_ratio']) # 0.847
print(result['trades'])
print(result['metrics'])Dependencies
依赖项
| Library | Required | Used for |
|---|---|---|
| ✅ | Vectorised computation, arrays, cumprod |
| ✅ | CSV I/O, rolling operations, DataFrames |
| ✅ | Distribution fitting, KS test, copulas |
| Optional | STL decomposition in indicators.py (Class 5) |
To run the full validation suite () you also need for the Level 4 regression check.
py scripts/validate.pypytestNo , , required.
archquantlibsklearn| 库 | 是否必需 | 用途 |
|---|---|---|
| ✅ | 向量化计算、数组、累积乘积 |
| ✅ | CSV输入输出、滚动操作、DataFrames |
| ✅ | 分布拟合、KS检验、Copula |
| 可选 | indicators.py中的STL分解(第5类指标) |
要运行完整的验证套件(),还需要来进行第4级回归检查。
py scripts/validate.pypytest无需、、。
archquantlibsklearnSee Also
相关链接
- Gauss314 Skills Repository — other skills for financial data
- — marco conceptual del backtesting (ES)
references/BACKTESTING_THEORY.md - — fórmulas, convención de retornos, advertencias (ES)
references/RATIOS.md - — taxonomía de 10 clases de indicadores con edges (ES)
references/FEATURES.md - — pipeline de Johnson SU + cópula (ES)
references/SIMULATIONS.md - — suite de validación de 4 niveles (ES)
references/VALIDATION.md - — fundamental, sentimiento, exógenos (ES)
references/OTHER_FEATURES.md
- Gauss314 Skills Repository — 其他金融数据相关Skill
- — 回测宏观概念(西班牙语)
references/BACKTESTING_THEORY.md - — 公式、收益率约定、注意事项(西班牙语)
references/RATIOS.md - — 带优势的10类指标分类体系(西班牙语)
references/FEATURES.md - — Johnson SU + Copula流程(西班牙语)
references/SIMULATIONS.md - — 四级验证套件(西班牙语)
references/VALIDATION.md - — 基本面、情绪、外生变量(西班牙语)
references/OTHER_FEATURES.md