A comprehensive Python framework for backtesting quantitative trading strategies with support for multiple strategies, performance metrics, and automated reporting.
- Multiple Trading Strategies: 8 different strategies with distinct approaches
- Comprehensive Metrics: Sharpe ratio, maximum drawdown, returns, volatility
- Automated Reporting: HTML reports with interactive charts and performance visualizations
- Batch Processing: Run multiple strategies across different parameters
- Data Management: Automated data fetching and caching
- CLI Interface: Easy-to-use command-line tools
- Jupyter Notebooks: Detailed analysis notebooks for each strategy
Backtesting-Framework-QuantTrading/
├── qb/ # Core backtesting engine
│ ├── backtester.py # Main backtesting logic
│ ├── data.py # Data loading and management
│ ├── metrics.py # Performance calculations
│ └── strategy.py # Strategy base classes
├── strategies/ # Strategy configurations (YAML files)
│ ├── buy_and_hold.yaml
│ ├── sma_crossover.yaml
│ ├── rsi.yaml
│ ├── bollinger.yaml
│ ├── MA200.yaml
│ ├── momentum.yaml
│ ├── atr_trailing.yaml
│ └── donchian.yaml
├── scripts/ # Utility scripts
│ ├── fetch_data.py # Data fetching
│ ├── generate_report.py # Report generation
│ ├── run_all_strategies.py # Batch processing
│ ├── create_notebooks.py # Jupyter notebook generation
│ └── strategy_comparison.py # Strategy comparison
├── tests/ # Test suite
│ ├── test_strategies.py # Strategy tests
│ └── test_backtester.py # Backtester tests
├── notebooks/ # Jupyter notebooks for analysis
├── cli/ # Command-line interface
│ └── run_batch.py # Batch execution CLI
├── data/ # Stock data (gitignored)
├── reports/ # Generated reports (gitignored)
├── main.py # Main entry point
├── setup.py # Package setup
└── CONTRIBUTING.md # Contributing guidelines
# Install dependencies
pip install -r requirements.txt
# Fetch stock data
python main.py fetch-data
# Run all strategies on all stocks
python main.py run-all
# Compare all strategies on a specific stock
python main.py compare --stock GOOGL
# Generate comprehensive HTML report
python main.py report
# Run tests
python main.py test# Install dependencies
pip install -r requirements.txt
# Fetch data
python scripts/fetch_data.py
# Run a single strategy
python cli/run_batch.py --strategy sma_crossover --symbol SPY
# Run all strategies
python scripts/run_all_strategies.py
# Generate report
python scripts/generate_report.py| Strategy | Type | Description | Key Parameters |
|---|---|---|---|
| Buy & Hold | Baseline | Buy once at the beginning and hold until the end | Allocate: 100% |
| SMA Crossover | Trend Following | Buy when fast MA crosses above slow MA (golden cross) | Fast: 20d, Slow: 50d |
| RSI | Mean Reversion | Buy when oversold (RSI < 30), sell when overbought (RSI > 70) | Period: 14d, Thresholds: 30/70 |
| Bollinger Bands | Mean Reversion | Buy at lower band, sell at upper band | Window: 20d, Std Dev: 2.0 |
| MA200 | Trend Following | Buy above 200-day MA, sell below 200-day MA | Window: 200d |
| Momentum | Trend Following | Buy on positive momentum, sell on negative momentum | Lookback: 90d |
| ATR Trailing Stop | Risk Management | Dynamic trailing stops using ATR for position sizing | Window: 14d, Multiplier: 3.0 |
| Donchian Channel | Breakout | Buy on upper channel breakout, sell on lower channel breakout | Window: 20d, Tolerance: 1% |
The simplest strategy that buys at the beginning and holds until the end. This serves as a baseline comparison for all other strategies. It captures the natural market appreciation without any trading costs or timing decisions.
How it works: Allocates 100% of capital at the start and never sells, capturing the full market return.
A classic trend-following strategy that uses two moving averages to identify trend changes. When the fast moving average crosses above the slow moving average, it signals an uptrend (buy). When it crosses below, it signals a downtrend (sell).
How it works:
- Calculates 20-day and 50-day simple moving averages
- Generates buy signal when fast MA crosses above slow MA (golden cross)
- Generates sell signal when fast MA crosses below slow MA (death cross)
The Relative Strength Index strategy exploits overbought and oversold conditions. It buys when the asset is oversold (RSI < 30) and sells when it's overbought (RSI > 70), assuming prices will revert to the mean.
How it works:
- Calculates RSI over 14 periods
- Buys when RSI drops below 30 (oversold condition)
- Sells when RSI rises above 70 (overbought condition)
Uses Bollinger Bands to identify when prices have moved too far from their moving average. The strategy buys when prices touch the lower band and sells when they touch the upper band.
How it works:
- Calculates 20-day moving average and 2 standard deviation bands
- Buys when price touches or crosses below the lower band
- Sells when price touches or crosses above the upper band
A long-term trend following strategy that uses the 200-day moving average as a major support/resistance level. It's based on the principle that prices above the 200-day MA indicate a bullish trend.
How it works:
- Calculates 200-day simple moving average
- Buys when price is above the 200-day MA
- Sells when price falls below the 200-day MA
Captures momentum by comparing current prices to historical prices. It buys when the asset shows positive momentum and sells when momentum turns negative.
How it works:
- Calculates 90-day price momentum (current price / price 90 days ago)
- Buys when momentum is positive (price higher than 90 days ago)
- Sells when momentum is negative (price lower than 90 days ago)
A sophisticated risk management strategy that uses Average True Range (ATR) to set dynamic trailing stops. It adjusts position sizes based on volatility and uses trailing stops to protect profits.
How it works:
- Calculates ATR over 14 periods to measure volatility
- Uses 3x ATR multiplier for trailing stop distance
- Adjusts position size inversely to volatility
- Trails stops to lock in profits while allowing for volatility
A breakout strategy that identifies when prices break out of their recent trading range. It buys on breakouts above the upper channel and sells on breakouts below the lower channel.
How it works:
- Calculates 20-day high and low channels
- Buys when price breaks above the upper channel (with 1% tolerance)
- Sells when price breaks below the lower channel (with 1% tolerance)
Strategies are configured using YAML files in the strategies/ directory. Each strategy can be customized with different parameters like:
- Initial cash amount
- Lookback periods
- Threshold values
- Risk management rules
The framework generates comprehensive HTML reports including:
- Performance charts
- Risk-return scatter plots
- Sharpe ratio heatmaps
- Strategy comparisons
Reports are automatically saved to the reports/ directory with timestamps.
Here are some example results from running our strategies on popular stocks:
Our framework generates comprehensive comparison charts showing how different strategies perform:
- Equity Curves: Visual comparison of cumulative returns
- Risk-Return Scatter: Sharpe ratio vs. total return analysis
- Performance Metrics: Detailed statistics for each strategy
- Drawdown Analysis: Maximum drawdown and recovery periods
- Trend Following strategies (SMA Crossover, MA200, Momentum) tend to perform well in trending markets
- Mean Reversion strategies (RSI, Bollinger Bands) excel in sideways markets
- Risk Management strategies (ATR Trailing Stop) provide better downside protection
- Breakout strategies (Donchian Channel) capture major price movements
Strategy | Total Return | Sharpe Ratio | Max Drawdown
------------------|--------------|--------------|-------------
Buy & Hold | 45.2% | 0.85 | -18.3%
SMA Crossover | 52.1% | 1.12 | -12.7%
RSI | 38.9% | 0.78 | -15.2%
Bollinger Bands | 41.3% | 0.91 | -13.8%
MA200 | 48.7% | 1.05 | -14.1%
Momentum | 55.3% | 1.18 | -11.9%
ATR Trailing Stop | 43.8% | 1.02 | -9.4%
Donchian Channel | 47.2% | 1.08 | -13.5%
Ready to explore quantitative trading strategies? Here's how to get started:
# Clone the repository
git clone https://github.com/KarlNueckel/Backtesting-Framework-QuantTrading.git
cd Backtesting-Framework-QuantTrading
# Install dependencies
pip install -r requirements.txt
# Run a quick comparison on GOOGL
python main.py compare --stock GOOGL
# Generate a comprehensive report
python main.py report- Interactive HTML reports with performance charts
- Strategy comparison tables with key metrics
- Jupyter notebooks for detailed analysis
- Trade-by-trade breakdown for each strategy
- Risk analysis including drawdown and volatility metrics
- Add your own stocks: Modify the data fetching script
- Create new strategies: Follow our contributing guidelines
- Adjust parameters: Edit YAML configuration files
- Extend metrics: Add custom performance calculations
- Students learning quantitative finance
- Researchers testing trading hypotheses
- Developers building algorithmic trading systems
- Traders backtesting strategies before live trading
- Educators teaching financial modeling concepts
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a feature branch
- Add your strategy or improvements
- Submit a pull request
See our Contributing Guidelines for detailed instructions.
- Strategy Development: Check out our Jupyter notebooks for detailed analysis
- Performance Metrics: Understand Sharpe ratios, drawdowns, and risk measures
- Backtesting Best Practices: Learn about proper backtesting methodology
- Risk Management: Explore position sizing and stop-loss strategies
This project is open source and available under the MIT License.
Ready to start your quantitative trading journey? 🚀📈
Clone the repository and run your first backtest today!