Editors' picks
OPEN-SOURCE SCRIPT

Bayesian BBSMA Oscillator

Updated
Sometime ago (very long ago), one of my tinkering project was to do a spam or ham classification type app to filter news I'd wanna read. So I built myself a Naive Bayes Classifier to feed me my relevant articles. It worked great, I can cut through the noise.

The hassle was I needed to manually train it to understand what I wanna read. I trained it using 50 articles and to my surprise, it's enough.

Complexity Theory

I've been reading a book called The Road to Ruin by Jim Rickards. He described how he got to his conclusion of how the stock market works by using Complexity Theory. Bill Williams would agree. Jim tells us that by using just enough data, we calculate the probability of an event to occur. We can't say for sure when but we know it's coming. This was my light bulb moment.

While Jim talks much about Bayesian Inference in which a probability of an event can always be updated as more evidence comes to light, I had my eyes set on binary probabilities of when prices are going up and down.

Assumptions

These are my assumptions:

  • Prices breaking up a Bollinger basis line will have fuel to go up even higher
  • Prices will go down when prices have broken up a Bollinger upper band
  • Scalping is the main method so we should use a lower period Moving Average (MA)
  • When prices are above MA, it's likelier a correction to the downside is imminent
  • When prices are below MA, it's likelier a correction to the upside is imminent
  • Optimize parameters for 1 hour timeframe which will give us time to react while still having more opportunities to trade


Building Blocks

Jim Rickards started with limited data (events) while in technical trading, data are plentiful. I decided to classify 2 events which are:

  • Next candles would be breaking up
  • Next candles would be breaking down


Key facts:

  • We won't know for sure when prices are going to break
  • We won't know for sure how much the prices movements are going to be


Formulas

Breaking up:

Pr(Up|Indicator) = Pr(Indicator|Up) * Pr(Up) / Pr(Indicator|Up) * Pr(Up) + Pr(Indicator|Down) * Pr(Down)

Breaking down:

Pr(Down|Indicator) = Pr(Indicator|Down) * Pr(Down) / Pr(Indicator|Down) * Pr(Down) + Pr(Indicator|Up) * Pr(Up)

Reading The Oscillator

  • Green is the probability of prices breaking up
  • Red is the probability of prices breaking down
  • When either green or red is flatlining ceiling, immediately on the next candle when the probability decreases go short or long based on which direction you're observing - Strong Signal
  • When either green or red is flatlining ceiling, take no action while it's ceiled
  • Usually when either green or red is flatlining bottom, the next candle when the probability increases, immediately take a short long position based on the direction you're observing - Weak Signal
  • When either green or red is flatlining bottom, take no action while it's bottomed


Alerts

Use Once per Bar option when generating alerts.
Release Notes
snapshot

This update changes how the oscillator is read. After tinkering with the indicator for a bit, I added a new probability called Prime Probability which is a combination of bot classifiers probabilities. A lower threshold is also added to define uncertainty or sideways movements. By adding these, we read the oscillator like below:

  • Sideways or uncertainty is represented when the lower threshold area is colored gray, be very cautious when entering trades in this condition
  • When prime probability changes from zero to a value above the lower threshold, a long signal is fired
  • When breaking up probability changes from 100% to a value less than 100%, a long signal is fired
  • When prime probability changes from a value above the lower threshold to zero, a short signal is fired
  • When breaking down probability changes from 100% to a value less than 100%, a short signal is fired


All the long and short signals are strong signal provided that at the time the signal fires, probabilities tells us we're not in a sideways movement. Weaker signals although excluded from the indicator signals can be determined like below:

  • When prime probability changes from zero or close to zero to a value within the lower threshold, this is a weak long signal
  • When breaking up probability changes from a value above the lower threshold to a value less than the previous candle, this is a weak long signal
  • When prime probability changes from a value within the lower threshold to zero or close to zero, this is a weak short signal
  • When breaking down probability changes from a value above the lower threshold to a value less than the previous, this is a weak short signal


Play around with the lower threshold to find a suitable value. The default values in this indicator are derived from BTCUSDT in Binance at 1H timeframe.
Release Notes
This update adds Bill WIlliams indicators to confirm moves. It is disabled by default, more readings about Bill Williams indicators on my blog post below:

https://bango29.com/studying-chaos-and-learning-from-bill-williams/
bayesianbreakoutOscillatorsprobabilitiesTrend Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Also on:

Disclaimer