A beginner-friendly quantitative trading system implementing an RSI (Relative Strength Index) mean-reversion strategy.
This project is designed to teach the fundamentals of quantitative trading through a hands-on implementation. It includes:
- Data Fetching: Download historical market data from Yahoo Finance
- Technical Indicators: Calculate RSI and generate trading signals
- Backtesting Engine: Simulate trading on historical data
- Performance Metrics: Calculate Sharpe ratio, drawdown, and more
- Visualization: Generate comprehensive charts
testQuant/
├── config.py # Configuration parameters
├── main.py # Main entry point
├── requirements.txt # Python dependencies
├── README.md # This file
├── data/
│ ├── raw/ # Cached raw data
│ └── processed/ # Processed data
├── src/
│ ├── data/
│ │ ├── fetcher.py # Data downloading
│ │ └── processor.py # Data cleaning
│ ├── strategy/
│ │ ├── indicators.py # Technical indicators (RSI, SMA, etc.)
│ │ └── rsi_strategy.py # RSI trading strategy
│ ├── backtest/
│ │ ├── engine.py # Backtesting simulation
│ │ └── metrics.py # Performance calculations
│ └── visualization/
│ └── plots.py # Chart generation
├── notebooks/ # Jupyter notebooks for exploration
├── results/ # Saved charts and reports
└── tests/ # Unit tests
pip install -r requirements.txtpython main.py# Test on different stock
python main.py --symbol AAPL --start 2022-01-01 --end 2023-12-31
# Adjust RSI parameters
python main.py --rsi-period 14 --oversold 25 --overbought 75
# Change capital and commission
python main.py --capital 50000 --commission 0.002RSI (Relative Strength Index) measures the speed and magnitude of price changes on a scale of 0-100.
- RSI < 30: Oversold condition → Potential buying opportunity
- RSI > 70: Overbought condition → Potential selling opportunity
- Buy Signal: When RSI crosses above 30 (leaving oversold zone)
- Sell Signal: When RSI crosses below 70 (leaving overbought zone)
- Stop-Loss: Exit if position loses more than 5% (configurable)
RSI = 100 - (100 / (1 + RS))
Where:
RS = Average Gain / Average Loss (over N periods)
| Metric | Description | Good Value |
|---|---|---|
| Total Return | Overall profit/loss | Higher is better |
| Sharpe Ratio | Risk-adjusted return | > 1.0 is good, > 2.0 is excellent |
| Max Drawdown | Largest peak-to-trough decline | Lower is better (< 20% ideal) |
| Win Rate | % of profitable trades | > 50% is good, but not required |
| Profit Factor | Gross profit / Gross loss | > 1.5 is good |
- Backtest Results: Price chart with signals, RSI, portfolio value, drawdown
- RSI Signals: Zoomed view of recent signals
- Monthly Returns: Heatmap showing performance by month
- Trade Analysis: Distribution of trade P&L
Edit config.py to change default parameters:
# Trading symbol
SYMBOL = "SPY"
# Date range
START_DATE = "2020-01-01"
END_DATE = "2024-12-31"
# RSI parameters
RSI_PERIOD = 14
RSI_OVERSOLD = 30
RSI_OVERBOUGHT = 70
# Backtest settings
INITIAL_CAPITAL = 100_000
COMMISSION_PCT = 0.001 # 0.1%
STOP_LOSS_PCT = 0.05 # 5%This project teaches:
- Data Handling: Working with financial time series data
- Technical Analysis: Understanding and implementing indicators
- Backtesting: Simulating trading strategies historically
- Risk Management: Position sizing, stop-losses
- Performance Analysis: Evaluating strategy effectiveness
- Python Best Practices: Clean code, documentation, modularity
- Overfitting: A strategy tuned too closely to historical data will fail live
- Look-Ahead Bias: Never use future data in decisions
- Survivorship Bias: Historical data may exclude failed companies
- Transaction Costs: Always include commissions and slippage
- Market Impact: Large orders can move prices against you
Ideas for improvement:
- Add More Indicators: MACD, Bollinger Bands, Moving Averages
- Combine Strategies: Use multiple indicators for confirmation
- Parameter Optimization: Find optimal RSI periods and thresholds
- Walk-Forward Testing: More robust validation technique
- Multiple Assets: Trade a portfolio of stocks
- Paper Trading: Connect to broker API for simulated live trading
MIT License - Feel free to use and modify for learning purposes.
Happy Trading! 📈🤖