A comprehensive Python-based event-driven backtesting framework for quantitative trading strategies, featuring a pairs trading implementation.
- Event-Driven Architecture: Clean separation of concerns with Market, Signal, Order, and Fill events
- Real Market Data: Integration with Yahoo Finance API for historical data
- Pairs Trading Strategy: Statistical arbitrage using cointegration and z-score analysis
- Portfolio Management: Complete position tracking, P&L calculation, and performance metrics
- Simulated Execution: Realistic order execution with commission costs
- Extensible Design: Modular architecture for easy strategy development
├── event.py # Event system classes
├── data_handler.py # Market data fetching and management
├── strategy.py # Pairs trading strategy implementation
├── portfolio.py # Portfolio and risk management
├── execution.py # Order execution simulation
├── backtest.py # Main backtesting engine
├── requirements.txt # Python dependencies
└── README.md # Project documentation
The implemented pairs trading strategy follows these steps:
- Cointegration Test: Uses Engle-Granger test to identify cointegrated pairs
- Spread Calculation: Computes the price spread between the two assets
- Z-Score Analysis: Normalizes the spread using rolling statistics
- Signal Generation:
- Long Signal: When z-score < -2.0 (spread is oversold)
- Short Signal: When z-score > 2.0 (spread is overbought)
- Exit Signal: When |z-score| < 0.5 (spread reverts to mean)
- Clone the repository:
git clone https://github.com/vkk28/Event-Driven-Backtesting-System.git
cd Event-Driven-Backtesting-System- Install dependencies:
pip install -r requirements.txt- Run the backtest:
python3 backtest.pyModify parameters in backtest.py:
# Trading pair configuration
symbol_pair = ['AAPL', 'MSFT'] # Stocks to trade
# Backtest period
start_date = '2020-01-01'
end_date = '2023-01-01'
# Capital allocation
initial_capital = 100000.0 # Starting capital in USDStrategy parameters in strategy.py:
lookback = 100 # Rolling window for statistics
z_entry = 2.0 # Z-score threshold for entry
z_exit = 0.5 # Z-score threshold for exitThe system provides comprehensive performance analysis:
- Total Return: Overall portfolio return percentage
- Equity Curve: Time series of portfolio value
- Drawdown Analysis: Peak-to-trough decline measurements
- Sharpe Ratio: Risk-adjusted return calculation
- Trade Statistics: Win rate, average trade duration, etc.
Backtest Started...
Backtest Finished.
--- Performance ---
Total Return: 3.00%
Sharpe Ratio: 0.85
Max Drawdown: -2.1%
Total Trades: 12
Win Rate: 66.7%
Equity Curve:
AAPL MSFT cash commission total returns equity_curve
datetime
2022-12-23 13002.62 0.0 87197.38 12.0 100200.00 0.002000 1.033014
2022-12-27 12822.17 0.0 87177.83 13.0 100000.00 -0.001996 1.032941
...
- Create a new strategy class inheriting from a base strategy interface
- Implement the
calculate_signals()method - Define entry/exit logic and signal generation
- Extend the
DataHandlerclass - Implement data fetching from your preferred source
- Ensure consistent data format and event generation
- Modify the
OrderEventclass to support new order types - Update the
SimulatedExecutionHandlerfor realistic execution - Add slippage and market impact models
Run the test suite to verify system integrity:
python -m pytest tests/- pandas: Data manipulation and analysis
- numpy: Numerical computing
- yfinance: Yahoo Finance API wrapper
- statsmodels: Statistical analysis and econometrics
- matplotlib: Plotting and visualization
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by quantitative trading frameworks and academic research in statistical arbitrage
- Built using modern Python financial libraries and best practices
- Event-driven architecture based on institutional trading system designs
Varun Kashyap - GitHub
Project Link: https://github.com/vkk28/Event-Driven-Backtesting-System