| name | backtesting-trading-strategies |
| description | Backtest crypto and traditional trading strategies against historical data.
Calculates performance metrics (Sharpe, Sortino, max drawdown), generates equity curves,
and optimizes strategy parameters. Use when user wants to test a trading strategy,
validate signals, or compare approaches.
Trigger with phrases like "backtest strategy", "test trading strategy", "historical performance",
"simulate trades", "optimize parameters", or "validate signals".
|
| allowed-tools | Read, Write, Edit, Grep, Glob, Bash(python:*) |
| version | 2.0.0 |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| license | MIT |
Backtesting Trading Strategies
Overview
Validate trading strategies against historical data before risking real capital. This skill provides a complete backtesting framework with 8 built-in strategies, comprehensive performance metrics, and parameter optimization.
Key Features:
- 8 pre-built trading strategies (SMA, EMA, RSI, MACD, Bollinger, Breakout, Mean Reversion, Momentum)
- Full performance metrics (Sharpe, Sortino, Calmar, VaR, max drawdown)
- Parameter grid search optimization
- Equity curve visualization
- Trade-by-trade analysis
Prerequisites
Install required dependencies:
pip install pandas numpy yfinance matplotlib
For A-share / ETF backtests (6-digit codes such as 510300 or 510300.SS), the scripts load history via this repo’s plugins/data_collection stack (AkShare, mootdx, project cache — same strategy as openclaw-data-china-stock). Install:
pip install akshare
Optional for advanced features:
pip install ta-lib scipy scikit-learn
Instructions
Step 1: Fetch Historical Data
Crypto / US (Yahoo):
python {baseDir}/scripts/fetch_data.py --symbol BTC-USD --period 2y --interval 1d --source yfinance
China ETF (plugin-aligned pipeline, default for --source auto when symbol is a 6-digit code):
python {baseDir}/scripts/fetch_data.py --symbol 510300.SS --period 2y --interval 1d --source auto
Data is cached to {baseDir}/data/{symbol}_{interval}.csv for reuse.
Step 2: Run Backtest
Basic backtest with default parameters:
python {baseDir}/scripts/backtest.py --strategy sma_crossover --symbol BTC-USD --period 1y --data-source yfinance
China ETF (uses plugins/data_collection automatically; no Yahoo):
python {baseDir}/scripts/backtest.py --strategy sma_crossover --symbol 510300.SS --period 1y
Advanced backtest with custom parameters:
python {baseDir}/scripts/backtest.py \
--strategy rsi_reversal \
--symbol ETH-USD \
--period 1y \
--capital 10000 \
--params '{"period": 14, "overbought": 70, "oversold": 30}'
Step 3: Analyze Results
Results are saved to {baseDir}/reports/ including:
*_summary.txt - Performance metrics
*_trades.csv - Trade log
*_equity.csv - Equity curve data
*_chart.png - Visual equity curve
Step 4: Optimize Parameters
Find optimal parameters via grid search:
python {baseDir}/scripts/optimize.py \
--strategy sma_crossover \
--symbol BTC-USD \
--period 1y \
--param-grid '{"fast_period": [10, 20, 30], "slow_period": [50, 100, 200]}'
Output
Performance Metrics
| Metric | Description |
|---|
| Total Return | Overall percentage gain/loss |
| CAGR | Compound annual growth rate |
| Sharpe Ratio | Risk-adjusted return (target: >1.5) |
| Sortino Ratio | Downside risk-adjusted return |
| Calmar Ratio | Return divided by max drawdown |
Risk Metrics
| Metric | Description |
|---|
| Max Drawdown | Largest peak-to-trough decline |
| VaR (95%) | Value at Risk at 95% confidence |
| CVaR (95%) | Expected loss beyond VaR |
| Volatility | Annualized standard deviation |
Trade Statistics
| Metric | Description |
|---|
| Total Trades | Number of round-trip trades |
| Win Rate | Percentage of profitable trades |
| Profit Factor | Gross profit divided by gross loss |
| Expectancy | Expected value per trade |
Example Output
================================================================================
BACKTEST RESULTS: SMA CROSSOVER
BTC-USD | [start_date] to [end_date]
================================================================================
PERFORMANCE | RISK
Total Return: +47.32% | Max Drawdown: -18.45%
CAGR: +47.32% | VaR (95%): -2.34%
Sharpe Ratio: 1.87 | Volatility: 42.1%
Sortino Ratio: 2.41 | Ulcer Index: 8.2
--------------------------------------------------------------------------------
TRADE STATISTICS
Total Trades: 24 | Profit Factor: 2.34
Win Rate: 58.3% | Expectancy: $197.17
Avg Win: $892.45 | Max Consec. Losses: 3
================================================================================
Supported Strategies
| Strategy | Description | Key Parameters |
|---|
sma_crossover | Simple moving average crossover | fast_period, slow_period |
ema_crossover | Exponential MA crossover | fast_period, slow_period |
rsi_reversal | RSI overbought/oversold | period, overbought, oversold |
macd | MACD signal line crossover | fast, slow, signal |
bollinger_bands | Mean reversion on bands | period, std_dev |
breakout | Price breakout from range | lookback, threshold |
mean_reversion | Return to moving average | period, z_threshold |
momentum | Rate of change momentum | period, threshold |
Configuration
Edit {baseDir}/config/settings.yaml (loaded automatically by backtest.py, optimize.py, fetch_data.py):
data.provider: auto | china | yfinance | coingecko — used when CLI omits --data-source / --source and env BACKTEST_DATA_SOURCE is unset.
data.cache_dir / reporting.output_dir: paths relative to the skill root unless absolute.
BACKTEST_SKILL_SETTINGS: optional path to a different YAML (same schema).
- Precedence: CLI →
BACKTEST_DATA_SOURCE → YAML data.provider → auto.
data:
provider: auto
cache_dir: ./data
default_interval: 1d
backtest:
default_capital: 10000
commission: 0.001
slippage: 0.0005
reporting:
output_dir: ./reports
risk:
max_position_size: 0.95
stop_loss: null
take_profit: null
Implementation: {baseDir}/scripts/skill_settings.py.
Verification (smoke tests)
Full command log and ops notes live in the assistant repo: docs/ops/回测使用指导-自动任务与日常交互.md → §7(脚本实测摘要;全文仅覆盖本 Skill,不含涨停回马枪工具)。
| Check | Command (from {baseDir}/scripts/) | Expected |
|---|
| List strategies | python3 backtest.py --list | Prints 8 strategies |
| SMA needs long history | python3 backtest.py --strategy sma_crossover --symbol 510300.SS --period 90d | Exit 1, message about ≥205 bars |
| CN ETF RSI | python3 backtest.py --strategy rsi_reversal --symbol 510300.SS --period 1y --quiet | Exit 0; data via plugins/data_collection |
| Crypto Yahoo | python3 backtest.py --strategy rsi_reversal --symbol BTC-USD --period 120d --data-source yfinance --quiet | Exit 0 |
| Crypto CoinGecko | python3 backtest.py --strategy rsi_reversal --symbol ETH-USD --period 90d --data-source coingecko --quiet | Exit 0 |
| Fetch CN | python3 fetch_data.py --symbol 510300.SS --period 120d | Exit 0; writes data/510300.SS_1d.csv |
| Fetch Yahoo | python3 fetch_data.py --symbol BTC-USD --period 60d --source yfinance | Exit 0 |
| Tiny optimize grid | python3 optimize.py --strategy rsi_reversal --symbol 510300.SS --period 1y --param-grid '{"period":[12,14]}' | Exit 0 |
| Repo wrapper | From scripts/: python3 ../../../scripts/run_backtest_trading_strategies.py --strategy rsi_reversal --symbol 510300 --period 6m --quiet (or use repo absolute path to that script) | Exit 0; same as backtest.py |
Lightweight Backtest (MVP mode)
When users ask for “quick backtest / strategy optimization / Sharpe / max drawdown” but the environment lacks a dedicated tool_backtest_*, run a lightweight backtest by combining:
tool_fetch_market_data (to get sufficient historical OHLCV)
tool_calculate_technical_indicators (to derive signals/features)
Mandatory rules (MVP mode):
- If history is insufficient to evaluate the described strategy, return
insufficient_evidence.
- Explicitly state this is
MVP mode and describe capability boundaries.
- Do not output specific buy/sell points, position sizing, or leverage advice.
- Parameter search ranges must be read from
config/strategy-backtester_config.yaml (skill-local config).
Output structure (fixed):
strategy_spec and backtest_window\n2) performance and risk_metrics\n3) trade_stats and parameter_sensitivity\n4) limitations and next_experiments
Remember: 510300 vs 510300.SS → two different cache files under data/; pick one symbol style per workflow.
Error Handling
See {baseDir}/references/errors.md for common issues and solutions.
Examples
See {baseDir}/references/examples.md for detailed usage examples including:
- Multi-asset comparison
- Walk-forward analysis
- Parameter optimization workflows
Files
| File | Purpose |
|---|
scripts/backtest.py | Main backtesting engine |
scripts/china_stock_loader.py | CN ETF path via plugins/data_collection |
scripts/skill_settings.py | YAML + env + CLI precedence for paths and data.provider |
scripts/fetch_data.py | Historical data fetcher |
scripts/strategies.py | Strategy definitions |
scripts/metrics.py | Performance calculations |
scripts/optimize.py | Parameter optimization |
Resources