Backtesting Guide
Backtesting lets you evaluate a trading strategy against historical market data before risking real capital. KlawTrade includes a built-in backtesting engine that replays past price action, simulates order fills, and produces detailed performance reports -- all from a single CLI command.
Why Backtesting Matters
Every profitable strategy looks great on paper. Backtesting reveals whether that edge holds up across different market conditions -- trending, range-bound, and volatile environments. Without backtesting, you are trading blind.
- Validate assumptions -- confirm that your entry and exit logic generates alpha
- Quantify risk -- measure max drawdown, volatility, and tail losses before going live
- Compare strategies -- run multiple backtests side by side to choose the best approach
- Build confidence -- deploy capital knowing the strategy has survived years of historical data
Running a Backtest
Use the klawtrade backtest command to run a strategy against historical data. The engine downloads price data automatically if it is not already cached locally.
klawtrade backtest --strategy momentum --start 2022-01-01 --end 2024-12-31You can target specific tickers, set an initial capital amount, and choose a data source:
klawtrade backtest \
--strategy momentum \
--tickers AAPL,MSFT,NVDA \
--capital 100000 \
--start 2022-01-01 \
--end 2024-12-31 \
--data-source alpacaThe backtest runs locally using the same execution engine as live trading, so fills, slippage modeling, and commission calculations match production behavior.
Backtest Configuration
For more control, define backtest parameters in your config/settings.yaml file. See the Configuration guide for the full schema.
backtest:
strategy: momentum
tickers:
- AAPL
- MSFT
- NVDA
start_date: "2022-01-01"
end_date: "2024-12-31"
initial_capital: 100000
commission: 0.001 # 0.1% per trade
slippage: 0.0005 # 0.05% slippage model
data_source: alpaca
benchmark: SPYInterpreting Results
After a backtest completes, KlawTrade prints a performance summary and saves a detailed HTML report to output/backtest_report.html. Key metrics to focus on:
The HTML report also includes an interactive equity curve, a drawdown chart, a trade-by-trade log, and a monthly returns heatmap.
Example: Momentum Strategy on AAPL, MSFT, NVDA
Here is a sample backtest command and the type of summary output you can expect. This runs the built-in momentum strategy across three large-cap tech stocks over a three-year window.
$ klawtrade backtest --strategy momentum --tickers AAPL,MSFT,NVDA \
--capital 100000 --start 2022-01-01 --end 2024-12-31
Backtest Complete
─────────────────────────────────────────
Strategy: Momentum
Period: 2022-01-01 to 2024-12-31
Tickers: AAPL, MSFT, NVDA
Initial Capital: $100,000.00
─────────────────────────────────────────
Total Return: 42.3%
Sharpe Ratio: 1.87
Sortino Ratio: 2.41
Max Drawdown: -14.2%
Win Rate: 58.4%
Profit Factor: 1.92
Total Trades: 347
Avg Trade Duration: 4.2 days
─────────────────────────────────────────
Report saved to output/backtest_report.htmlAvoiding Overfitting
A strategy that performs perfectly on historical data but fails in live trading is overfitted. Follow these guidelines to keep results realistic:
- Use out-of-sample testing -- reserve the last 20% of your data as a holdout set that the strategy never sees during development
- Limit parameters -- the more tunable knobs a strategy has, the easier it is to curve-fit noise instead of signal
- Test across multiple tickers -- a robust strategy works on a basket of instruments, not just one
- Walk-forward analysis -- retrain on rolling windows instead of a single fixed period
- Paper trade first -- run in simulation mode for at least 30 days before allocating real capital. See the Getting Started guide for simulation setup
Walk-Forward Analysis
KlawTrade supports walk-forward analysis out of the box. Add the --walk-forward flag to split the date range into rolling train/test windows:
klawtrade backtest --strategy momentum --tickers AAPL,MSFT,NVDA \
--capital 100000 --start 2022-01-01 --end 2024-12-31 \
--walk-forward --train-months 6 --test-months 2This produces a combined report showing performance across all out-of-sample windows, giving a more realistic view of how the strategy would perform on unseen data.
Next Steps
- Strategies -- learn about the built-in momentum and mean reversion strategies
- Risk Management -- understand how the 14-check risk gate protects your capital
- Circuit Breakers -- configure the 7-trigger safety system for live trading
- CLI Reference -- explore all available backtest flags and options