OPEN-SOURCE SCRIPT
Markov Regime Detector - CriptoMatiko

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
---
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
---
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat TradingView sebenar, pencipta skrip ini telah menjadikannya sumber terbuka, jadi pedagang boleh menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupuan anda boleh menggunakan secara percuma, ingat bahawa penerbitan semula kod ini tertakluk kepada Peraturan Dalaman.
Penafian
Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.