LearnFinance
← Algorithmic & Systematic Trading Lesson 6 of 6 · 25 min · +50 XP
Algorithmic & Systematic Trading · Lesson 06

Project: backtest a rule honestly

Test a simple market-timing rule on 26 years of real Nifty 50 total-return data and see how easily a backtest flatters. Fix the rule before looking, split the data into in-sample and out-of-sample periods, include costs, compare against buy and hold, count how many choices you tried, and write up the result honestly. A self-check list and a model write-up let you mark your work; and how Harvey, Liu and Zhu's 2016 study of 316 published return factors argued a new one should clear a t-statistic of 3.0, not 2.

Facts reviewed Oct 2026

The backtesting lesson listed the ways backtests mislead: overfitting, look-ahead, survivorship, ignored costs, and too many variants tried. This project makes you run one, on real data. Then write it up as if your money depended on it.

The data is the Nifty 50 Total Return Index’s calendar-year returns from 2000 to 2025. They include reinvested dividends.

Nifty 50 Total Return Index: calendar-year returns
−100% −50% 0% 50% 100% 2000: −13.4%2001: −15%2002: 5.3%2003: 76.6%2004: 13%2005: 38.6%2006: 41.9%2007: 56.8%2008: −51.3%2009: 77.6%2010: 19.2%2011: −23.8%2012: 29.4%2013: 8.1%2014: 32.9%2015: −3%2016: 4.4%2017: 30.3%2018: 4.6%2019: 13.5%2020: 16.1%2021: 25.6%2022: 5.7%2023: 21.3%2024: 10.1%2025: 11.9% 2000200420082012201620202024
Returns include reinvested dividends. Sources differ by a few tenths in some years. Source: NSE Indices, '25 Years Journey of Nifty 50' (2000–2020); Bajaj AMC, Nifty 50 historical returns (2021–2025) (as of 2026-10).

The rule to test

A simple trend-following idea, popular in many forms:

At the end of each year, if the Nifty 50 TRI rose over that year, hold equity for the next year. If it fell, hold debt earning a fixed return instead.

Before you look at any results, write down:

  • the exact rule, including the threshold. Start with “rose at all”, or 0%;
  • the debt return you’ll assume, 7%;
  • the cost of each switch, covering brokerage, market impact and tax, 0.5%;
  • the in-sample period you’ll design on, 2001–2012, and the out-of-sample period you’ll judge on, 2013–2025; and
  • what counts as success. Say, beating buy and hold out of sample, after costs, with a smaller worst year.

Run it

Backtest: own the Nifty only after an up year
Rule: compound return a year
12.31%
Buy and hold: compound return a year
14.42%
Rule minus buy and hold (points a year)
−2.11
Switches between equity and debt
8
Worst year under the rule
−51.3%
Worst year, buy and hold
−51.3%
Rule: compound returnBuy and hold

Nifty 50 Total Return Index calendar-year returns, 2000–2025 (NSE and Bajaj AMC; see the chart above). Each year's signal is the previous year's return. Defaults: the rule compounds at about 12.3% a year against 14.4% for buy and hold over 2001–2025, with 8 switches. Test 2013–2025 alone and it narrowly wins (13.6% against 13.5%); test 2001–2012 and it loses badly.

Worked example: what the switches cost. With the default settings, the rule switches 8 times over 2001–2025, at 0.5% each.

  1. Each switch keeps 99.5% of the money.
  2. After 8 switches: 0.995⁸ ≈ 0.961.
  3. So costs take about 3.9% off the final value. That’s small next to the gap with buy and hold, so costs aren’t why the rule loses.

Your deliverable

A half-page write-up:

  1. The rule and assumptions, fixed before testing.
  2. In-sample results, for 2001–2012, against buy and hold.
  3. Out-of-sample results, for 2013–2025, against buy and hold.
  4. How many variants you tried, such as thresholds and periods, and why that matters.
  5. A conclusion: would you trade it, and how confident are you?
Quick check

You try thresholds from −30% to +30%, and the best one beats buy and hold by 1 point a year over 2001–2025. What's the honest conclusion?

Self-check list

  • The rule, costs and periods were fixed before you looked at results.
  • Signals use only information available at the time: the previous year’s return.
  • Results include switching costs, and compare against buy and hold on the same years.
  • You report both in-sample and out-of-sample results, and the worst year, not just the average.
  • You state how many variants you tried.
  • You note the sample size: one data point a year, 25 in total, and only a handful of switches.
  • Your conclusion matches the evidence, including “no edge”, if that’s what you found.
Practice 1: the model write-up. Open it only after doing your own.

Rule and assumptions, fixed in advance: hold equity next year only if this year’s TRI return was positive. Otherwise, hold debt at 7%. Charge 0.5% per switch. Design on 2001–2012, and judge on 2013–2025.

In sample, 2001–2012: the rule compounded at about 11.0% a year, against 15.4% for buy and hold. It held equity through 2008, because 2007 was a great year, so it took the full −51%. Then it sat in debt for the big rebounds of 2003 and 2009. It failed in sample.

Out of sample, 2013–2025: about 13.6% against 13.5%, with only two switches. That’s effectively no difference.

Whole period: about 12.3% against 14.4%, with eight switches, and the same worst year, −51%.

Variants: across thresholds from −30% to +30%, none clearly beats buy and hold over the whole period. Higher thresholds spend long stretches in debt, and lose several points a year. Trying them all and picking the best would be data mining.

Conclusion: with yearly data, this rule shows no edge. It doesn’t even protect against the worst year, because crashes often follow strong years. I wouldn’t trade it. A fair test of trend-following would need monthly data, many markets, and realistic costs and taxes. It would also need a reason to expect the effect to last, as the strategies lesson discusses.

Practice 2: You test 30 thresholds that have no real edge, and call a result 'significant' at the 5% level. What's the chance at least one looks significant by luck?
  1. Chance a single test stays quiet: 95%.
  2. Chance all 30 stay quiet: 0.95³⁰ ≈ 21%.
  3. Chance at least one looks significant: 1 − 0.21 ≈ 79%. On average, about 1.5 of the 30 will.

A real case: 316 factors, 2016

Finance research has the same problem as your threshold search, on a much bigger scale.

  • 2016: Campbell Harvey, Yan Liu and Heqing Zhu published “…and the Cross-Section of Expected Returns” in the Review of Financial Studies.
  • What they counted: at least 316 different factors that published papers had claimed could predict stock returns.
  • The problem: many researchers had tested many ideas on the same data. The usual bar, a t-statistic of about 2, is far too easy to clear by luck when so many tries are made.
  • Their suggestion: a new factor should clear a t-statistic of about 3.0. By that standard, they argued, many published findings were likely false.

Worked example: why the bar must rise. Say 20 researchers each test one useless factor, at the usual 5% level.

  1. Chance one test looks significant by luck: 5%.
  2. Chance at least one of the 20 does: 1 − 0.95²⁰ ≈ 64%.
  3. So a “discovery” is more likely than not, even when nothing is there. And it’s the one that gets published.

The lessons:

  • Count every test, not just the winner.
  • Raise the bar as the number of tries grows.
  • Ask for a reason, not just a result.

Going further: why this applies to you. A retail trader with a laptop can test thousands of rules in an afternoon. That’s the 316-factor problem in miniature. The defences are the ones this project uses. Decide the test before you look. Keep untouched data for the final check. Write down every variant you tried. And prefer rules with a clear economic story, such as risk, behaviour or costs, that explains why they should work. A rule with no story, found by searching, is usually just a pattern in noise.

Common mistakes

  • Choosing the test periods after seeing the results.
  • Using the current year’s return to trade in the same year.
  • Comparing with no benchmark, or a different one.
  • Reporting only the best variant.
  • Ignoring the tiny sample of yearly decisions.

Key takeaways

  • Fix the rule, costs and test periods before looking, and use only information available at the time.
  • Compare with buy and hold on the same years, after costs. Report the worst year, as well as the average.
  • Split data into in-sample and out-of-sample periods, and count every variant you try.
  • With few observations, most “edges” are noise. A backtest is better at rejecting ideas than proving them.
  • Harvey, Liu and Zhu counted 316 published factors in 2016, and argued a new one should clear a t-statistic of 3.0.

Go deeper

Complete this lesson
Pass the quiz below (60% or more) to earn +50 XP and unlock the next lesson.
Quiz not passed yet
🧠

Quiz: Project: backtest a rule honestly

5 questions · pass 60% to complete