PROTECTED SOURCE SCRIPT

Bayesian Average

123
This indicator calculates a statistically informed average of the price data using the Bayesian update rule. It overlays directly on the price chart of the trading instrument.
Inputs:
* Length: An integer input (default is 20) that determines the period over which the rolling mean and variance of the closing prices are calculated. This essentially sets the lookback window for recent price action.
* Prior Mean: A floating-point number input (default is 0.0) representing the initial guess or prior belief about the average price of the asset before observing any price data within the specified Length.
* Prior Variance: A floating-point number input (default is 1.0) that quantifies the uncertainty associated with the Prior Mean. A higher value indicates less confidence in the initial guess.
Calculations:
* Rolling Mean and Variance: The script first computes the simple moving average (rollingMean) and the sample variance (rollingVariance) of the closing price using the ta.sma and ta.variance functions over the length period.
* Bayesian Update: The indicator then applies the Bayesian update formulas to combine the prior beliefs with the information extracted from the recent price data:
* Posterior Variance: This value represents the updated uncertainty about the average price after considering the recent price fluctuations. It is calculated by combining the inverse of the Prior Variance with the precision of the data (number of observations divided by the Rolling Variance).
* Posterior Mean (Bayesian Average): This is the main output of the indicator. It represents the refined estimate of the average price, taking into account both the initial Prior Mean and the information from the Rolling Mean and Rolling Variance. The formula weights the Prior Mean and the Rolling Mean based on their respective precisions (inverse of their variances).
Plotting:
* Bayesian Average (Blue Line): The indicator plots the calculated posteriorMean, which is the Bayesian average of the price, as a blue line on the chart.
* Rolling Mean (Gray Line - Optional): For comparison, the script optionally plots the traditional simple moving average (rollingMean) as a gray line.
* Prior Mean (Dotted Red Line - Optional): A horizontal line representing the initial priorMean is optionally drawn on the chart as a red dotted line, serving as a reference.
* Posterior Variance (Orange Line - Optional): The posteriorVariance, representing the uncertainty, is also optionally plotted as an orange line.
In essence, this indicator attempts to provide a more statistically sound and potentially smoother representation of the average price by blending an initial belief with observed market data. The Prior Mean and Prior Variance allow users to incorporate their existing knowledge or assumptions into the calculation, while the Bayesian update mechanism adjusts these beliefs based on the recent price action.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.