Backtesting
AlgoTradingAI provides backtesting capabilities to evaluate trading strategies against historical data. This guide explains how backtesting works and how to interpret results.
How Backtesting Works
- Data Loading: Historical OHLCV candles are fetched from Zerodha KiteConnect (or Redis cache). Candles are aligned to IST and restricted to market hours (09:15-15:30).
- Strategy Replay: The strategy layer processes candles sequentially, generating CandidateTrade objects using pattern detection (breakout, reversal) and trend filters (SMA crossovers).
- ML Filter: Each candidate passes through the proprietary ML quality gate. Only candidates above the confidence threshold are accepted as signals.
- Trade Simulation: Accepted signals are evaluated against their stop-loss and target levels using subsequent candle data. No hindsight bias — decisions use only data available at the signal timestamp.
- Metrics Calculation: Win rate, profit factor, Sharpe ratio, max drawdown, and other metrics are computed from the set of closed simulated trades.
Backtest Assumptions
Every backtest uses a consistent set of assumptions that are always disclosed:
- Slippage: 0.05% per leg (configurable in strategy params).
- Transaction Costs: Zerodha brokerage schedule (Rs 20 per intraday order, 0 for delivery).
- Data Source: Zerodha historical candles with Redis cache fallback (7-day TTL).
- Walk-Forward: 70% training / 30% validation split. Results reported on out-of-sample period only.
- No Future Data: All indicator values and signals are computed using only data available at the decision timestamp.
AI Advisory Backtest
The AI advisory pipeline supports a separate backtest mode:
- Saved advisory decisions (from
/api/advisor/decisions) are replayed through the backtest engine. - Each decision's recommended action, size, and stop-loss are simulated against actual price movements.
- Results are compared to a Buy & Hold baseline for the same symbol and period.
- Available from the Advisor Backtest page in the sidebar.
Interpreting Results
Key Metrics
- Win Rate: Percentage of trades that hit profit target before stop-loss. Above 50% is generally positive, but depends on risk:reward ratio.
- Profit Factor: Gross profit / gross loss. Above 1.0 means net profitable. Above 1.5 is considered strong.
- Sharpe Ratio: Risk-adjusted return. Above 1.0 is acceptable, above 2.0 is strong.
- Max Drawdown: Largest peak-to-trough decline. Lower is better. Compare to your personal risk tolerance.
- Exposure: Percentage of time capital was deployed. Lower exposure with similar returns indicates more selective trading.
Common Pitfalls
- Overfitting: A strategy that performs exceptionally on historical data but fails on new data. Walk-forward validation helps detect this.
- Survivorship Bias: Testing only on stocks that exist today. Delisted stocks are not included in backtests.
- Slippage Underestimation: Real slippage in illiquid options can be significantly higher than the default 0.05%.
Next Steps
- Indicators & Models — Understand the signals feeding into strategies.
- Performance Methodology — How we report results publicly.
- Risk Disclosure — Important limitations and disclaimers.