Backtesting and risk analysis for a rule-based strategy
This project implements a 50/200 moving-average crossover strategy on Apple stock data and evaluates performance with Backtrader and PyFolio-style metrics.
Challenge
- A profitable strategy can still underperform a passive benchmark.
- Risk-adjusted metrics and drawdown behavior matter more than final return alone.
- Backtests need assumptions about commission, position sizing, and trading frequency.
System architecture
Data and inputs
AAPL daily data from 2019-01-01 to 2024-12-31 with open, high, low, close, and volume fields.
Technical approach
- Buy when the 50-day SMA crosses above the 200-day SMA.
- Sell when the 50-day SMA crosses below the 200-day SMA.
- Use 95% of available cash on entry, no leverage, and 0.1% commission.
- Compare performance against buy-and-hold AAPL.
Evaluation and results
Starting value $100,000
Total return 16.23%
Maximum drawdown 15.73%
- The strategy ended at $116,229.76 from $100,000, with 16.23% total return.
- Maximum drawdown was 15.73%.
- Buy-and-hold AAPL delivered higher total return, which makes benchmark comparison essential.
Implementation and code
Implementation focus
The implementation connects data preparation, modeling, evaluation, and interpretation in a structured workflow that makes the technical decisions clear.
Source code
The code is available for exploring the implementation details and extending the experiment when needed.
Scope and responsible use
The analysis is intended for modeling and evaluation, not investment advice. Real trading use would require risk controls, transaction-cost modeling, out-of-sample validation, and continuous monitoring.
Future development
- Add transaction-cost sensitivity.
- Test other lookback windows and assets.
- Add walk-forward evaluation before interpreting robustness.
Technical contribution
The project emphasizes benchmark comparison, drawdown analysis, and risk-adjusted interpretation in strategy evaluation.