Education only. This is not investment advice and nothing here is a buy or sell recommendation. Trading carries risk of loss, and past results do not predict the future. Backtests are hypothetical: practise on paper only, never connect broker API keys or place live orders from these lessons. For personal advice, talk to a SEBI-registered Investment Adviser.
By the end of this lesson you will know what a backtest can and cannot tell you, write a trading rule exact enough to test, and have a Python project ready with safe rules for your AI agent.
You need: a laptop with Python 3 installed, a coding agent (Claude Code, Codex or OpenCode; see the setup guides), the basics from Run a project · Time: 9 minutes
A backtest replays a written rule on past prices and asks: if someone had followed this rule exactly, with real costs, what would have happened?
It can tell you:
- whether the rule did anything different from simply holding, after costs,
- how bad the worst fall was (the drawdown), and how often the rule traded,
- whether your idea even makes sense once written down exactly.
It cannot tell you what will happen next. Markets change, and a rule that worked in one period often fails in the next. A backtest is a hypothesis test, not a forecast.
Why?SEBI found that 7 in 10 individual intraday traders lost money in FY23, and 87.7% of individual equity-derivatives traders made net losses in FY26. Many of them traded ideas that "looked right" on a chart. Testing honestly, before believing, is the skill this course teaches.
CarefulEducation only. This is not investment advice, and no index or stock here is a recommendation. We use NIFTY 50 data only as test data. Nothing in this course connects to a broker or places orders.
A computer cannot test "buy when the trend looks strong". It needs every decision spelled out. Use these rows:
- Data: which prices, which timeframe, which dates.
- Entry: the exact condition.
- Exit: the exact condition.
- Size: how much is in the trade.
- Fill: at which price the trade happens, and when.
- Costs: brokerage, taxes, charges and slippage per trade.
Our example rule for the whole course: on daily NIFTY 50 closes from 2015, be "in" when the 20-day simple moving average is above the 50-day one, and "out" otherwise. The numbers are examples to test the code, not recommendations.
Your turn Rewrite this idea as a rule a computer can test
Weak version
test if moving average crossover works on nifty
What you type stays in this browser. It is never sent anywhere.Why?Half of all "great strategies" fall apart at this step: once the rule is exact, it is often different from what the person thought they were doing. Writing it down is already the first test.
Make a folder with its own virtual environment (a private Python for this project, so packages don't clash with other projects) and install three packages: pandas (tables), yfinance (free price data) and matplotlib (charts).
Terminal · macOS / Linuxmkdir backtest-lab cd backtest-lab python3 -m venv .venv .venv/bin/python -m pip install pandas yfinance matplotlibPowerShell · Windowsmkdir backtest-lab cd backtest-lab python -m venv .venv .venv\Scripts\python -m pip install pandas yfinance matplotlibWe call the project's own Python directly (
.venv/bin/pythonor.venv\Scripts\python), so you never need to "activate" anything. Check that it worked:Why?Your agent will run scripts many times. A private environment means the same versions every time, and nothing you install here can break another project.
Open your coding agent in the
backtest-labfolder and ask it to save a rules file. You learned about rules files in Rules file and prompt library:AGENTS.mdfor Codex and OpenCode,CLAUDE.mdfor Claude Code.Prompt · copy it into your AI agentCreate an AGENTS.md file in this folder with these rules, then show it to me before saving: # Backtest lab rules - This project is for learning only. It tests rules on past data. It is not investment advice. - Never connect to a broker, never use broker API keys, never place or simulate live orders. - Run Python only with .venv/bin/python (Windows: .venv\Scripts\python). Ask before installing any package. - Download price data once into a CSV in the data/ folder; all scripts read the CSV, not the internet. - No look-ahead: a signal computed from a day's close can only affect returns from the next day. - Every backtest subtracts costs per trade and prints a check table of 5 rows that I can verify in Excel. - Explain every script in simple English before running it.
Do it now Do it now
Why?Rules in a file are repeated to the agent every session. "No look-ahead" and "costs on every trade" are exactly the mistakes AI-written backtests make most often, so you forbid them once, in writing.
- A backtest replays an exact rule on past prices with costs; it is a hypothesis test, not a forecast or advice.
- Write every rule with six rows: Data, Entry, Exit, Size, Fill, Costs.
- One folder, one
.venv, call.venv/bin/python(or.venv\Scripts\python) directly. - Save project rules for your agent: no brokers, no look-ahead, costs on every trade, a check table every time.
- Next: Get price data and check it.
Create a free account to save your progress and open every lesson. Create free account