In: Algorithmic Trading

Backtesting tests a trading idea on historical Indian market data to estimate performance before risking capital. Use clean, survivorship-bias-free data, realistic costs/slippage, and an out-of-sample validation process. Avoid look-ahead bias and curve-fitting. Implement walk-forward testing and Monte Carlo to assess robustness.


Why backtesting matters

Backtests help you separate luck from edge. Done right, they reveal if a strategy survives real-world frictions in India—tick sizes, lot sizes, STT/GST, exchange fees, and liquidity. Done wrong, they produce fragile results that collapse in live trading.


What is backtesting?

Backtesting simulates trades using historical prices and rules. Key outputs:

  • CAGR: CAGR=(Ending ValueStarting Value)1/n−1\text{CAGR} = \left(\frac{\text{Ending Value}}{\text{Starting Value}}\right)^{1/n} – 1
  • Volatility (annualised): σ=252×stdev(daily returns)\sigma = \sqrt{252}\times \text{stdev(daily returns)}
  • Sharpe Ratio: Sharpe=Return−Rfσ\text{Sharpe} = \frac{\text{Return} – R_f}{\sigma}
  • Sortino: Sortino=Return−Rfσdown\text{Sortino} = \frac{\text{Return} – R_f}{\sigma_{\text{down}}}
  • Max Drawdown: largest peak-to-trough fall
  • Calmar: Calmar=CAGR∣Max DD∣\text{Calmar}=\frac{\text{CAGR}}{|\text{Max DD}|}
  • Expectancy per trade: E=p×Wˉ−(1−p)×LˉE = p \times \bar{W} – (1-p)\times \bar{L}

The backtesting workflow (step-by-step)

  1. Define the hypothesis
    E.g., “NIFTY 50 mean-reverts on 1% intraday spikes,” or “BankNifty momentum over 3–5 days.”
  2. Assemble the right data
    • Instruments: equities (cash), F&O (index & stock), commodities, currency futures.
    • Granularity: daily/EOD for swing; minute/second for intraday.
    • Adjustments: splits/bonus/dividends (cash equities), continuous futures rolls (F&O).
    • Meta: lot sizes, tick size, freeze quantities, trading hours (09:15–15:30), holidays.
  3. Pre-process
    Align timestamps to IST, handle corporate actions, filter auctions/pre-open if your logic needs it, remove halts/circuit days prudently.
  4. Specify trading rules
    Entry, exit, position sizing, stop loss, target, time-based exits, re-entry constraints.
  5. Model execution
    • Apply slippage (e.g., 5–20 bps), partial fills, market impact for higher turnover.
    • Use lot sizes for F&O; ensure margin feasibility.
  6. Include full costs
    Brokerage, STT, exchange tx charges, SEBI fees, GST on brokerage/tx charges, stamp duty, and DP charges (for delivery). Parameterise by broker.
  7. Evaluate
    Report the metrics above with turnover, trade count, average holding period, exposure.
  8. Validate
    • Train / Test split (chronological).
    • Walk-forward: re-fit parameters on rolling windows; test on the next segment.
    • Monte Carlo / bootstrap trades to stress sequencing risk.
  9. Dry run
    Paper trade or very small capital with live quotes to compare backtest vs live.

Data for Indian markets: what to watch

  • Survivorship-bias-free universes: include delisted/merged stocks when testing long history.
  • Corporate actions: use back-adjusted prices for signals; book P&L with cash dividends as income.
  • Futures rolls: define a roll rule (e.g., volume-based or 3 days before expiry).
  • Quality flags: remove zero/negative trades, stale bars, and outliers; cap returns on known fat-finger events only with documentation.
  • Liquidity filters: minimum median bid-ask/volume; avoid freeze breaches.
  • Trading calendars: NSE/BSE holidays, special sessions, pre-open quirks for IPO listings and rebalances.

Tools & platforms (pick by complexity)

NeedGood fitNotes
Point-and-click EOD testsAmibroker, ExcelFast for factor screens; code light.
Python backtestingbacktrader, vectorbt, pandasFlexibility; best for research pipelines.
Event-driven intradaybacktrader + custom execution; Zipline-like frameworksAdd latency/slippage models.
R/Quantitativequantstrat, PerformanceAnalyticsStrong for portfolio stats.
Cloud & databasesClickHouse, PostgreSQL, ParquetScales to minute/second data.
Broker connectivityZerodha (Kite Connect), Dhan, FYERS, Alice Blue APIsFor paper/live to compare with backtests.

Tip: Keep research code separate from execution code to avoid implicit look-ahead in live systems.


Metrics that matter (beyond Sharpe)

  • Hit rate vs payoff ratio (a 40% hit rate can work if winners >> losers).
  • Capacity/impact: P&L vs capital; slippage grows non-linearly.
  • Turnover & costs: effective bps per trade × trades per year = drag on CAGR.
  • Skew/kurtosis: avoid strategies with deep left-tail risk hidden by high average returns.
  • Exposure & correlation: beta to NIFTY, sector concentration, options Greeks (for options books).

Common pitfalls (and fixes)

  1. Look-ahead bias
    Using future info (e.g., today’s close to decide a trade “at” the close).
    Fix: generate signals from data available before the trade; trade on next bar.
  2. Survivorship bias
    Testing only current NIFTY 50 constituents.
    Fix: use historical index memberships; include delisted names.
  3. Data snooping / overfitting
    Trying many parameters till Sharpe peaks.
    Fix: limit degrees of freedom, use out-of-sample and combinatorial purged CV; prefer simple rules.
  4. Leakage from feature engineering
    Using full-sample normalisation or forward-filled fundamentals.
    Fix: rolling transforms only; lag fundamental data to account for reporting dates.
  5. Unrealistic costs & fills
    Assuming mid-price fills, zero impact, or retail brokerage only.
    Fix: model bid/ask, partial fills, and full Indian cost stack (STT, GST, stamp duty, etc.).
  6. Ignoring margins & risk
    Oversized positions that would violate SPAN/VAR or broker RMS.
    Fix: apply exchange margin models or conservative haircuts; test with capital constraints.
  7. No warm-up
    Indicators computed from insufficient history.
    Fix: burn-in period (e.g., 252 days for yearly signals).
  8. Regime blindness
    Strategy dies in high-vol regimes.
    Fix: add volatility filters or regime switches; test across 2013 taper, 2020 crash, 2022 tightening, 2024–25 cycles.

Designing realistic Indian cost assumptions

Include these in every run (illustrative figures—parameterise in your code):

  • Brokerage: flat/volume-based (often low for F&O, higher for cash).
  • STT/CTT: on equity delivery & options (on sell side for F&O).
  • Exchange txn charges & clearing.
  • SEBI turnover fees.
  • GST on brokerage and txn charges.
  • Stamp duty (state-wise) on buy side.
  • DP charges (for delivery).
  • Slippage: start with 5–10 bps for liquid cash and 10–25 bps for mid-caps; options vary widely.
  • Impact: scale with order size ÷ ADV; cap participation (e.g., ≤5% of ADV).

Validation & robustness

  • Walk-forward optimisation: split 2–3 year train / next-year test, roll forward.
  • White’s Reality Check / Deflated Sharpe: discount Sharpe after multiple trials.
  • Monte Carlo of trade sequences: shuffle trade order to see P&L range.
  • Parameter stability: heatmaps across lookbacks/thresholds; prefer wide plateaus over sharp peaks.
  • Stress tests: widen spreads, add latency (e.g., 250–500 ms), ban trades in first/last 5 minutes, add gap risks.

Featured snippet: backtesting checklist

  • Clean, survivorship-free data with proper corporate actions.
  • Signals computed with no look-ahead; trade next bar only.
  • Full Indian cost stack + realistic slippage & partial fills.
  • Position sizing respects lot size, tick size, margins, and freeze limits.
  • Out-of-sample and walk-forward validation.
  • Risk metrics: Max DD, Calmar, tail risk, turnover.
  • Documentation & versioning of data, parameters, and results.

FAQs

Q1. How long should a backtest be?
At least one full market cycle—ideally 8–10 years for equities; for high-frequency intraday, aim for multiple volatility regimes and structural changes.

Q2. Which is better: EOD or intraday backtesting?
Depends on edge and capacity. EOD is simpler, cheaper, and more scalable. Intraday may offer more signals but is sensitive to microstructure and costs.

Q3. How do I model slippage in India?
Use bid/ask when available. Else, penalise fills by a spread fraction (e.g., 25–50% of prevailing spread) plus an impact function increasing with participation rate.

Q4. Can I combine ML with backtesting?
Yes—use nested cross-validation, strict temporal splits, and avoid leakage. Prioritise interpretability and stable features.


Conclusion

Backtesting is the bridge between an idea and deployable capital. For Indian markets, success hinges on clean data, honest execution assumptions, and robust validation. Follow the workflow, avoid the pitfalls, and your live results are far likelier to resemble your backtests.

Leave a Reply

Your email address will not be published. Required fields are marked *