OPEN-SOURCE SCRIPT

SAO RUBIQ Regime v2

441
# SAO · RUBIQ — Regime Visualizer (v2)

**by SNP420 · Jarvis Claudos · Finexus s.r.o.**
*Pine Script v6 · Build 2026-05-26*

---

## What is it

A research-grade market regime overlay that classifies every confirmed
bar into one of **five regimes** and paints the chart accordingly.
Born out of a simple insight:

> *"The right algorithm in the wrong phase of the market still fails —
> even when it is 100% correct under ideal conditions."*

This is the central thesis of the **SAO · RUBIQ** project: every
strategy lives or dies inside a specific regime × timeframe cell.
Without active regime perception, every trading system is necessarily
under-performing on the bars it was not designed for. This indicator
makes those cells visible.

## What's new in v2

v2 throws out the v1 AND-stack of ADX / BB-width / drift cutoffs and
replaces it with four orthogonal signals that vote together:

- **Fractal pivots** (3-bar centered) labeled **HH / LH / HL / LL**
- **Trend state machine** with **re-anchoring**:
UP / DOWN / RANGE / REVERSAL_UP / REVERSAL_DOWN
- **Multi-TF RSI** on M30 / H1 / H4 / D1 / W1 with per-TF thresholds
and a **≥3 of 5 agreement** rule
- **S/R level clustering** (±0.3 ATR tolerance) with **double-bounce**
reversal flag that persists for 10 bars

Measured improvements over v1.1 on EUR/USD M30, 2024-2025 (24,863 bars):

| Metric | v1.1 calibrated | v2 |
| ----------------------- | --------------- | ----------- |
| Confident coverage | 44.87% | **53.96%** |
| Transitions (less whipsaw) | 1,567 | **855** |
| BULL_CALM Sharpe (h=5) | +0.085 | **+0.271** |
| BEAR_CALM direction | +0.04 ⚠ (wrong)| **−0.19 ✅** |
| Shuffle F-stat (h=5) | 0.0027 | **0.0068** |
| Strategy diagonal score | 1 / 5 | **2 / 5** |

All validated by 1000-iter shuffle Monte Carlo (p < 0.001) and a
strategy × regime P&L matrix.

## The five regimes

| Regime | Meaning |
| ------------- | -------------------------------------------------------- |
| **BULL_CALM** | Trend UP + ≥3 of 5 RSI TFs agree bullish |
| **BEAR_CALM** | Trend DOWN + ≥3 of 5 RSI TFs agree bearish |
| **RANGE** | Mixed last pivots (no clean HH+HL or LH+LL) |
| **CHOPPY** | Trend UP/DOWN without RSI agreement, or active reversal |
| **STRESS** | Volatility spike: rv20_norm > 2 OR atr_z > 2 |
| *UNCERTAIN* | Fallback — pre-warmup or no labels yet (no box drawn) |

Priority order (highest wins): **STRESS > BULL > BEAR > CHOPPY > RANGE.**

## How it works

For every confirmed bar the indicator:

1. Computes ATR(14), realized vol (20), z-scored ATR — used by stress
detection and S/R cluster tolerance.
2. Requests RSI(14) on five timeframes via `request.security`
(M30 / H1 / H4 / D1 / W1) and counts bull / bear agreements.
3. Detects fractal pivots with `ta.pivothigh / pivotlow(3, 3)` and
labels each as HH / LH / HL / LL versus the previous same-type
pivot.
4. Adds each pivot to a greedy S/R level cluster (±0.3 ATR). On the
second touch within 500 bars it fires a **double-bounce reversal**
flag (type H → REVERSAL_DOWN, type L → REVERSAL_UP) that persists
for 10 bars.
5. Maintains a trend state machine: **UP** when last H=HH and last L=HL,
**DOWN** when last H=LH and last L=LL, **RANGE** otherwise.
6. Combines trend × RSI × stress into the final label, then applies
a hysteresis smoother (`min_run = 5 bars`) so single-bar flips
never get committed.

## Visualization (FX-Sessions-style)

- **Dashed segment box** per confirmed regime run, sized to that
segment's high/low.
- **Background tint** — semi-transparent regime color over the span.
- **Bar color** (off by default) — paints OHLC bars with regime color.
- **Segment label** anchored to the top of each box.
- **Pivot markers** — HH / LH / HL / LL drawn at every confirmed pivot
with bull/bear tint.
- **Reversal arrows** — ▲ green at support double-bounces, ▼ red at
resistance double-bounces.
- **Info table** (top-right) — current state, trend, last H and L labels,
RSI per TF, bull/bear agree counts, rv20_norm, atr_z.
- **Stats table** (bottom-right) — N bars and % share per regime across
the visible history.

UNCERTAIN bars deliberately render no box and no tint (clean chart).

## Settings worth knowing

- **Hysteresis min_run** (5) — bars of consistent raw state before
commit. Bigger = less flicker, more boundary lag.
- **Pivot left/right** (3) — fractal pivot window. Smaller catches
more pivots but more noise.
- **Per-TF RSI thresholds** — defaults are 65/35 (M30), 62/38 (H1),
60/40 (H4), 55/45 (D1), 50/50 (W1). Overridable per pair / TF.
- **RSI agree min** (3) — TFs that must agree to qualify as
BULL_CALM / BEAR_CALM. Lower = more sensitive.
- **S/R cluster tolerance** (0.3 × ATR) — width of an S/R level zone.
- **S/R max age** (500 bars) — oldest first-touch still eligible for
double-bounce reversal.
- **Reversal persistence** (10 bars) — how long after the second touch
the REVERSAL state stays active.
- **Max active S/R levels** (200) — FIFO ceiling on level memory.
- **All six regime colors + reversal arrow colors** — fully overridable.

## Sanity-check expectation (EUR/USD M30, 2 years)

If the v2 baseline distribution holds on your data window:

| State | Share |
| --------- | ------ |
| BULL_CALM | ~2.6% |
| BEAR_CALM | ~1.8% |
| RANGE | ~2.4% |
| CHOPPY | ~38.7% |
| STRESS | ~8.4% |
| UNCERTAIN | ~46.0% |

v2 deliberately has stricter BULL / BEAR (needs trend state + 3-of-5
RSI agreement) and a wider CHOPPY (catches trend bars without RSI
agreement + all active reversals). Cleaner regime blocks, better
direction mapping.

## Honest limitations — please read

- **Tuned to EUR/USD M30 vol scale.** Features and rules are
TF-agnostic in math, but the thresholds were calibrated on M30.
On H1 / D1 / crypto / equities the distribution will be approximate
unless you re-tune.
- **v2 direction mapping is improved but not perfect.** Diagonal
score = 2 / 5 (vs 1 / 5 in v1.1). BULL_CALM and BEAR_CALM now point
the right way; RANGE drifts slightly up; CHOPPY and STRESS still
show counter-intuitive mean-reversion edge. **Treat the indicator
as a labeled regime overlay for research, not a stand-alone
trade-direction signal.**
- **Streaming lags exist.** Pivot detection lag ≈ 3 bars,
hysteresis commit lag ≈ `min_run − 1` bars, multi-TF RSI uses
`lookahead=barmerge.lookahead_off` so higher-TF RSI updates only on
higher-TF bar close.
- **S/R level memory capped at 200 (FIFO).** The Python reference
keeps levels unbounded; on very long charts you may see different
bounce decisions than the offline version near the cap.

## Alerts

Seven alert conditions ship in:

- Regime → STRESS
- Regime → BULL_CALM
- Regime → BEAR_CALM
- Regime → RANGE
- Regime → CHOPPY
- Double-bounce UP (support held, second touch)
- Double-bounce DOWN (resistance held, second touch)

Regime alerts fire on transition (state differs from previous bar).
Double-bounce alerts fire at the second-touch bar of any cluster.

## Credits & attribution

- **Visual style** inspired by *FX Market Sessions* by **boitoki**
(Mozilla Public License 2.0). The segment-box-per-run pattern is
borrowed from that script; all classifier logic, feature math,
pivot state machine and S/R clustering is original to SAO · RUBIQ.
- **RUBIQ thesis** — *Rubik's-cube model of the market*: the right
algorithm in the wrong market phase still fails. Distilled from
100+ failed variants across the SAO portfolio.
- **Built by** SNP420 · Jarvis Claudos · Finexus s.r.o.

## License

Same as the parent SAO_RUBIQ project. Use freely, modify freely,
attribute when republishing.

---

*"Trh je proměnlivé prostředí. RUBIQ je centrální nervová soustava,
která to řeší pro všechny SAO strategie."*

— SNP420

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.