Indicator · Pine Script v4 · Own pane

Bayesian BBSMA Oscillator

by tistaharahap· code credited to tista

This indicator calculates directional probabilities based on price position relative to Bollinger Bands and a simple moving average over a rolling lookback window.

View on GitHub

GitHub stars25on the repository
Pine versionv4older, still runs
Lines of code87
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update6 years ago29 Jun 2020

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

87 lines of Pine Script v4 from bayesian/indicators/bbsma.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

bbsma.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tista

//@version=4
study("Bayesian BBSMA Oscillator", shorttitle="BayesBbSmaOsc")

bbSmaPeriod = input(20, title="BB SMA Period")
bbStdDevMult = input(2.5, title="BB Standard Deviation", maxval=50.0)

bbBasis = sma(close, bbSmaPeriod)
bbStdDev = bbStdDevMult * stdev(close, bbSmaPeriod)

bbUpper = bbBasis + bbStdDev
bbLower = bbBasis - bbStdDev

// SMA
smaPeriod = input(20, title="SMA Period")
smaValues = sma(close, smaPeriod)


// Bayesian Theorem Starts
bayesPeriod = input(20, title="Bayesian Lookback Period")

// Next candles are breaking Down
probBbUpperUpSeq = close > bbUpper ? 1 : 0
probBbUpperUp = sum(probBbUpperUpSeq, bayesPeriod) / bayesPeriod
probBbUpperDownSeq = close < bbUpper ? 1 : 0
probBbUpperDown = sum(probBbUpperDownSeq, bayesPeriod) / bayesPeriod

probUpBbUpper = probBbUpperUp / (probBbUpperUp + probBbUpperDown)

probBbBasisUpSeq = close > bbBasis ? 1 : 0
probBbBasisUp = sum(probBbBasisUpSeq, bayesPeriod) / bayesPeriod
probBbBasisDownSeq = close < bbBasis ? 1 : 0
probBbBasisDown = sum(probBbBasisDownSeq, bayesPeriod) / bayesPeriod

probUpBbBasis = probBbBasisUp / (probBbBasisUp + probBbBasisDown)

probSmaUpSeq = close > smaValues ? 1 : 0
probSmaUp = sum(probSmaUpSeq, bayesPeriod) / bayesPeriod
probSmaDownSeq = close < smaValues ? 1 : 0
probSmaDown = sum(probSmaDownSeq, bayesPeriod) / bayesPeriod

probUpSma = probSmaUp / (probSmaUp + probSmaDown)

sigmaProbsDown = nz(probUpBbUpper * probUpBbBasis * probUpSma / probUpBbUpper * probUpBbBasis * probUpSma + ((1 - probUpBbUpper) * (1 - probUpBbBasis) * (1 - probUpSma)))

// Next candles are breaking Up
probDownBbUpper = probBbUpperDown / (probBbUpperDown + probBbUpperUp)
probDownBbBasis = probBbBasisDown / (probBbBasisDown + probBbBasisUp)
probDownSma = probSmaDown / (probSmaDown + probSmaUp)

sigmaProbsUp = nz(probDownBbUpper * probDownBbBasis * probDownSma / probDownBbUpper * probDownBbBasis * probDownSma + ( (1 - probDownBbUpper) * (1 - probDownBbBasis) * (1 - probDownSma) ))

showNextCandleDown = input(true, title="Plot Next Candles Breaking Down?")
plot(showNextCandleDown ? sigmaProbsDown * 100 : na, title="Next Candle Breaking Down Probs", transp=0, color=color.red, linewidth=2)

showNextCandleUp = input(true, title="Plot Next Candles Breaking Up?")
plot(showNextCandleUp ? sigmaProbsUp * 100 : na, title="Next Candle Breaking Up Probs", transp=0, color=color.green, linewidth=2)

probPrime = nz(sigmaProbsDown * sigmaProbsUp / sigmaProbsDown * sigmaProbsUp + ( (1 - sigmaProbsDown) * (1 - sigmaProbsUp) ))

showPrime = input(true, title="Plot Prime Probability?")
plot(showPrime ? probPrime * 100 : na, title="Prime Probability", transp=0, color=color.blue, linewidth=2)

lowerThreshold = input(15.0, title="Lower Threshold")

sideways = probPrime < lowerThreshold / 100 and sigmaProbsUp < lowerThreshold / 100 and sigmaProbsDown < lowerThreshold / 100

longUsingProbPrime = probPrime > lowerThreshold / 100 and probPrime[1] == 0
longUsingSigmaProbsUp = sigmaProbsUp < 1 and sigmaProbsUp[1] == 1

shortUsingProbPrime = probPrime == 0 and probPrime[1] > lowerThreshold / 100
shortUsingSigmaProbsDown = sigmaProbsDown < 1 and sigmaProbsDown[1] == 1

longSignal = longUsingProbPrime or longUsingSigmaProbsUp
shortSignal = shortUsingProbPrime or shortUsingSigmaProbsDown

barcolor(longSignal ? color.lime : na, title="Long Bars")
barcolor(shortSignal ? color.maroon : na, title="Short Bars")

hzl3 = hline(lowerThreshold, color=#333333, linestyle=hline.style_solid)
hzl4 = hline(0, color=#333333, linestyle=hline.style_solid)
fill(hzl3, hzl4, title="Lower Threshold", color=sideways ? color.gray : color.maroon, transp=70)

alertcondition(longSignal, title="Long!", message="Bayesian BBSMA - LONG - {{exchange}}:{{ticker}} at {{close}}")
alertcondition(shortSignal, title="Short!", message="Bayesian BBSMA - SHORT - {{exchange}}:{{ticker}} at {{close}}")

Overview

Bayesian BBSMA Oscillator computes breakup, breakdown, and combined prime probability percentages, plotting up to three separate probability curves in an oscillator pane. Crossing events and threshold conditions trigger long and short signals that highlight price chart bars in green or maroon. Users can configure Bollinger Band settings, SMA periods, Bayesian sample lengths, and threshold values.

Details

Published by

tistaharahap on GitHub

Code credit

tista, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Moving averages, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
BB SMA Periodint20bbSmaPeriod
BB Standard Deviationfloat2.5bbStdDevMult
SMA Periodint20smaPeriod
Bayesian Lookback Periodint20bayesPeriod
Plot Next Candles Breaking Down?booltrueshowNextCandleDown
Plot Next Candles Breaking Up?booltrueshowNextCandleUp
Plot Prime Probability?booltrueshowPrime
Lower Thresholdfloat15.0lowerThreshold
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V

MultiIndicator

by capissimo
114 stars

MultiIndicator combines multiple technical indicators into a single separate pane to save chart space. It calculates and displays market regime detection based on ordinary least squares regression, a Holt exponential smoothing crossover, normalized RSI, ADX, a

Indicator v4 Oscillators & momentumTrend
Updated 1 year ago · None stated GitHub repository of MultiIndicator

PPO ATR Bands

by cryptorife
75 stars

PPO ATR Bands plots a percentage price oscillator in a separate pane. It compares a Hull average of typical price with a volume-weighted exponential average of Parabolic SAR, then scales three true-range bands around the slow average into matching oscillator l

Indicator v4 Oscillators & momentumMoving averages
Updated 3 years ago · GPL-3.0 GitHub repository of PPO ATR Bands

This indicator plots a relative strength index in a separate pane. It calculates RSI from the average of the previous ten opens rather than the current bar, so that source does not change during the live candle. A moving average of the RSI is plotted and can b

Indicator v5 Oscillators & momentumMoving averages
Updated 2 years ago · None stated GitHub repository of No Repaint - Relative Strength Index

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers