ChartPrime

Bayesian Trend Indicator [ChartPrime]

ChartPrime Updated   
Bayesian Trend Indicator

Overview:
In probability theory and statistics, Bayes' theorem (alternatively Bayes' law or Bayes' rule), named after Thomas Bayes, describes the probability of an event, based on prior knowledge of conditions that might be related to the event.

The "Bayesian Trend Indicator" is a sophisticated technical analysis tool designed to assess the direction of price trends in financial markets. It combines the principles of Bayesian probability theory with moving average analysis to provide traders with a comprehensive understanding of market sentiment and potential trend reversals.

At its core, the indicator utilizes multiple moving averages, including the Exponential Moving Average (EMA), Simple Moving Average (SMA), Double Exponential Moving Average (DEMA), and Volume Weighted Moving Average (VWMA). These moving averages are calculated based on user-defined parameters such as length and gap length, allowing traders to customize the indicator to suit their trading strategies and preferences.

The indicator begins by calculating the trend for both fast and slow moving averages using a Smoothed Gradient Signal Function. This function assigns a numerical value to each data point based on its relationship with historical data, indicating the strength and direction of the trend.
// Smoothed Gradient Signal Function 
sig(float src, gap)=>
    ta.ema(source >= src[gap]   ? 1   : 
     source >= src[gap-1] ? 0.9 :
     source >= src[gap-2] ? 0.8 :
     source >= src[gap-3] ? 0.7 :
     source >= src[gap-4] ? 0.6 :
     source >= src[gap-5] ? 0.5 :
     source >= src[gap-6] ? 0.4 :
     source >= src[gap-7] ? 0.3 :
     source >= src[gap-8] ? 0.2 :
     source >= src[gap-9] ? 0.1 :
      0, 4)

Next, the indicator calculates prior probabilities using the trend information from the slow moving averages and likelihood probabilities using the trend information from the fast moving averages. These probabilities represent the likelihood of an uptrend or downtrend based on historical data.
// Define prior probabilities using moving averages
prior_up = (ema_trend + sma_trend + dema_trend + vwma_trend) / 4
prior_down = 1 - prior_up

// Define likelihoods using faster moving averages
likelihood_up = (ema_trend_fast + sma_trend_fast + dema_trend_fast + vwma_trend_fast) / 4
likelihood_down = 1 - likelihood_up


Using Bayes' theorem, the indicator then combines the prior and likelihood probabilities to calculate posterior probabilities, which reflect the updated probability of an uptrend or downtrend given the current market conditions. These posterior probabilities serve as a key signal for traders, informing them about the prevailing market sentiment and potential trend reversals.

// Calculate posterior probabilities using Bayes' theorem
posterior_up = prior_up * likelihood_up 
                             / 
               (prior_up * likelihood_up + prior_down * likelihood_down)
                 


Key Features:

◆ The trend direction:
To visually represent the trend direction, the indicator colors the bars on the chart based on the posterior probabilities. Bars are colored green to indicate an uptrend when the posterior probability is greater than 0.5 (>50%), while bars are colored red to indicate a downtrend when the posterior probability is less than 0.5 (<50%).

◆ Dashboard on the chart
Additionally, the indicator displays a dashboard on the chart, providing traders with detailed information about the probability of an uptrend, as well as the trends for each type of moving average. This dashboard serves as a valuable reference for traders to monitor trend strength and make informed trading decisions.

◆ Probability labels and signals:
Furthermore, the indicator includes probability labels and signals, which are displayed near the corresponding bars on the chart. These labels indicate the posterior probability of a trend, while small diamonds above or below bars indicate crossover or crossunder events when the posterior probability crosses the 0.5 threshold (50%).
The posterior probability of a trend
Crossover or Crossunder events

◆ User Inputs
  • Source:
    Description: Defines the price source for the indicator's calculations. Users can select between different price values like close, open, high, low, etc.
  • MA's Length:
    Description: Sets the length for the moving averages used in the trend calculations. A larger length will smooth out the moving averages, making the indicator less sensitive to short-term fluctuations.
  • Gap Length Between Fast and Slow MA's:
    Description: Determines the difference in lengths between the slow and fast moving averages. A higher gap length will increase the difference, potentially identifying stronger trend signals.
  • Gap Signals:
    Description: Defines the gap used for the smoothed gradient signal function. This parameter affects the sensitivity of the trend signals by setting the number of bars used in the signal calculations.



In summary, the "Bayesian Trend Indicator" is a powerful tool that leverages Bayesian probability theory and moving average analysis to help traders identify trend direction, assess market sentiment, and make informed trading decisions in various financial markets.
Release Notes:
Updated Thumbnail

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 a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?