Project 3: Simple Moving Average Crossover Backtester
Build a backtester that evaluates an SMA crossover strategy.
Project Overview
| Attribute | Value |
|---|---|
| Difficulty | Level 2: Intermediate |
| Time Estimate | Weekend |
| Main Language | Python |
| Alternative Languages | R, JavaScript |
| Knowledge Area | Backtesting |
| Tools | CSV data, plotting |
| Main Book | “Quantitative Trading” by Ernest Chan |
What you’ll build: A simulator that generates buy/sell signals based on two moving averages and evaluates returns.
Why it teaches quant: You learn to separate signals from execution and measure strategy performance.
Core challenges you’ll face:
- Defining signal logic cleanly
- Preventing lookahead bias
- Computing returns and drawdowns
Real World Outcome
You will run a backtest and get equity curves and summary metrics.
Example Output:
$ python backtest.py --symbol AAPL --fast 20 --slow 50
Total return: 18.2%
Max drawdown: -12.4%
Sharpe: 0.81
Saved equity curve to charts/AAPL_sma.png
Verification steps:
- Confirm signals align with moving average crosses
- Check that trades occur on the next bar
The Core Question You’re Answering
“How do I test a trading rule without accidentally cheating with future data?”
Backtesting is only useful if it avoids bias.
Concepts You Must Understand First
Stop and research these before coding:
- Moving averages
- How do fast and slow averages generate signals?
- Book Reference: “Quantitative Trading” by Ernest Chan, Ch. 4
- Lookahead bias
- Why do you trade at the next bar instead of the same bar?
- Book Reference: “Advances in Financial Machine Learning” by Marcos Lopez de Prado, Ch. 3
- Performance metrics
- How do you compute drawdown and Sharpe ratio?
- Book Reference: “Quantitative Trading” by Ernest Chan, Ch. 7
Questions to Guide Your Design
- Signal timing
- When exactly does a crossover trigger a trade?
- How will you handle missing data?
- Transaction costs
- Will you include commissions or slippage?
- How do costs change results?
Thinking Exercise
Signal Timing
Given a fast MA crossing above a slow MA at today’s close, should you buy today or tomorrow? Why?
Questions while working:
- How does trading on the same bar create bias?
- How do you handle market open vs close prices?
The Interview Questions They’ll Ask
Prepare to answer these:
- “What is lookahead bias?”
- “How do you compute drawdown?”
- “Why does Sharpe ratio matter?”
- “How do transaction costs affect strategy results?”
- “What is overfitting in backtesting?”
Hints in Layers
Hint 1: Starting Point Compute MAs and plot them over price.
Hint 2: Next Level Generate signals and shift them by one bar.
Hint 3: Technical Details Calculate returns only when in position.
Hint 4: Tools/Debugging Print trade log to verify entry/exit timing.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| MA strategies | “Quantitative Trading” by Ernest Chan | Ch. 4 |
| Bias | “Advances in Financial Machine Learning” by Marcos Lopez de Prado | Ch. 3 |
| Metrics | “Quantitative Trading” by Ernest Chan | Ch. 7 |
Implementation Hints
- Keep data aligned by index to avoid shifts.
- Add a trade log for inspection.
- Include transaction cost settings.
Learning Milestones
- First milestone: You can generate MA crossover signals.
- Second milestone: You can backtest without lookahead bias.
- Final milestone: You can interpret performance metrics correctly.