Pairs Trading with Order Book Visualization
Analyzing Cointegration and Backtesting Trading Strategies with Real-Time Visualizations
Introduction
The Pairs Trading with Order Book Visualization project combines financial data analysis, statistical techniques, backtesting, and visualization to identify trading opportunities between two correlated assets. Pairs trading is a market-neutral strategy that involves simultaneously buying one asset and shorting another based on their cointegration relationship.
- Primary Goal: Identify statistically significant price deviations between two correlated assets.
- Tools Used: Python, NumPy, Pandas, Matplotlib, JavaScript (for visualization).
- Key Modules: Data fetching, cointegration analysis, signal generation, backtesting, and order book visualization.
Technical Workflow
1. Data Retrieval
Data for the order book and asset prices is fetched using the data/data_fetcher.py module. The
fetch_data() function leverages APIs or CSV inputs to retrieve historical prices and order book
snapshots for two assets.
- Order Book: Depth data showing bid-ask levels.
- Timeframe: Configurable historical range for price and order book analysis.
The data module ensures consistent formatting of fetched data, handling missing values and ensuring timestamps align for accurate analysis.
2. Cointegration Analysis
Cointegration testing, implemented in analysis/cointegration.py, forms the foundation of the
strategy. Two assets are said to be cointegrated if their price series have a constant mean-reverting
relationship over time.
The workflow for cointegration testing:
- Augmented Dickey-Fuller (ADF) Test: Determines stationarity of the residual between two assets.
- P-value Threshold: If the p-value is below a specified threshold (e.g., 0.05), the assets are considered cointegrated.
- Implementation: Utilizes the
statsmodelslibrary for robust statistical testing.
The output of this step is a confirmation of cointegration, along with model coefficients (e.g., hedge ratio), which are used to calculate deviations from the mean.
3. Signal Generation
Signals are generated based on price deviations relative to the mean of the spread between two assets. The
process is handled in analysis/signals.py:
- Mean-Spread Calculation: Compute the historical spread between asset prices using the hedge ratio.
- Z-Score Calculation: Standardize deviations using the formula:
Z = (Spread - Mean(Spread)) / StdDev(Spread) - Trading Logic:
- Buy the spread when Z-Score < -2 (underpriced).
- Sell/Short the spread when Z-Score > 2 (overpriced).
- Exit positions when Z-Score reverts close to 0.
The signals are stored and passed to the backtesting module for evaluation.
4. Backtesting
The backtesting/backtest.py module evaluates the pairs trading strategy using historical data.
The backtester simulates trades based on the generated signals and calculates key performance metrics.
The backtesting process includes:
- Position Management: Simulate long and short positions for each trade.
- Risk Management: Leverage the
execution/risk_manager.pyto apply stop-loss limits and maximum capital exposure. - Performance Metrics:
- Cumulative P&L
- Sharpe Ratio
- Maximum Drawdown
The module generates a detailed report on strategy performance, including trade-level and portfolio-level statistics.
5. Order Book Visualization
The visualization/order_book_viz.js implements a real-time order book dashboard. Using
JavaScript, it renders bid-ask levels dynamically, providing insights into liquidity and market depth.
- Features:
- Visual representation of order book depth.
- Highlighting price levels triggering trade signals.
- Integration: Fetches live or simulated order book data from the
data/order_book.pyscript.
The visualization assists traders in identifying liquidity imbalances and validating trade entries.
Key Highlights
- Robust cointegration testing to identify correlated asset pairs.
- Dynamic signal generation using mean-reverting spreads.
- Comprehensive backtesting framework with performance metrics.
- Interactive order book visualization for real-time decision-making.
GitHub Repository
Access the complete code, data, and visualization tools here: GitHub Repository
Developed by Aryan. Explore more projects on GitHub.
