OPEN-SOURCE SCRIPT

Rolling Window Geometric Brownian Motion Projections

📊 Rolling GBM Projections + EV & Adjustable Confidence Bands
Overview

The Rolling GBM Projections + EV & Adjustable Confidence Bands indicator provides traders with a robust, dynamic tool to model and project future price movements using Geometric Brownian Motion (GBM). By combining GBM-based simulations, expected value (EV) calculations, and customizable confidence bands, this indicator offers valuable insights for decision-making and risk management.
Key Features

Rolling GBM Projections: Simulate potential future price paths based on drift (μμ) and volatility (σσ).
Expected Value (EV) Line: Represents the average projection of simulated price paths.
Confidence Bands: Define ranges where the price is expected to remain, adjustable from 51% to 99%.
Simulation Lines: Visualize individual GBM paths for detailed analysis.
EV of EV Line: A smoothed trend of the EV, offering additional clarity on price dynamics.
Customizable Lookback Periods: Adjust the rolling lookback periods for drift and volatility calculations.

Mathematical Foundation
1. Geometric Brownian Motion (GBM)

GBM is a mathematical model used to simulate the random movement of asset prices, described by the following stochastic differential equation:
dSt=μStdt+σStdWt
dSt​=μSt​dt+σSt​dWt​

Where:

StSt​: Price at time tt
μμ: Drift term (expected return)
σσ: Volatility (standard deviation of returns)
dWtdWt​: Wiener process (standard Brownian motion)

2. Drift (μμ) and Volatility (σσ)

Drift (μμ): Represents the average logarithmic return of the asset. Calculated using a simple moving average (SMA) over a rolling lookback period.
μ=SMA(ln⁡(St/St−1),Lookback Drift)
μ=SMA(ln(St​/St−1​),Lookback Drift)

Volatility (σσ): Measures the standard deviation of logarithmic returns over a rolling lookback period.
σ=STD(ln⁡(St/St−1),Lookback Volatility)
σ=STD(ln(St​/St−1​),Lookback Volatility)

3. Price Simulation Using GBM

The GBM formula for simulating future prices is:
St+Δt=St×e(μ−12σ2)Δt+σϵΔt
St+Δt​=St​×e(μ−21​σ2)Δt+σϵΔt


Where:

ϵϵ: Random variable from a standard normal distribution (N(0,1)N(0,1)).

4. Confidence Bands

Confidence bands are determined using the Z-score corresponding to a user-defined confidence percentage (CC):
Upper Band=EV+Z⋅σ
Upper Band=EV+Z⋅σ
Lower Band=EV−Z⋅σ
Lower Band=EV−Z⋅σ

The Z-score is computed using an inverse normal distribution function, approximating the relationship between confidence and standard deviations.
Methodology

Rolling Drift and Volatility:
Drift and volatility are calculated using logarithmic returns over user-defined rolling lookback periods (default: μ=20μ=20, σ=16σ=16).
Drift defines the overall directional tendency, while volatility determines the randomness and variability of price movements.

Simulations:
Multiple GBM paths (default: 30) are generated for a specified number of projection candles (default: 12).
Each path is influenced by the current drift and volatility, incorporating random shocks to simulate real-world price dynamics.

Expected Value (EV):
The EV is calculated as the average of all simulated paths for each projection step, offering a statistical mean of potential price outcomes.

Confidence Bands:
The upper and lower bounds of the confidence bands are derived using the Z-score corresponding to the selected confidence percentage (e.g., 68%, 95%).

EV of EV:
A running average of the EV values, providing a smoothed perspective of price trends over the projection horizon.

Indicator Functionality

User Inputs:
Drift Lookback (Bars): Define the number of bars for rolling drift calculation (default: 20).
Volatility Lookback (Bars): Define the number of bars for rolling volatility calculation (default: 16).
Projection Candles (Bars): Set the number of bars to project future prices (default: 12).
Number of Simulations: Specify the number of GBM paths to simulate (default: 30).
Confidence Percentage: Input the desired confidence level for bands (default: 68%, adjustable from 51% to 99%).

Visualization Components:
Simulation Lines (Blue): Display individual GBM paths to visualize potential price scenarios.
Expected Value (EV) Line (Orange): Highlight the mean projection of all simulated paths.
Confidence Bands (Green & Red): Show the upper and lower confidence limits.
EV of EV Line (Orange Dashed): Provide a smoothed trendline of the EV values.
Current Price (White): Overlay the real-time price for context.

Display Toggles:
Enable or disable components (e.g., simulation lines, EV line, confidence bands) based on preference.

Practical Applications

Risk Management:
Utilize confidence bands to set stop-loss levels and manage trade risk effectively.
Use narrower confidence intervals (e.g., 50%) for aggressive strategies or wider intervals (e.g., 95%) for conservative approaches.

Trend Analysis:
Observe the EV and EV of EV lines to identify overarching trends and potential reversals.

Scenario Planning:
Analyze simulation lines to explore potential outcomes under varying market conditions.

Statistical Insights:
Leverage confidence bands to understand the statistical likelihood of price movements.

How to Use

Add the Indicator:
Copy the script into the TradingView Pine Editor, save it, and apply it to your chart.

Customize Settings:
Adjust the lookback periods for drift and volatility.
Define the number of projection candles and simulations.
Set the confidence percentage to tailor the bands to your strategy.

Interpret the Visualization:
Use the EV and confidence bands to guide trade entry, exit, and position sizing decisions.
Combine with other indicators for a holistic trading strategy.

Disclaimer

This indicator is a mathematical and statistical tool. It does not guarantee future performance.
Use it in conjunction with other forms of analysis and always trade responsibly.

Happy Trading! 🚀
forecastingstatisticsTrend Analysis

สคริปต์โอเพนซอร์ซ

ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถเข้าใจและตรวจสอบได้ ต้องขอบคุณผู้เขียน! ที่ให้คุณใช้ได้ฟรี แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้

ต้องการที่จะใช้สคริปต์นี้บนชาร์ตใช่ไหม?

คำจำกัดสิทธิ์ความรับผิดชอบ