Anchored GBM (Geometric Brownian Motion) Projections + EV & Confidence Bands
Version: Pine Script v6
Overlay: Yes
Author: [Gary Sebastian Brown III]
Published On: [12-31-24]
Overview
The Anchored GBM Projections + EV & Confidence Bands indicator leverages the Geometric Brownian Motion (GBM) model to project future price movements based on historical data. By simulating multiple potential future price paths, it provides traders with insights into possible price trajectories, their expected values, and confidence intervals. Additionally, it offers a "Mean of EV" (EV of EV) line, representing the running average of expected values across the projection period.
Key Features
Anchor Time Setup:
Define a specific point in time from which the projections commence.
By default, it uses the current bar's timestamp but can be customized.
Projection Parameters:
Projection Candles (Bars): Determines the number of future bars (time periods) to project.
Number of Simulations: Specifies how many GBM paths to simulate, ensuring statistical relevance via the Central Limit Theorem (CLT).
Display Toggles:
Simulation Lines: Visual representation of individual GBM simulation paths.
Expected Value (EV) Line: The average price across all simulations at each projection bar.
Upper & Lower Confidence Bands: 95% confidence intervals indicating potential price boundaries.
EV of EV Line: Running average of EV values, providing a smoothed central tendency across the projection period. Additionally, this line often acts as an indicator of trend direction.
Visualization:
Clear and distinguishable lines with customizable colors and styles.
Overlayed on the price chart for direct comparison with actual price movements.
Mathematical Foundation
Geometric Brownian Motion (GBM):
Definition: GBM is a continuous-time stochastic process used to model stock prices. It assumes that the logarithm of the stock price follows a Brownian motion with drift.
Equation:
S(t)=S0⋅e(μ−12σ2)t+σW(t)
S(t)=S0⋅e(μ−21σ2)t+σW(t) Where:
S(t)S(t) = Stock price at time tt
S0S0 = Initial stock price
μμ = Drift coefficient (average return)
σσ = Volatility coefficient (standard deviation of returns)
W(t)W(t) = Wiener process (standard Brownian motion)
Drift (μμ) and Volatility (σσ):
Drift (μμ) represents the expected return of the stock.
Volatility (σσ) measures the stock's price fluctuation intensity.
Central Limit Theorem (CLT):
Principle: With a sufficiently large number of independent simulations, the distribution of the sample mean (EV) approaches a normal distribution, regardless of the underlying distribution.
Application: Ensures that the EV and confidence bands are statistically reliable.
Expected Value (EV) and Confidence Bands:
EV: The mean price across all simulations at each projection bar.
Confidence Bands: Range within which the actual price is expected to lie with a specified probability (e.g., 95%).
EV of EV (Mean of Sample Means):
Definition: Represents the running average of EV values across the projection period, offering a smoothed central tendency.
Methodology
Anchor Time Setup:
The indicator starts projecting from a user-defined Anchor Time. If not customized, it defaults to the current bar's timestamp.
Purpose: Allows users to analyze projections from a specific historical point or the latest market data.
Calculating Drift and Volatility:
Returns Calculation: Computes the logarithmic returns from the Anchor Time to the current bar.
returns=ln(StSt−1)
returns=ln(St−1St)
Drift (μμ): Calculated as the simple moving average (SMA) of returns over the period since the Anchor Time.
Volatility (σσ): Determined using the standard deviation (stdev) of returns over the same period.
Simulation Generation:
Number of Simulations: The user defines how many GBM paths to simulate (e.g., 30).
Projection Candles: Determines the number of future bars to project (e.g., 12).
Process:
For each simulation:
Start from the current close price.
For each projection bar:
Generate a random number zz from a standard normal distribution.
Calculate the next price using the GBM formula:
St+1=St⋅e(μ−12σ2)+σz
St+1=St⋅e(μ−21σ2)+σz
Store the projected price in an array.
Expected Value (EV) and Confidence Bands Calculation:
EV Path: At each projection bar, compute the mean of all simulated prices.
Variance and Standard Deviation: Calculate the variance and standard deviation of simulated prices to determine the confidence intervals.
Confidence Bands: Using the standard normal z-score (1.96 for 95% confidence), establish upper and lower bounds:
Upper Band=EV+z⋅σEV
Upper Band=EV+z⋅σEV
Lower Band=EV−z⋅σEV
Lower Band=EV−z⋅σEV
EV of EV (Running Average of EV Values):
Calculation: For each projection bar, compute the average of all EV[j] values up to that bar.
EV of EV[j]=1j+1∑k=0jEV[k]
EV of EV[j]=j+11k=0∑jEV[k]
Visualization: Plotted as a dynamic line reflecting the evolving average EV across the projection period.
Visualization Elements
Simulation Lines:
Appearance: Semi-transparent blue lines representing individual GBM simulation paths.
Purpose: Illustrate a range of possible future price trajectories based on current drift and volatility.
Expected Value (EV) Line:
Appearance: Solid orange line.
Purpose: Shows the average projected price at each future bar across all simulations.
Confidence Bands:
Upper Band: Dashed green line indicating the upper 95% confidence boundary.
Lower Band: Dashed red line indicating the lower 95% confidence boundary.
Purpose: Highlight the range within which the price is statistically expected to remain with 95% confidence.
EV of EV Line:
Appearance: Dashed purple line.
Purpose: Displays the running average of EV values, providing a smoothed trend of the central tendency across the projection period. As the mean of sample means it approximates the population mean (i.e. the trend since the anchor point.)
Current Price:
Appearance: Semi-transparent white line.
Purpose: Serves as a reference point for comparing actual price movements against projected paths.
Usage Instructions
Configuring User Inputs:
Anchor Time:
Set to a specific timestamp to start projections from a historical point or leave it as default to use the current bar's time.
Projection Candles (Bars):
Define the number of future bars to project (e.g., 12). Adjust based on your trading timeframe and analysis needs.
Number of Simulations:
Specify the number of GBM paths to simulate (e.g., 30). Higher numbers yield more accurate EV and confidence bands but may impact performance.
Display Toggles:
Show Simulation Lines: Toggle to display or hide individual GBM simulation paths.
Show Expected Value Line: Toggle to display or hide the EV path.
Show Upper Confidence Band: Toggle to display or hide the upper confidence boundary.
Show Lower Confidence Band: Toggle to display or hide the lower confidence boundary.
Show EV of EV Line: Toggle to display or hide the running average of EV values.
Managing TradingView's Object Limits:
Understanding Limits:
TradingView imposes a limit on the number of graphical objects (e.g., lines) that can be rendered. High values for projection candles and simulations can quickly consume these limits. TradingView appears to only allow a total of 55 candles to be projected, so if you want to see two complete lines, you would have to set the projection length to 27: since 27 * 2 = 54 and 54 < 55.
Optimizing Performance:
Use Toggles: Enable only the necessary visual elements. For instance, disable simulation lines and confidence bands when focusing on the EV and EV of EV lines. You can also use the maximum projection length of 55 with the lower limit confidence band as the only line, visualizing a long horizon for your risk.
Adjust Parameters: Lower the number of projection candles or simulations to stay within object limits without compromising essential insights.
Interpreting the Indicator:
Simulation Lines (Blue):
Represent individual potential future price paths based on GBM. A wider spread indicates higher volatility.
Expected Value (EV) Line (Goldenrod):
Shows the mean projected price at each future bar, providing a central trend.
Confidence Bands (Green & Red):
Indicate the statistical range (95% confidence) within which the price is expected to remain.
EV of EV Line (Dotted Line - Goldenrod):
Reflects the running average of EV values, offering a smoothed perspective of expected price trends over the projection period.
Current Price (White):
Serves as a benchmark for assessing how actual prices compare to projected paths.
Practical Applications
Risk Management:
Confidence Bands: Help in identifying potential support and resistance levels based on statistical confidence intervals.
EV Path: Assists in setting realistic target prices and stop-loss levels aligned with projected expectations.
Trend Analysis:
EV of EV Line: Offers a smoothed trendline, aiding in identifying overarching market directions amidst price volatility. Indicative of the population mean/overall trend of the data since your anchor point.
Scenario Planning:
Simulation Lines: Enable traders to visualize multiple potential outcomes, fostering better decision-making under uncertainty.
Performance Evaluation:
Comparing Actual vs. Projected Prices: Assess how actual price movements align with projected scenarios, refining trading strategies over time.
Mathematical and Statistical Insights
Simulation Integrity:
Independence: Each simulation path is generated independently, ensuring unbiased and diverse projections.
Randomness: Utilizes a Gaussian random number generator to introduce variability in diffusion terms, mimicking real market randomness.
Statistical Reliability:
Central Limit Theorem (CLT): By simulating a sufficient number of paths (e.g., 30), the sample mean (EV) converges to the population mean, ensuring reliable EV and confidence band calculations.
Variance Calculation: Accurate computation of variance from simulation data ensures precise confidence intervals.
Dynamic Projections:
Running Average (EV of EV): Provides a cumulative perspective, allowing traders to observe how the average expectation evolves as the projection progresses.
Customization and Enhancements
Adjustable Parameters:
Tailor the projection length and simulation count to match your trading style and analysis depth.
Visual Customization:
Modify line colors, styles, and transparency to enhance clarity and fit chart aesthetics.
Extended Statistical Metrics:
Future iterations can incorporate additional metrics like median projections, skewness, or alternative confidence intervals.
Dynamic Recalculation:
Implement logic to automatically update projections as new data becomes available, ensuring real-time relevance.
Performance Considerations
Object Count Management:
High simulation counts and extended projection periods can lead to a significant number of graphical objects, potentially slowing down chart performance.
Solution: Utilize display toggles effectively and optimize projection parameters to balance detail with performance.
Computational Efficiency:
The script employs efficient array handling and conditional plotting to minimize unnecessary computations and object creation.
Conclusion
The Anchored GBM Projections + EV & Confidence Bands indicator is a robust tool for traders seeking to forecast potential future price movements using statistical models. By integrating Geometric Brownian Motion simulations with expected value calculations and confidence intervals, it offers a comprehensive view of possible market scenarios. The addition of the "EV of EV" line further enhances analytical depth by providing a running average of expected values, aiding in trend identification and strategic decision-making.
Hope it helps!