Financial market regime detection using Hidden Markov Models for adaptive trading strategies
Financial Market Regime Detection
A regime detection system using Hidden Markov Models and machine learning to identify market states (bull, bear, high-volatility) and adapt investment strategies accordingly.
Problem Statement
Financial markets exhibit distinct behavioral regimes that require different trading strategies. A strategy optimized for bull markets often fails during bear markets or high-volatility periods. This system automatically identifies market regimes and enables regime-aware portfolio management.
Industry Applications
- Quantitative trading firms
- Asset management (regime-aware allocation)
- Risk management (volatility forecasting)
- Algorithmic trading systems
- Portfolio optimization
- Market timing strategies
Key Features
- Hidden Markov Models (HMM): Unsupervised regime detection with Gaussian emissions
- Gaussian Mixture Models (GMM): Alternative clustering-based approach
- Change Point Detection: Structural break identification
- Regime-Conditioned Trading: Adaptive strategies based on detected regime
- Walk-Forward Validation: Proper backtesting without lookahead bias
Detected Regimes
| Regime | Characteristics | |--------|-----------------| | Bull/Calm | Low volatility, positive returns | | Bear/Crisis | High volatility, negative returns | | Transition | Medium volatility, mixed returns |
Technical Architecture
Market Data (Yahoo Finance)
|
v
+------------------+
| Feature Eng |
| - Returns |
| - Volatility |
| - Technicals |
+------------------+
|
v
+------------------+ +------------------+
| Regime Detection | --> | Strategy Engine |
| - HMM | | - Position Sizing|
| - GMM | | - Risk Mgmt |
| - Change Points | +------------------+
+------------------+ |
| v
v +------------------+
+------------------+ | Backtesting |
| Visualization | | - Walk-Forward |
| - Regime Plots | | - Metrics |
| - Equity Curves | +------------------+
+------------------+
Project Structure
market-regime-detection/
โโโ data/ # Cached market data
โโโ src/
โ โโโ data_loader.py # Yahoo Finance data fetching
โ โโโ features.py # Feature engineering
โ โโโ hmm_regime.py # HMM regime detection
โ โโโ gmm_regime.py # GMM clustering approach
โ โโโ changepoint.py # Change point detection
โ โโโ strategy.py # Regime-based trading strategies
โ โโโ backtest.py # Backtesting framework
โ โโโ visualize.py # Regime visualization
โ โโโ walk_forward.py # Walk-forward validation
โโโ notebooks/
โ โโโ EDA.ipynb # Exploratory data analysis
โโโ docs/
โ โโโ IMPLEMENTATION_PLAN.md
โโโ tests/
โโโ requirements.txt
โโโ README.md
Quick Start
# Clone repository
git clone https://github.com/Sakeeb91/market-regime-detection.git
cd market-regime-detection
Install dependencies
pip install -r requirements.txt
Download market data
python src/data_loader.py --ticker SPY --start 2000-01-01
Fit HMM and detect regimes
python src/hmm_regime.py --n-states 3
Run backtest
python src/backtest.py --strategy regime
Generate visualizations
python src/visualize.py
Expected Results
| Metric | Target | |--------|--------| | Regime detection | Correctly identify 2008, 2020 crises | | Strategy Sharpe | 0.5-1.0 (after costs) | | Max Drawdown | Reduced vs buy-and-hold | | Volatility | Lower than benchmark |
Key Insights
The system should detect:
- 2008 Financial Crisis: Transition to bear regime in late 2008
- 2020 COVID Crash: Sharp regime change in March 2020
- Bull Markets: Extended periods of low-volatility positive returns
Requirements
- Python 3.8+
- hmmlearn
- scikit-learn
- yfinance
- pandas
- numpy
- ruptures
- matplotlib
- seaborn
License
MIT License
Author
Sakeeb Rahman - GitHub
Disclaimer
This project is for educational purposes only. Past performance does not guarantee future results. This is not financial advice.