OPEN-SOURCE SCRIPT

Martingale Engine [point algo]

306
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
  • 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
  1. The indicator projects a forward volatility cone based on the current innovation variance.
  2. The cone illustrates how expected dispersion increases over time under a martingale assumption.
  3. 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.

免責事項

これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。