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.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
🔵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.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
🔵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.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.