Dynamic-Regime-Allocation is a quantitative trading strategy designed for the Nifty 50 index. Unlike traditional forecasting models that attempt to predict exact prices, this system uses Unsupervised Learning (Gaussian Hidden Markov Models) to decode latent market regimes (Bull, Bear, and Sideways).
The primary objective is Risk-Adjusted Return. The strategy dynamically adjusts portfolio leverage based on the identified regime, aiming to capture market upside while preserving capital during structural downtrends (e.g., 2008, 2020).
Key Differentiator: This is not a theoretical model. It includes a rigorous Transaction Cost Analysis (TCA), factoring in brokerage fees, slippage, and the yield on idle cash (Liquid BeEs), making the results audit-ready and deployable.
The strategy was backtested over an 18-year period covering multiple market cycles.
| Metric | Market (Buy & Hold) | HMM Strategy |
|---|---|---|
| Net Return | 319.27% | 311.01% |
| Drawdown Risk | High (-60% in 2008) | Significantly Reduced |
| Total Trades | N/A | 57 (Low Churn) |
| Fees Paid | 0.0% | ~5.6% of Capital |
Visual Analysis: The strategy matches the market's long-term compounding but avoids the "emotional torture" of deep drawdowns. Note the flat-lining (protection) during the 2008 and 2020 crashes.
We feed the HMM three distinct features to separate market states:
- Volatility: 20-day annualized rolling standard deviation (Risk Proxy).
- Trend (Z-Score): Price distance from the 50-day Moving Average (Trend Proxy).
- Momentum: 14-day price difference (Velocity Proxy).
The model assumes the market operates in three hidden states. It calculates the probability of being in each state daily:
- 🟢 Bull Regime: Low Volatility, Positive Trend.
- 🟠 Sideways Regime: Mixed Volatility, Mean Reverting.
- 🔴 Bear Regime: High Volatility, Negative Trend.
To prevent "whipsaw" losses (buying/selling every day), we apply a 10-day Moving Average to the regime probabilities. The model must sustain a signal for ~2 weeks before capital is committed. This reduced total trades from 441 to 57.
- Transaction Costs: 0.1% per trade (Brokerage + STT + Slippage).
- Cash Yield: Idle cash (when not invested) earns a 6% annualized return, simulating parking capital in Liquid BeEs or Overnight Funds.
| Market Regime | Signal Confidence | Action | Leverage |
|---|---|---|---|
| Strong Bull | P(Bull) > 80% | Aggressive Buy | 1.5x |
| Bull | P(Bull) > 60% | Standard Buy | 1.0x |
| Sideways | P(Side) > 60% & Dip | Buy the Dip | 1.0x |
| Bear | P(Bear) > 60% | Sell / Cash | 0x |
-
Clone the repository:
git clone [https://github.com/SahilMotyar/Dynamic-Regime-Allocation.git](https://github.com/SahilMotyar/Dynamic-Regime-Allocation.git) cd Dynamic-Regime-Allocation -
Install dependencies:
pip install yfinance pandas numpy matplotlib hmmlearn scikit-learn
-
Run the strategy:
python hmm.py
The script concludes by generating a Live "Trade Card" for the current day. It retrains the model on the most recent data to provide an actionable recommendation.
This project is for educational and research purposes only. It does not constitute financial advice. Algorithmic trading involves significant risk. Past performance is not indicative of future results.
