Interactive Pairs Trading Analysis
1 Introduction
This interactive analysis tool lets you perform pairs trading analysis on any two stocks of your choice. Pairs trading is a market-neutral strategy that matches a long position in one stock with a short position in another stock that has historically moved in a similar pattern. When the pair diverges from its historical relationship, a trade is initiated with the expectation that the relationship will revert to its mean.
2 Interactive Analysis Tool
Use the tool below to analyze potential pairs trading opportunities. Simply enter the ticker symbols for two stocks and adjust the trading parameters as needed.
3 How To Use
- Select Stock Pairs: Enter the ticker symbols for two stocks in the sidebar (e.g., “AZO” and “ORLY”)
- Configure Parameters:
- Initial Capital: Set the starting investment amount
- Shares Per Trade: Specify how many shares to trade with each signal
- Stop-Loss Percentage: Set the maximum loss allowed before exiting a position
- Analyze: Click the “Analyze Pair” button to generate results
- Review Results: Examine the statistical analysis, performance metrics, and visualizations
4 Interpretation Guide
4.1 Statistical Indicators
- Correlation: Values above 0.7 indicate strong correlation, suitable for pairs trading
- Cointegration p-value: Values below 0.05 suggest the pair is cointegrated
- ADF p-value: Values below 0.05 indicate the spread is stationary
- Half-life: Optimal values range from 5 to 60 days
4.2 Trading Signals
The Z-score chart shows the normalized deviation of the pair relationship:
- Entry Signals: Positions are opened when the Z-score crosses beyond the threshold lines (typically ±2.0)
- Exit Signals: Positions are closed when the Z-score returns to the mean (between ±0.5)
5 About The Implementation
This interactive tool is powered by Streamlit and uses the interactive_pairs.py
module to perform the analysis. The analysis includes:
- Historical price data retrieval
- Statistical tests for correlation and cointegration
- Z-score calculation for trading signals
- Backtesting with position tracking
- Performance evaluation with metrics such as total return, Sharpe ratio, and maximum drawdown
For more technical details on the implementation, please see the sample_pairs.qmd document or examine the source code on GitHub.