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
---
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。