A statistical arbitrage trading strategy that identifies and trades correlated stock pairs. This project executes real-time pairs trading using the Alpaca API, focusing on PulteGroup (PHM) and Toll Brothers (TOL). The strategy profits from temporary mispricings between these two homebuilder stocks.
- Cointegration & Correlation Testing: Identifies tradable stock pairs.
- Z-Score Based Trade Signals: Detects market inefficiencies.
- Backtesting Engine: Simulates historical performance.
- Live Trading via Alpaca API: Executes trades in real time.
- Risk Management: Implements position sizing and exit strategies.
PulteGroup (PHM) and Toll Brothers (TOL) are leading homebuilders in the U.S. housing market. Their stock prices are highly correlated and cointegrated, which makes them ideal for a pairs trade.
- Macroeconomic Sensitivity: Both stocks react similarly to changes in mortgage rates, housing demand, and supply chain dynamics.
- Market Segmentation: PHM targets the mass market while TOL focuses on the luxury segment. This difference can lead to temporary valuation divergences.
- Earnings & Industry Trends: Their prices often move together following sector-specific news and earnings reports.
The strategy exploits temporary deviations in the spread (the difference between PHM and TOL prices), betting on a reversion to the mean.
- Identify a Stock Pair: Select PHM and TOL based on high correlation and cointegration.
- Calculate the Spread & Z-Score: Standardize the difference in their prices to detect overbought or oversold conditions.
- Generate Trading Signals:
- Long PHM / Short TOL: When the Z-score is below -2.
- Short PHM / Long TOL: When the Z-score is above 2.
- Exit: When the Z-score returns between -0.5 and 0.5.
- Execute Trades via Alpaca API: Automatically place orders based on the signals.
- Backtest Performance: Use historical data to simulate strategy performance.
pairs-trading/
├── data/ # Stored data files
│ ├── stock_data.csv
│ ├── cointegrated_pairs.csv
│ └── trading_signals.csv
├── docs/ # Documentation files
│ ├── strategy.md
│ ├── backtesting.md
│ └── trading_signals.md
│ └── returns.md
├── src/ # Core source code
│ ├── config.py # API keys & strategy settings
│ ├── data_loader.py # Fetches stock data
│ ├── pairs_selection.py # Identifies tradable stock pairs
│ ├── trading_strategy.py# Generates trade signals
│ ├── backtest.py # Backtests strategy performance
│ └── live_trading.py # Executes live trades
├── .gitignore
├── requirements.txt
└── README.md
- Python 3.7 or later
- An Alpaca API account (for paper or live trading)
- Required Python packages (listed in
requirements.txt)
- Clone the Repository:
git clone https://github.com/ronitbatra/pairs-trading cd pairs-trading - Install Dependencies:
pip install -r requirements.txt
- Configure API Keys:
Update
src/config.pywith your Alpaca API credentials:ALPACA_API_KEY = "your_api_key" ALPACA_SECRET_KEY = "your_secret_key" ALPACA_BASE_URL = "https://paper-api.alpaca.markets"
- Run the Trading Pipeline:
- Backtesting:
python src/backtest.py
- Live Trading:
python src/live_trading.py
- Backtesting:
-
Pairs Trading:
A market-neutral strategy that profits from the relative price movements of two correlated stocks. -
Cointegration:
A statistical property where two time series move together over the long term, allowing us to bet on their mean reversion. -
Correlation:
A measure of how two stocks move relative to each other. High correlation (e.g., 0.8 or above) indicates similar price movements. -
Z-Score:
A standardized metric indicating how many standard deviations the current spread is from its mean. It is used to signal trades. -
Spread:
The difference between the prices of two stocks, calculated as:
Spread = PHM - TOL
A mean-reverting spread is key for pairs trading. -
Long/Short Positions:
- Long PHM / Short TOL: Buy PHM and sell TOL when the spread is abnormally low.
- Short PHM / Long TOL: Sell PHM and buy TOL when the spread is abnormally high.
-
Alpaca API:
An API that enables live trading by executing orders, fetching real-time market data, and managing positions.
This project is licensed under the MIT License. Feel free to modify and distribute.
- Ronit Batra – GitHub Profile
- Email: xfb7hj@virginia.edu