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:
set -euo pipefail
pip install pandas numpy yfinance matplotlib
Optional for advanced features:
set -euo pipefail
pip install ta-lib scipy scikit-learn
Instructions
- Fetch historical data (cached to
${CLAUDESKILLDIR}/data/ for reuse):
python ${CLAUDE_SKILL_DIR}/scripts/fetch_data.py --symbol BTC-USD --period 2y --interval 1d
- Run a backtest with default or custom parameters:
python ${CLAUDE_SKILL_DIR}/scripts/backtest.py --strategy sma_crossover --symbol BTC-USD --period 1y
python ${CLAUDE_SKILL_DIR}/scripts/backtest.py \
--strategy rsi_reversal \
--symbol ETH-USD \
--period 1y \
--capital 10000 \ # 10000: 10 seconds in ms
--params '{"period": 14, "overbought": 70, "oversold": 30}'
- Analyze results saved to
${CLAUDESKILLDIR}/reports/ -- includes summary.txt (performance metrics), trades.csv (trade log), equity.csv (equity curve data), and chart.png (visual equity curve).
- Optimize parameters via grid search to find the best combination:
python ${CLAUDE_SKILL_DIR}/scripts/optimize.py \
--strategy sma_crossover \
--symbol BTC-USD \
--period 1y \
--param-grid '{"fast_period": [10, 20, 30], "slow_period": [50, 100, 200]}' # HTTP 200 OK
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 |
How It Works
/backtest-strategy
Testing moving average strategy:
- Symbol: BTC/USDT
- Period: 1 year
- Capital: $10,000
- Parameters: 50/200 MA
Ready to use trading-strategy-backtester?