Inspiration

Traditional pairs trading strategies often rely purely on historical price correlation. While this works in stable markets, these relationships frequently break down during macroeconomic regime shifts such as interest rate changes, inflation shocks, or currency volatility. In emerging markets like India, macroeconomic forces significantly influence stock behavior. I wanted to explore whether incorporating structural economic sensitivity into clustering could create more stable and economically meaningful trading pairs rather than relying solely on surface-level return similarity.

What it does

Macro-Aware Clustering enhances traditional pairs trading by integrating macroeconomic sensitivity into the pair selection process. Instead of clustering stocks only by return-based features, the model incorporates regression-based macro betas derived from:

Inflation (ΔCPI) Interest rate changes Crude oil prices USD/INR exchange rate GDP growth

Stocks are clustered using hierarchical clustering on enriched feature vectors. Cointegrated pairs are then identified using the Augmented Dickey-Fuller (ADF) test.

The system compares:

Baseline (return-only clustering) Macro-aware clustering

Backtests evaluate:

Sharpe Ratio Win Rate Maximum Drawdown

The result is a more structurally robust and economically interpretable pairs trading framework.

How I built it

  1. Data Collection Weekly data for 110 NSE-listed stocks (2000–2024) Macroeconomic data from RBI, MOSPI, and EIA

  2. Feature Engineering For each stock, we estimated macroeconomic sensitivity using OLS regression

  3. Clustering We evaluated KMeans, DBSCAN, OPTICS, and Hierarchical Clustering. Hierarchical clustering (Ward’s method) achieved the best silhouette score and interpretability.

  4. Cointegration Testing Within each cluster, pairs were tested using the ADF test. Pairs with p-value < 0.05 were retained.

  5. Backtesting Framework Strategies were evaluated across multiple historical windows using Sharpe ratio, win rate, and maximum drawdown.

  6. Streamlit Dashboard A dashboard was built to visualize:

Cluster structures Top cointegrated pairs Performance comparisons Regime-based backtests

Challenges I ran into

Aligning long-term macroeconomic data with stock price data across 24 years. Handling missing listings and ensuring time-series synchronization. Avoiding overfitting when clustering high-dimensional financial features. Translating a research-heavy notebook into an interactive dashboard.

Accomplishments that I'm proud of

Successfully integrating macroeconomic betas into an unsupervised clustering framework. Demonstrating improved long-term robustness compared to a baseline model. Achieving higher historical Sharpe ratios and lower drawdowns in segmented backtests. Building a research-grade financial model and deploying it as an interactive application.

What I learned

Correlation does not imply structural similarity. Macro regime shifts significantly impact statistical arbitrage performance. Financial machine learning requires both quantitative rigor and economic reasoning. Interpretability is as important as performance in trading systems.

What's next for Macro-Aware Clustering for Smarter Pairs Trading

Introduce regime detection using Hidden Markov Models. Add dynamic clustering that adapts to changing macro environments. Integrate transaction costs and slippage modeling. Expand beyond India to other emerging markets. Deploy as a live paper-trading framework with real-time macro updates.

Built With

Share this project:

Updates

Submission history