OPEN-SOURCE SCRIPT
AetherEdge - Hidden Markov Regime

🖊️ Overview
AE-HMM treats the market regime as a hidden state you can't observe directly (Bull/Bear/Range) and infers each state's probability from observable returns using a Hidden Markov Model's forward algorithm. The background bleeds a continuous "mood gradient" — green when Bull dominates, red for Bear, neutral grey for Range — visualizing the market's current mood. Online EM learns each state's emission parameters, auto-adapting to what Bull/Bear/Range look like on this instrument.
🔶 Key Features
3-state Hidden Markov Model: probabilistic Bull/Bear/Range via forward algorithm
Gaussian emission likelihood: each state has a return distribution
Sticky transition matrix: tunable regime persistence
Online EM learning: auto-learns each state's mean/variance from data
Sign clamping: keeps Bull>0, Bear<0, preventing label switching
Mood gradient background: red→grey→green continuum visualizing sentiment
State-colored EMA: moving average color changes with the most-likely state
State probability stack (optional): probability mass of 3 states below price
BUY/SELL on most-likely-state transitions
HUD showing each state's probability and learned parameters
Per-event alerts
🧠 Technical Architecture
This implements an HMM forward algorithm. Three hidden states (Bull/Bear/Range) each carry a Gaussian emission b_j(o) over ATR-normalized returns, with a sticky transition matrix A[i→j] (diagonal = persistence). The scaled forward recursion γ_t(j) ∝ [Σ_i γ_{t-1}(i)·A[j]]·b_j(o_t) computes the filtered posterior of each state per bar, normalized for stability. Online EM updates emissions via μ_j ← μ_j + η·γ_j·(o−μ_j) and σ²_j ← σ²_j + η·γ_j·((o−μ_j)²−σ²_j), sign-clamping Bull mean positive and Bear mean negative to preserve label meaning. Most-likely state = argmax γ. The bull-bear score = γ_bull − γ_bear drives the background gradient. Signals fire when the most-likely state transitions into Bull/Bear and probability exceeds threshold. State updates occur on confirmed bars only — no repaint.
⚙️ Recommended Settings & Tuning Guide
Regime Persistence 0.90 is a starting point — the transition matrix diagonal (regime stickiness). Higher = smoother, slower state changes; lower = more sensitive. Return Smoothing 1 smooths the observed return; raise for noisy instruments. Learn Emission Parameters ON is recommended; Learning Rate 0.03 is standard speed. Initial |Bull/Bear Mean| 0.5 sets the initial separation of Bull(+)/Bear(−) emission means. Min State Probability to Signal 0.4 requires the new dominant state to reach this probability before signaling — higher means only high-conviction transitions. Min Variance Floor bounds the variance.
💡 How to Use in Practice
Apply to chart and the background colors continuously with the market mood. Deeper green means higher Bull-state conviction, deeper red means Bear, pale grey means Range (no direction). The state-colored EMA shows the most-likely regime at a glance. Check STATE POSTERIOR in the HUD for the three probabilities; a sharp rise in Bull probability overtaking Bear marks a turning point. LEARNED EMISSIONS shows the model-learned mean return and volatility of Bull/Bear/Range for this instrument. Standing aside while Range probability is high, and entering once Bull/Bear is clear, is effective. Combining with other trend tools and trading only when regimes agree is another approach.
⚠️ Important Notes
This uses an online-EM approximation (not full Baum-Welch EM), a filtered forward posterior (not the Viterbi algorithm), Gaussian emissions, and sign-clamped labels. The forward-algorithm math, however, is the genuine HMM recursion. With 1-D return observations, Bull/Bear are clear by sign, while Range is represented as a near-zero mean. Online learning needs convergence time per instrument; emissions are not optimized initially. All regime estimates are probabilistic decisions based on historical data and do not guarantee future profits.
🚨 Disclaimer
This indicator is provided for educational and informational purposes only and does not constitute financial or investment advice. No signal guarantees future profit, and past performance does not indicate future results. All trading decisions are made at your own risk.
AE-HMM treats the market regime as a hidden state you can't observe directly (Bull/Bear/Range) and infers each state's probability from observable returns using a Hidden Markov Model's forward algorithm. The background bleeds a continuous "mood gradient" — green when Bull dominates, red for Bear, neutral grey for Range — visualizing the market's current mood. Online EM learns each state's emission parameters, auto-adapting to what Bull/Bear/Range look like on this instrument.
🔶 Key Features
3-state Hidden Markov Model: probabilistic Bull/Bear/Range via forward algorithm
Gaussian emission likelihood: each state has a return distribution
Sticky transition matrix: tunable regime persistence
Online EM learning: auto-learns each state's mean/variance from data
Sign clamping: keeps Bull>0, Bear<0, preventing label switching
Mood gradient background: red→grey→green continuum visualizing sentiment
State-colored EMA: moving average color changes with the most-likely state
State probability stack (optional): probability mass of 3 states below price
BUY/SELL on most-likely-state transitions
HUD showing each state's probability and learned parameters
Per-event alerts
🧠 Technical Architecture
This implements an HMM forward algorithm. Three hidden states (Bull/Bear/Range) each carry a Gaussian emission b_j(o) over ATR-normalized returns, with a sticky transition matrix A[i→j] (diagonal = persistence). The scaled forward recursion γ_t(j) ∝ [Σ_i γ_{t-1}(i)·A[j]]·b_j(o_t) computes the filtered posterior of each state per bar, normalized for stability. Online EM updates emissions via μ_j ← μ_j + η·γ_j·(o−μ_j) and σ²_j ← σ²_j + η·γ_j·((o−μ_j)²−σ²_j), sign-clamping Bull mean positive and Bear mean negative to preserve label meaning. Most-likely state = argmax γ. The bull-bear score = γ_bull − γ_bear drives the background gradient. Signals fire when the most-likely state transitions into Bull/Bear and probability exceeds threshold. State updates occur on confirmed bars only — no repaint.
⚙️ Recommended Settings & Tuning Guide
Regime Persistence 0.90 is a starting point — the transition matrix diagonal (regime stickiness). Higher = smoother, slower state changes; lower = more sensitive. Return Smoothing 1 smooths the observed return; raise for noisy instruments. Learn Emission Parameters ON is recommended; Learning Rate 0.03 is standard speed. Initial |Bull/Bear Mean| 0.5 sets the initial separation of Bull(+)/Bear(−) emission means. Min State Probability to Signal 0.4 requires the new dominant state to reach this probability before signaling — higher means only high-conviction transitions. Min Variance Floor bounds the variance.
💡 How to Use in Practice
Apply to chart and the background colors continuously with the market mood. Deeper green means higher Bull-state conviction, deeper red means Bear, pale grey means Range (no direction). The state-colored EMA shows the most-likely regime at a glance. Check STATE POSTERIOR in the HUD for the three probabilities; a sharp rise in Bull probability overtaking Bear marks a turning point. LEARNED EMISSIONS shows the model-learned mean return and volatility of Bull/Bear/Range for this instrument. Standing aside while Range probability is high, and entering once Bull/Bear is clear, is effective. Combining with other trend tools and trading only when regimes agree is another approach.
⚠️ Important Notes
This uses an online-EM approximation (not full Baum-Welch EM), a filtered forward posterior (not the Viterbi algorithm), Gaussian emissions, and sign-clamped labels. The forward-algorithm math, however, is the genuine HMM recursion. With 1-D return observations, Bull/Bear are clear by sign, while Range is represented as a near-zero mean. Online learning needs convergence time per instrument; emissions are not optimized initially. All regime estimates are probabilistic decisions based on historical data and do not guarantee future profits.
🚨 Disclaimer
This indicator is provided for educational and informational purposes only and does not constitute financial or investment advice. No signal guarantees future profit, and past performance does not indicate future results. All trading decisions are made at your own risk.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.