OPEN-SOURCE SCRIPT

Markov Regime Detector - CriptoMatiko

518
The **Markov Regime Detector** classifies each bar as Bull, Bear, or Lateral using rolling log-return, then builds a 3×3 Markov transition matrix from the chart's full visible history.

The matrix diagonal measures **persistence** — the probability of each regime continuing into the next bar. Alerts fire only when persistence exceeds a user-defined threshold, filtering out short-lived, low-confidence regime flips.

---

**How it works**

1. Computes log-return over the last N bars (default: 20)
2. Labels each bar: Bull (log-ret > +5%), Bear (log-ret < −5%), Lateral (in between)
3. Counts all historical transitions and row-normalizes into probabilities
4. Diagonal of the matrix = persistence of the current regime
5. Alert fires when: regime changes AND persistence >= configured threshold

---

**Visuals**

- Ribbon — real-time background tint: green (Bull) / rose (Bear) / grey (Lateral)
- Triangles ▲▼◆ — mark the start of each confirmed new regime
- Floating labels — regime name + persistence % at each transition point
- Table (bottom-right) — current regime, persistence, log-return and alert status

Note: The ribbon updates on the forming bar. Labels, shapes and alerts only fire on confirmed bar close (barstate.isconfirmed).

---

**Alerts**

Set up as "Any alert() call" + "Once per bar close". Dynamic message format:

REGIME: BEAR
Anterior: LATERAL
Persistencia: 85%
Preco: 2022.60
Indicador: Markov | CriptoMatiko

---

**Parameters**

Lookback window (default 20): bars used to compute log-return and classify the current state
Bull threshold (default 5%): log-ret above this value = Bull
Bear threshold (default 5%): log-ret below -5% = Bear
Min persistence to alert (default 55%): minimum persistence to trigger an alert
Min bars to hold (default 3): consecutive bars in same regime before label appears

---

כתב ויתור

המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.