OPEN-SOURCE SCRIPT
Martingale Engine [point algo]

Martingale Engine
The Martingale Engine is an educational quantitative analysis tool that visualizes whether a price series behaves more like a martingale, sub-martingale, or super-martingale using concepts from stochastic processes and financial mathematics.
The script combines Doob Decomposition, Variance Ratio Testing, and Quadratic Variation into a single dashboard for studying the statistical characteristics of market returns.
It is intended for analysis and research rather than providing standalone trading signals.
Mathematical Background
For a stochastic process Xt, Doob's Decomposition states:
X = M + A
where
This indicator estimates both components from logarithmic returns and displays how much of the recent price movement is explained by drift versus random innovation.
Included Models
1. Doob Decomposition
The indicator estimates
and separates total movement into
This helps visualize whether recent price behavior has been primarily trend-driven or innovation-driven.
2. Martingale Probability Bands
Using the estimated innovation volatility, the script builds statistical bands around the estimated drift.
Displayed bands include
These bands represent expected dispersion under the current volatility estimate and should not be interpreted as support, resistance, or guaranteed price targets.
3. Forward Martingale Cone
4. Variance Ratio Test (Lo–MacKinlay)
The script performs the Variance Ratio Test over multiple aggregation periods.
Evaluated windows:
Interpretation:
Corresponding z-statistics are also displayed for reference.
5. Quadratic Variation
Quadratic Variation measures accumulated return variability over the selected estimation window.
The dashboard reports
along with a realized volatility band derived from the estimated variation.
Process Classification
Based on the estimated annualized drift, the script classifies the process as:
Sub-Martingale
Expected future value exceeds the current value.
Martingale
Expected future value is approximately equal to the current value.
Super-Martingale
Expected future value is below the current value.
This classification reflects the estimated statistical process over the selected lookback window and may change as new data becomes available.
Dashboard
The integrated dashboard displays:
Inputs
Estimation Window : Number of historical bars used for calculations.
Doob Drift EMA Length : EMA length used to estimate conditional drift.
Forward Projection : Length of the projected martingale cone.
Show Doob Decomposition : Displays estimated drift and statistical bands.
Show Martingale Bounds Cone : Displays the forward projection cone.
Show Quadratic Variation Band : Displays the realized volatility band.
Intended Use
This indicator may be useful for:
Important Notes
References
This script is inspired by concepts from:
Disclaimer
This indicator is provided for educational and analytical purposes only. It does not provide investment advice or guarantee future market performance. Users should perform their own research and apply appropriate risk management before making trading decisions.
The Martingale Engine is an educational quantitative analysis tool that visualizes whether a price series behaves more like a martingale, sub-martingale, or super-martingale using concepts from stochastic processes and financial mathematics.
The script combines Doob Decomposition, Variance Ratio Testing, and Quadratic Variation into a single dashboard for studying the statistical characteristics of market returns.
It is intended for analysis and research rather than providing standalone trading signals.
Mathematical Background
For a stochastic process Xt, Doob's Decomposition states:
X = M + A
where
- M is the martingale (unpredictable innovation) component.
- A is the predictable drift component.
This indicator estimates both components from logarithmic returns and displays how much of the recent price movement is explained by drift versus random innovation.
Included Models
1. Doob Decomposition
The indicator estimates
- Conditional Drift (μ)
- Martingale Innovation (ε)
and separates total movement into
- Predictable component
- Random component
This helps visualize whether recent price behavior has been primarily trend-driven or innovation-driven.
2. Martingale Probability Bands
Using the estimated innovation volatility, the script builds statistical bands around the estimated drift.
Displayed bands include
- ±1 Standard Deviation
- ±2 Standard Deviation
These bands represent expected dispersion under the current volatility estimate and should not be interpreted as support, resistance, or guaranteed price targets.
3. Forward Martingale Cone
- The indicator projects a forward volatility cone based on the current innovation variance.
- The cone illustrates how expected dispersion increases over time under a martingale assumption.
- This feature is intended for visualization and educational analysis.
4. Variance Ratio Test (Lo–MacKinlay)
The script performs the Variance Ratio Test over multiple aggregation periods.
Evaluated windows:
- q = 2
- q = 4
- q = 8
- q = 16
Interpretation:
- VR > 1 → Momentum tendency
- VR < 1 → Mean-reverting tendency
- VR ≈ 1 → Random walk behavior
Corresponding z-statistics are also displayed for reference.
5. Quadratic Variation
Quadratic Variation measures accumulated return variability over the selected estimation window.
The dashboard reports
- Total Quadratic Variation
- Martingale Component
- Drift Component
along with a realized volatility band derived from the estimated variation.
Process Classification
Based on the estimated annualized drift, the script classifies the process as:
Sub-Martingale
Expected future value exceeds the current value.
Martingale
Expected future value is approximately equal to the current value.
Super-Martingale
Expected future value is below the current value.
This classification reflects the estimated statistical process over the selected lookback window and may change as new data becomes available.
Dashboard
The integrated dashboard displays:
- Doob decomposition
- Drift contribution
- Innovation contribution
- Variance Ratio statistics
- z-statistics
- Quadratic Variation
- Estimated realized volatility
- Annualized drift
- Estimated innovation volatility
- Current process classification
Inputs
Estimation Window : Number of historical bars used for calculations.
Doob Drift EMA Length : EMA length used to estimate conditional drift.
Forward Projection : Length of the projected martingale cone.
Show Doob Decomposition : Displays estimated drift and statistical bands.
Show Martingale Bounds Cone : Displays the forward projection cone.
Show Quadratic Variation Band : Displays the realized volatility band.
Intended Use
This indicator may be useful for:
- Quantitative market research
- Studying stochastic price behavior
- Drift analysis
- Random walk analysis
- Mean reversion research
- Momentum research
- Educational demonstrations of martingale theory
- Financial mathematics
Important Notes
- The calculations are based on historical price data.
- Statistical estimates depend on the selected lookback period.
- The displayed probability bands are model-based estimates and are not predictive forecasts.
- The indicator should be used alongside other forms of technical or quantitative analysis.
- No statistical model can fully describe future market behavior.
References
This script is inspired by concepts from:
- Doob Decomposition
- Martingale Theory
- Quadratic Variation
- Lo & MacKinlay Variance Ratio Test
- Stochastic Processes
- Financial Econometrics
Disclaimer
This indicator is provided for educational and analytical purposes only. It does not provide investment advice or guarantee future market performance. Users should perform their own research and apply appropriate risk management before making trading decisions.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
Unlock powerful tools & indicators → pointalgo.com
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
Unlock powerful tools & indicators → pointalgo.com
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.