A Python-based quantitative research system for analyzing relationships between Indian equities, identifying potential mean-reversion opportunities, generating statistical arbitrage signals, and evaluating historical strategy performance.
Statistical arbitrage is a quantitative trading approach that attempts to identify temporary deviations in the historical relationship between assets and use statistical signals to evaluate potential mean-reversion opportunities. This project implements a simplified statistical arbitrage workflow using pairs of Indian equities. Historical market data is retrieved through Yahoo Finance, transformed into log prices, and analyzed using a hedge ratio, spread, rolling statistics, and Z-scores. The project also includes a backtesting component to evaluate the historical behavior of generated signals.
Note: This is a quantitative research and educational project. Backtest results are historical simulations and do not represent actual executed trades or guaranteed future returns.
- Analyze historical relationships between selected equities
- Compare normalized price movements
- Measure return correlation and annualized volatility
- Estimate a hedge ratio between two assets
- Construct a log-price spread
- Identify statistical deviations using rolling Z-scores
- Generate rule-based long/short spread signals
- Evaluate historical strategy performance through backtesting
- Present the analysis through an interactive Streamlit dashboard
The current workflow follows these stages:
Historical Market Data ↓ Price & Return Analysis ↓ Log Price Transformation ↓ Hedge Ratio Estimation ↓ Spread Construction ↓ Rolling Mean & Standard Deviation ↓ Z-Score Calculation ↓ Trading Signal Generation ↓ Historical Backtesting ↓ Performance Analysis
Historical adjusted price data is retrieved using yfinance.
The current stock universe includes:
- TCS
- Infosys
- Reliance Industries
- HDFC Bank
- ICICI Bank
The dashboard supports analysis periods of:
- 1 Year
- 3 Years
- 5 Years
Daily percentage returns are calculated for both selected assets.
The system reports:
- Total historical return
- Annualized volatility
- Return correlation
- Number of observations
The project estimates a hedge ratio using the covariance and variance of the two log-price series. The hedge ratio is then used to construct the relative spread between the assets.
The spread is constructed as: Spread = log(Price A) − Hedge Ratio × log(Price B) A 60-day rolling mean and standard deviation are used to measure the recent behavior of the spread.
The spread Z-score is calculated as: Z = (Spread − Rolling Mean) / Rolling Standard Deviation
The current signal framework uses:
| Z-Score | Signal |
|---|---|
| Z > +2 | SHORT SPREAD |
| Z < −2 | LONG SPREAD |
| −2 ≤ Z ≤ +2 | NO TRADE |
Generated signals are shifted by one trading day before being applied in the historical simulation to reduce direct look-ahead bias.
The backtest reports:
- Total Return
- Annualized Return
- Sharpe Ratio
- Annualized Volatility
- Maximum Drawdown
- Win Rate
The dashboard also visualizes the strategy equity curve and drawdown.
The application provides an interactive Streamlit dashboard where users can:
- Select two stocks
- Select the historical analysis period
- Compare normalized prices
- Inspect daily returns
- View return correlation
- Analyze the estimated hedge ratio
- Monitor the current spread Z-score
- View generated trading signals
- Examine historical signal data
- Evaluate backtest performance
- Inspect the strategy equity curve and drawdown
| Technology | Purpose |
|---|---|
| Python | Core programming language |
| Pandas | Data manipulation and analysis |
| NumPy | Numerical calculations |
| yfinance | Historical market data |
| Plotly | Interactive financial visualizations |
| Streamlit | Interactive research dashboard |
Stats_arb_rs/ │ ├── app.py ├── requirements.txt ├── README.md ├── .gitignore │ └── src/ ├── data.py ├── strategy.py └── backtest.py
git clone https://github.com/druvdoesDev/Stats_arb_rs.git cd Stats_arb_rs
python -m venv .venv
Activate it on Windows: .venv\Scripts\activate
pip install -r requirements.txt
streamlit run app.py The Streamlit dashboard will open in your browser.
This project is intended as a research and educational implementation rather than a production trading system.
Current limitations include:
- The hedge ratio is estimated over the selected historical dataset rather than through a rolling estimation framework.
- Transaction costs and brokerage costs are not modeled.
- Slippage and market impact are not modeled.
- Position sizing and capital allocation are simplified.
- The current backtest uses a simplified spread-return framework.
- No live order execution is implemented.
- Statistical stationarity and cointegration testing are not currently included.
- Results are dependent on the selected assets and historical period.
Potential extensions include:
- Cointegration testing using the Engle-Granger methodology
- Rolling hedge-ratio estimation
- More robust entry and exit rules
- Dynamic position sizing
- Transaction-cost modeling
- Walk-forward testing
- Out-of-sample evaluation
- Expanded stock universe
- Portfolio-level pair selection
- Risk management and exposure controls
- Live market-data monitoring
- Automated signal alerts
This project is intended for educational and quantitative research purposes only. Historical backtest performance does not guarantee future results. The system does not provide investment advice, and no live trades are executed by the application.