OPEN-SOURCE SCRIPT

[SGM Return Distribution]

Code Description

This Pine Script™ is designed to analyze the distribution of historical returns of a financial asset and project future confidence levels. It uses statistical techniques to estimate the probability of winning and losing as well as displaying confidence bands and distribution statistics.

User Entries

Length (252): The number of days used to calculate statistics.
Offset (20): Offset used to project future values.
Projection Days (10): Number of days projected into the future.
Smoothing Confidence Levels (10): Smoothing confidence bands.

Display Settings

Plot Distribution: Shows the distribution of returns.
Show Probabilities: Shows winning and losing probabilities.
Show Distribution Stats: Shows distribution statistics.
Show Confidence Bands: Shows confidence bands.
Show Confidence Lines: Shows confidence lines.
Calculations and Features

Distribution of Yields:

Calculates logarithmic returns and their statistics (average, volatility, skewness, kurtosis).
Projects the average and volatility over the projected number of days.
Displays the distribution of returns as a histogram.

Confidence Interval:

Uses the inv_norm function to calculate Z scores for different confidence levels.
Calculates the upper and lower bounds of the confidence bands.

Probability Display:

Calculates and displays win and loss probabilities based on the distribution of returns.

Statistics Display:

Shows key statistics such as mean, volatility, skewness and kurtosis.

Trust Bands and Lines:

Shows confidence bands and lines based on calculated confidence levels.

snapshot

snapshot

Mathematical Assumptions Used

Logarithmic Returns: Returns are calculated using the logarithm of prices, which is common for financial time series because it makes returns independent of price level.

Normal Distribution for Confidence Bands: Confidence interval calculations are based on the assumption that returns follow a normal distribution.

Average and Volatility Projection: Average returns and volatility are projected over a future period assuming they remain constant.

Skewness and Kurtosis: Although these measures are calculated for understanding the distribution of returns, they are not used in box projections but can provide additional information about the distribution of historical returns.

snapshot

snapshot

Use in Trading

Risk Estimation: Confidence bands can help estimate likely future price levels, which is crucial for determining strike levels and risk management.

Risk Management: Use confidence bands to set stop-loss and take-profit levels.
Probability Analysis: Win and loss probabilities can help assess a position's likelihood of success.

Potential Problems

Assumption of Normality for Confidence Bands: Financial returns do not always follow a normal distribution, especially in the presence of extreme events (fat tails).

Stationarity: Assuming that return statistics (average, volatility) remain constant over time can be erroneous in volatile market periods.

Limited Historical Data: Using a limited history (252 days) may not capture all possible behaviors of the asset.

Input Parameters: Results can be sensitive to the input parameters chosen (length, offset, etc.).

forecastingoptionsstatistics

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?


Sigaud | Junior Quantitative Trader & Developer

Combining technical expertise with analytical precision.
Gaining experience and growing in the field.

📧 Contact: from the website
Also on:

Disclaimer