FeaturesLearnNISMGalleryFaqPricingAboutWeb Terminal (Desktop & iOS)Get Mobile App
Algorithmic Trading 101: Strategies, Backtesting & Risk Management
πŸ“š Course Β· 10 chaptersIntermediate 3.5 hours

Algorithmic Trading 101: Strategies, Backtesting & Risk Management

A practitioner-oriented course covering the core building blocks of systematic trading: how to backtest without fooling yourself, the statistics behind mean-reversion and momentum strategies, and how to size positions and manage risk so a good strategy doesn't blow up your account.

Algorithmic Trading

Course Syllabus

1 / 10
Chapter 1 of 10

Chapter 1: Backtesting Without Fooling Yourself

1/10

Chapter 1: Backtesting Without Fooling Yourself

Every algorithmic trading journey begins with a seductive idea: "What if I buy Nifty every time it dips 1% from its 20-day high, and sell when it recovers?" You code it up, run it against five years of historical data, and the equity curve looks beautiful β€” smooth, upward-sloping, with a Sharpe ratio that would make any fund manager jealous.

Then you go live. And the strategy bleeds money.

This is the single most common experience in algorithmic trading, and it almost always comes down to one thing: your backtest was lying to you.

This chapter is about learning to distrust your own backtest until you've earned the right to trust it β€” by systematically eliminating the biases that make historical simulations look far more profitable than they will ever be in live markets.

Why this chapter matters: A backtest is not a scoreboard to be optimized. It is a scientific instrument to be stress-tested. If you don't know how a backtest can deceive you, you cannot tell the difference between a genuine edge and a statistical mirage.


1. What Is Backtesting, Really?

Backtesting is the process of simulating a trading strategy on historical data to estimate how it would have performed in the past β€” with the implicit hope that this tells us something about how it will perform in the future.

Think of it as a flight simulator for your strategy. Before you risk real capital on Bank Nifty options or a Reliance Industries swing trade, you want to know:

  • Does the strategy actually make money over time, or was one lucky quarter carrying the whole track record?
  • How large are the drawdowns (peak-to-trough declines in equity), and could you psychologically and financially survive them?
  • Is the strategy's edge consistent across different market regimes β€” the 2020 COVID crash, the 2021 bull run, the sideways chop of 2023?

But here's the uncomfortable truth: a backtest can only be as honest as the assumptions baked into it. Every shortcut you take β€” every piece of "future" information that accidentally leaks into your signal, every corporate action you forget to adjust for β€” inflates your backtest's performance and sets you up for a painful surprise in live trading.

The Scientific Method Applied to Trading

A disciplined backtesting process should mirror the scientific method:

  1. Form a hypothesis β€” e.g., "Bank Nifty tends to mean-revert intraday after a sharp opening gap caused by global cues."
  2. Test the hypothesis using historical data and clearly defined rules.
  3. Evaluate honestly β€” does the data support or refute the hypothesis?
  4. Refine or reject β€” adjust the hypothesis only with fresh data, never by reverse-engineering rules to fit what already happened.

The traders who succeed long-term are not the ones who found the most profitable backtest. They are the ones who found the most honest one.

A side-by-side comparison of two equity curves β€” the left labeled 'Backtested Performance (2019-2023)' showing a smooth, steadily rising line from β‚Ή10,00,000 to β‚Ή28,00,000 with minimal drawdown, and the right labeled 'Live Trading Performance (2024)' showing the same strategy struggling sideways with sharp drawdowns, annotated with callouts pointing to the gap between expectation and reality
πŸ“· A side-by-side comparison of two equity curves β€” the left labeled 'Backtested Performance (2019-2023)' showing a smooth, steadily rising line from β‚Ή10,00,000 to β‚Ή28,00,000 with minimal drawdown, and the right labeled 'Live Trading Performance (2024)' showing the same strategy struggling sideways with sharp drawdowns, annotated with callouts pointing to the gap between expectation and reality

2. The Four Horsemen of Backtest Deception

Most inflated backtests suffer from one or more of four well-documented pitfalls. Let's break each one down with examples rooted in Indian markets.

2.1 Look-Ahead Bias

Look-ahead bias occurs when your backtest uses information that would not have actually been available at the time a trading decision was made. It is essentially your code accidentally "seeing the future."

Classic Example: Suppose you build a strategy that says, "Buy Reliance Industries if today's high is more than 1.5% above yesterday's close." If your backtesting code checks this condition using the full day's high to decide whether to enter a position at any point during that same day, you have a serious problem β€” you don't actually know what the day's high will be until the day is over!

In live trading, at 9:20 AM you only know the price up to that moment. You cannot know the day's eventual high until the market closes. A backtest that uses the full day's OHLC (Open-High-Low-Close) bar to generate an intraday signal for that same bar is committing look-ahead bias β€” and it will always show unrealistically good performance because it's cheating.

How to detect and fix it:

  • Always ask: "At the exact moment this signal fires, would I have actually known this data point in real time?"
  • Use only data from bar t-1 (the previous, fully closed candle) to generate a signal that gets acted on in bar t.
  • The gold-standard fix is to build your backtesting engine and your live execution engine using the same codebase, fed historical data in one mode and live streaming data in the other. This structurally makes look-ahead bias impossible, because the live version of your code physically cannot access tomorrow's numbers.

2.2 Data-Snooping Bias (Overfitting)

Data-snooping bias creeps in when you test so many parameter combinations, indicator tweaks, and entry/exit rules against the same historical dataset that you eventually find a combination that performs brilliantly β€” not because it captures a real market inefficiency, but purely by chance, fitting to random noise in that specific data sample.

Classic Example: You're building a Bank Nifty options-selling strategy. You test RSI thresholds of 25, 28, 30, 32, 35... combined with different moving average lengths of 9, 14, 21, 50 days... combined with different stop-loss percentages... After 400 backtest runs, you find that "RSI(32) crossing above a 21-day EMA with a 1.8% stop-loss" produced a stellar 65% win rate from 2018–2023. Exciting β€” until you realize you basically ran a lottery with 400 tickets and picked the one winner. That specific combination worked on that specific historical noise, and there's no reason to expect it will keep working going forward.

Warning signs of data-snooping bias:

  • Your strategy has many free parameters (5+ tunable inputs is a red flag).
  • Small changes to a parameter (e.g., RSI(32) to RSI(33)) cause large swings in backtested returns β€” a genuinely robust strategy should be reasonably stable across nearby parameter values.
  • You cannot explain, in plain English, why the strategy should work from a market-structure or behavioral standpoint. If the only justification is "the backtest says so," be suspicious.

How to guard against it:

  • Prefer simple, low-parameter models. A strategy with two or three parameters is far less likely to be overfit than one with fifteen.
  • Use out-of-sample testing: split your data into an in-sample period (say, 2015–2021) for developing the strategy, and a completely untouched out-of-sample period (2022–2024) to validate it once, without further tweaking.
  • Use cross-validation: test the strategy across multiple different sub-periods (e.g., separately across 2018, 2020's COVID crash, and 2023's range-bound market) to ensure it isn't only profitable in one unusual regime.
  • The ultimate test is walk-forward testing β€” actually trading the strategy live (even with minimal capital) and seeing if it holds up on data it has truly never seen.

Note: A strategy with a high Sharpe ratio and short drawdown periods tends to survive cross-validation more easily, because there are fewer "bad" sub-periods where it could fail the test.

A scatter plot titled 'Overfitting Illustrated' showing dozens of small grey dots representing different parameter combinations tested on Nifty historical data, clustered mostly around 0% to 8% annual return, with one gold-highlighted outlier dot at 34% return labeled 'The parameter combination you would have picked β€” pure luck, not edge', with a dotted trend line showing the cluster's true average
πŸ“· A scatter plot titled 'Overfitting Illustrated' showing dozens of small grey dots representing different parameter combinations tested on Nifty historical data, clustered mostly around 0% to 8% annual return, with one gold-highlighted outlier dot at 34% return labeled 'The parameter combination you would have picked β€” pure luck, not edge', with a dotted trend line showing the cluster's true average

2.3 Survivorship Bias

Survivorship bias happens when your historical stock database only contains companies that are still listed and trading today β€” silently excluding companies that were delisted, merged, or went bankrupt along the way.

Classic Example: Imagine backtesting a "buy the worst-performing stock of the previous month from the Nifty 500 universe" strategy using a stock database that only includes companies currently in existence. Names like Yes Bank (which crashed dramatically in 2020 before its reconstruction), or smaller companies that were delisted for regulatory or financial distress reasons, may be missing from your historical universe entirely. Your backtest would have unknowingly avoided buying into companies that later went to near-zero β€” not because your strategy was smart, but because your database quietly erased the losers from history.

This is especially dangerous for:

  • Long-only mean-reversion strategies that buy beaten-down stocks hoping for a bounce β€” survivorship bias makes these look artificially profitable, since the stocks that never bounced back (and got delisted) are invisible in the data.
  • Strategies built on the BSE SmallCap or MicroCap universe, where delistings and mergers are far more common than in large-cap indices like Nifty 50.

How to guard against it:

  • Use a historical database that explicitly includes delisted and merged companies, not just current constituents.
  • If such data isn't available, be conservative: limit backtests to shorter, more recent periods where survivorship distortion has less time to compound, and treat long-only small-cap results with heavy skepticism.
  • Remember: survivorship bias is less dangerous for strategies that are short-only or long-short, since the "disappearing losers" would have been profitable short positions that are also missing from a survivorship-biased database β€” the two effects partially offset (though not perfectly).

2.4 Unrealistic Fill Assumptions

Even a backtest free of look-ahead bias, overfitting, and survivorship bias can still lie to you if it assumes you can buy and sell at prices that were never actually achievable in real market conditions.

Classic Example: Your backtest assumes you can buy Bank Nifty futures at the exact Last Traded Price (LTP) the moment your signal fires, with zero slippage. But in reality:

  • The bid-ask spread on a fast-moving instrument can be several points wide during volatile sessions (like Budget Day or an RBI policy announcement).
  • Large orders in a mid-cap or small-cap stock like a lesser-traded SME counter can move the market against you β€” you may not get filled at your intended price at all.
  • Circuit limits (upper/lower price bands) on individual stocks can freeze trading entirely, so a stop-loss order you "assumed" would execute might not fill until the circuit reopens, often much further away from your intended exit price.
  • Options strategies are particularly vulnerable: illiquid strikes (deep OTM or far-dated Bank Nifty options) can have wide spreads where the backtest's assumed mid-price is nowhere close to a realistic fill.

Other realism gaps to watch for:

  • Brokerage, STT (Securities Transaction Tax), and other transaction costs are frequently omitted from backtests "for simplicity" β€” but in a high-frequency intraday strategy, these costs can eat 30–50% of gross profits.
  • Corporate actions: stock splits, bonus issues, and dividends must be adjusted in historical price data. Forgetting to adjust for a stock split (e.g., a hypothetical 1:5 split) creates a fake, massive overnight "price crash" in your raw data that can trigger false signals.
  • Order type mismatches: a backtest that assumes a market order fills instantly at the closing price, when in reality you'd need to route a Market-on-Close order to a specific exchange, can produce results that are simply not replicable.

How to guard against it:

  • Build in a realistic slippage assumption (e.g., 0.05%–0.1% per trade for liquid instruments like Nifty futures, higher for illiquid options strikes or small-cap stocks).
  • Always deduct brokerage, STT, exchange transaction charges, and GST from every simulated trade.
  • Use adjusted price data (split- and dividend-adjusted) from a reliable data vendor.
  • For options and less liquid instruments, model fills using the bid/ask quote, not the last traded price.

3. Building a Backtest You Can Actually Trust

Putting it all together, here is a practical checklist for structuring an honest backtest:

PitfallQuick CheckFix
Look-ahead biasDoes any signal use data not yet available at decision time?Use only t-1 and earlier data for a t decision; unify backtest/live code
Data-snooping biasHow many free parameters does the strategy have?Prefer simple models; use out-of-sample and cross-validation testing
Survivorship biasDoes the historical universe include delisted/merged companies?Use survivorship-bias-free data; be cautious with small/micro-cap universes
Unrealistic fillsAre slippage, brokerage, STT, and circuit limits accounted for?Model realistic slippage and costs; use bid/ask for illiquid instruments

The Walk-Forward Test: Your Final Reality Check

No matter how carefully you've screened for the biases above, some degree of data-snooping is almost impossible to fully eliminate β€” you inevitably made some design choices while looking at historical data. This is why the final, most trustworthy validation step is walk-forward testing:

  1. Finalize your strategy rules completely β€” no more tweaking.
  2. Paper trade it forward in real time for a defined period (e.g., 1–3 months on Nifty/Bank Nifty).
  3. If paper trading looks reasonable, go live with small, real capital β€” even β‚Ή10,000–₹20,000 β€” because paper trading cannot fully capture psychological pressure or real execution friction.
  4. Compare live results against the backtest. A healthy expectation: even a well-built strategy will often show a live Sharpe ratio noticeably lower than its backtested Sharpe ratio. If live performance is wildly worse, revisit your process for one of the four biases above.

Golden Rule: If you haven't traded a strategy with real (even if small) capital, you don't actually know how it performs. Backtests and paper trading can only get you so far β€” real market friction, real emotions, and real execution always reveal something a simulation cannot.

A flowchart titled 'The Honest Backtesting Pipeline' showing five sequential boxes connected by arrows: '1. Define Hypothesis (e.g., Nifty gap-fill strategy)' β†’ '2. Build Backtest (bias checks: look-ahead, snooping, survivorship, fills)' β†’ '3. In-Sample Testing (2015-2021)' β†’ '4. Out-of-Sample Validation (2022-2024, untouched data)' β†’ '5. Walk-Forward: Paper Trade then Small Live Capital', with a red warning icon next to a branch labeled 'Fails at any stage? Return to Step 1 β€” do not tweak and re-test on the same data'
πŸ“· A flowchart titled 'The Honest Backtesting Pipeline' showing five sequential boxes connected by arrows: '1. Define Hypothesis (e.g., Nifty gap-fill strategy)' β†’ '2. Build Backtest (bias checks: look-ahead, snooping, survivorship, fills)' β†’ '3. In-Sample Testing (2015-2021)' β†’ '4. Out-of-Sample Validation (2022-2024, untouched data)' β†’ '5. Walk-Forward: Paper Trade then Small Live Capital', with a red warning icon next to a branch labeled 'Fails at any stage? Return to Step 1 β€” do not tweak and re-test on the same data'

4. Key Takeaways

  • A backtest is a hypothesis-testing tool, not a profit guarantee β€” its only job is to tell you honestly whether an idea has merit.
  • Look-ahead bias happens when future data leaks into past decisions; avoid it by strictly using only data available at the time of each signal.
  • Data-snooping bias comes from testing too many parameter combinations on the same data until something "works" by chance; guard against it with simple models, out-of-sample testing, and cross-validation.
  • Survivorship bias arises from historical databases that silently exclude delisted or bankrupt companies, inflating long-only strategy performance β€” especially dangerous in small-cap and micro-cap universes.
  • Unrealistic fill assumptions β€” ignoring slippage, brokerage, STT, circuit limits, and corporate action adjustments β€” make a backtest's numbers unachievable in the real world.
  • The only true test of a strategy is walk-forward validation: paper trading followed by small-capital live trading, compared honestly against your backtest's expectations.

Coming up in Chapter 2: Now that we know how to build a trustworthy backtest, we'll dig into the statistical toolkit for detecting genuine, tradable mean-reversion behavior in prices β€” including the Augmented Dickey-Fuller (ADF) test, the Hurst exponent, and the concept of half-life, all illustrated using real Nifty and Bank Nifty price data.