Advanced regime detector
A market state code from 0 to 3: strong trend, choppy range, high volatility, or quiet and mixed.
Each bar gets one code from 0 to 3. Trend strength compares total up-moves with total down-moves across the last 20 closes: it is 0 when they balance and 1 when every move points one way. Volatility is the average true range over the last 14 bars, as a share of price, tested against cut-offs of 1.2% and 1.5%. Strong trend (2) wins whenever trend strength is above 0.3; choppy (3) needs trend strength below 0.15 and volatility under 1.2%; high volatility (1) needs volatility above 1.5% without a strong trend; everything else is 0.
How to read it
- The line sits in its own pane below the price on a 0 to 3 scale. Its values are codes, not prices, so do not read its height against the candles.
- A run of 2 means one-sided moves over the last 20 closes. A 3 means the closes are going back and forth with no net drift.
- The line stays blank for the first 19 bars while the 20-bar windows fill.
- 0 is the fallback for mixed readings as well as quiet ones, so it does not on its own mean low volatility.
What it outputs
- Regime code
- Market state from 0 to 3: 2 strong trend, 3 choppy range, 1 high volatility, 0 quiet or mixed. Blank during warm-up, 19 bars by default.
Settings
- atr_window
- Bars in the average true range behind the volatility tests. One-minute ATR sits far under the cut-offs, so this rarely changes the code. Keep it below the larger of trend_window and vol_window, or the first reading raises a TypeError.
- trend_window
- Closes in the up-versus-down measure. Longer windows need a sustained move to reach 2, so strong-trend readings get rarer, and shorter windows change state more often on small moves. It also lengthens the warm-up.
- vol_window
- Bars of volume history needed before the first reading. Volume never changes the regime, so raising this only delays the first reading. It also raises the ceiling for atr_window.
Pitfalls
- On one-minute bars the volatility cut-offs are too wide to matter: 1 is rare, and 3 versus 0 is set by trend strength alone. Volume only sets the warm-up.
- Setting atr_window at or above the larger of trend_window and vol_window is a code defect: the first reading raises a TypeError because the ATR has not formed yet.
- Codes are categories, not a scale: the median of 2 on SPY one-minute bars for September 2026 does not show how often the market trended, and the sample does not give each code's share.