Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Statistical Arbitrage Research System

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.

Overview

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.

Objectives

  • 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

Methodology

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

1.Market Data

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

2.Return Analysis

Daily percentage returns are calculated for both selected assets.

The system reports:

  • Total historical return
  • Annualized volatility
  • Return correlation
  • Number of observations

3.Hedge Ratio

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.

4.Spread

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.

5.Z-Score

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

6.Backtesting

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.

Dashboard

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

Tech Stack

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

Project Structure

Stats_arb_rs/ │ ├── app.py ├── requirements.txt ├── README.md ├── .gitignore │ └── src/ ├── data.py ├── strategy.py └── backtest.py

Getting Started

1.Clone the repository

git clone https://github.com/druvdoesDev/Stats_arb_rs.git cd Stats_arb_rs

2.Create a virtual environment

python -m venv .venv

Activate it on Windows: .venv\Scripts\activate

3.Install dependencies

pip install -r requirements.txt

4.Run the application

streamlit run app.py The Streamlit dashboard will open in your browser.

Current Limitations

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.

Future Improvements

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

Disclaimer

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.

About

Statistical arbitrage research system for pair analysis, spread construction, Z-score signals, and historical backtesting using Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages