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
---
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.