OPEN-SOURCE SCRIPT
Bayesian Trend Posterior [forexobroker]

Bayesian Trend Posterior treats each bar as a Bernoulli observation (up vs not-up) and runs a Beta-Bernoulli sequential update over a sliding window. The posterior probability P(next bar up) = (alpha + u) / (alpha + beta + N), where u is the count of up-bars in the window. Schmitt hysteresis on the posterior locks bull or bear regimes, preventing chatter near 0.5.
🔶 ALGORITHM
1. Count up-bars u in the last N closes (close > close[1]).
2. Posterior mean = (alpha + u) / (alpha + beta + N) using Beta(alpha, beta) prior.
3. Hysteresis: regime locks to +1 when posterior >= upper threshold, locks to -1 when posterior <= lower threshold, holds otherwise.
4. Within a locked regime, an EMA cross provides the entry trigger so the indicator can fire multiple times during sustained trends.
🔶 SIGNAL LOGIC
- Buy: bull regime locked AND close crosses pullback EMA up AND not already long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: bear regime locked AND close crosses pullback EMA down.
- Position-lock state machine.
🔶 INPUTS
- Posterior Window N (default 30)
- Prior alpha (default 2.0)
- Prior beta (default 2.0)
- Upper Hysteresis (default 0.55)
- Lower Hysteresis (default 0.45)
- Pullback EMA Length (default 8)
- Cooldown Bars (default 4)
- Visual: dashboard, glow, EMA toggle, buy / sell colors
🔶 ALERTS
BTP Buy, BTP Sell, BTP Any Signal, BTP Bull Lock, BTP Bear Lock, BTP High Posterior, BTP Low Posterior, BTP Cross 0.5, BTP Webhook JSON.
🔶 LIMITATIONS
- Bernoulli simplification ignores bar size; a tiny up-bar contributes the same as a strong one. Pair with an ATR-aware filter for size-weighted posteriors.
- Window N is the dominant tuning knob: smaller N reacts faster but flips more; larger N is steadier.
- Prior alpha = beta = 2 is uninformative; users with strong directional bias can adjust.
- Hysteresis prevents chatter but slightly delays regime detection vs threshold-only logic.
🔶 ALGORITHM
1. Count up-bars u in the last N closes (close > close[1]).
2. Posterior mean = (alpha + u) / (alpha + beta + N) using Beta(alpha, beta) prior.
3. Hysteresis: regime locks to +1 when posterior >= upper threshold, locks to -1 when posterior <= lower threshold, holds otherwise.
4. Within a locked regime, an EMA cross provides the entry trigger so the indicator can fire multiple times during sustained trends.
🔶 SIGNAL LOGIC
- Buy: bull regime locked AND close crosses pullback EMA up AND not already long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: bear regime locked AND close crosses pullback EMA down.
- Position-lock state machine.
🔶 INPUTS
- Posterior Window N (default 30)
- Prior alpha (default 2.0)
- Prior beta (default 2.0)
- Upper Hysteresis (default 0.55)
- Lower Hysteresis (default 0.45)
- Pullback EMA Length (default 8)
- Cooldown Bars (default 4)
- Visual: dashboard, glow, EMA toggle, buy / sell colors
🔶 ALERTS
BTP Buy, BTP Sell, BTP Any Signal, BTP Bull Lock, BTP Bear Lock, BTP High Posterior, BTP Low Posterior, BTP Cross 0.5, BTP Webhook JSON.
🔶 LIMITATIONS
- Bernoulli simplification ignores bar size; a tiny up-bar contributes the same as a strong one. Pair with an ATR-aware filter for size-weighted posteriors.
- Window N is the dominant tuning knob: smaller N reacts faster but flips more; larger N is steadier.
- Prior alpha = beta = 2 is uninformative; users with strong directional bias can adjust.
- Hysteresis prevents chatter but slightly delays regime detection vs threshold-only logic.
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.
🔵Gain access to our powerful tools : kestutisbalciunas.com/tradingview/
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
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.
🔵Gain access to our powerful tools : kestutisbalciunas.com/tradingview/
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
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.