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.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.