Featured Strategy: The EPAT Project by Aparna Singhal
Markets do not move in a straight line. There are phases where trends are strong, phases where volatility rises, and periods where markets remain range-bound. Identifying these phases early can help traders adjust risk and position sizing. This is where machine learning for market regime detection becomes relevant.
Watch the Full Video
Add the full walkthrough below to see how the model, features, and strategy were built step by step.
[Embed YouTube Video Here]
Download the Code
Access the implementation and test the model using the code below:
This project, developed by an EPAT learner from QuantInsti, focuses on building a regime detection framework using market breadth data and a Random Forest model. The objective is to classify market regimes and adjust capital allocation based on those regimes.
Why Market Regime Detection Matters
A trading strategy that performs well in a bull market may struggle during high volatility or bear phases. Detecting the current regime allows traders to:
- Adjust exposure
- Manage drawdowns
- Improve risk-adjusted returns
- Maintain consistency across market conditions
Instead of reacting after losses, regime detection helps in preparing for changing market environments.
Data and Feature Creation
The project uses historical data from the Nifty 500 index to represent broad market behaviour across large-cap, mid-cap, and small-cap stocks.
Market breadth indicators were created to capture:
- Momentum across stocks
- Trend strength
- Volatility participation
- Percentage of stocks moving above key moving averages
These features help measure whether the broader market supports index movement or shows divergence.
Defining Market Regimes
Four regimes were defined:
- Bull market
- Bear market
- High volatility
- Low volatility
Adaptive thresholds were used instead of fixed values to account for changing market environments. A persistence filter was also applied to avoid frequent regime shifts caused by short-term noise.
Model Training with Random Forest
A Random Forest classifier was used to detect regimes. The model was trained on historical market breadth features and tested on unseen data using time-series validation.
Random Forest works as a collection of decision trees that collectively classify the current market condition. This approach helps capture relationships between multiple features without relying on a single indicator.
Strategy and Capital Allocation
Once regimes are identified, position sizing is adjusted based on market conditions.
For example:
- Higher allocation during low-volatility bull phases
- Reduced exposure during high-volatility or bear phases
The focus is on reducing drawdowns and improving the Sharpe ratio rather than only increasing returns. Transaction costs and signal smoothing were also considered to keep the strategy realistic.
Conclusion
Market regime detection using machine learning provides a structured way to adapt trading decisions to changing market conditions. Combining market breadth indicators with models such as Random Forest allows traders to adjust exposure, manage risk, and build more stable strategies.
This project shows how Python and machine learning can be applied to regime detection and capital allocation using a clear, step-by-step workflow.

