OPEN-SOURCE SCRIPT

LGMM (flat buffers) — multivariate poly + latent states

441
LGMM POLYNOMIAL BANDS — DISCOVER THE MARKET’S HIDDEN STATES

Overview

Latent-Gaussian-Mixture-Models (LGMMs) view price action as a mix of several invisible regimes: trending up, drifting sideways, sudden volatility spikes, and so on.
A Gaussian Mixture learns these states directly from data and outputs, for every bar, the probability that the market is in each state.
This indicator feeds those probabilities into a rolling polynomial regression that draws a fair-value line, then builds adaptive upper and lower bands.
Band width expands when recent residuals are large *and* when the state mix is uncertain, and contracts when price is calm or one regime clearly dominates.
Crossing back into the band from below generates a buy flag; crossing back into the band from above generates a sell flag (or take-profit for longs).

Key Inputs

  • Price source – default is Close; you can choose HL2, OHLC4, etc.
  • Training window (bars) – look-back length for every retrain. 252 bars (one trading year) is a balanced default for US stocks on daily timeframe. Use fewer bars for intraday charts (say 7*24=168 for 1H bars on crypto), more for weekly periods.
  • Polynomial degree – 1 for a straight trend line, 2 for a curved fit. Curved fits are better when the symbol shows persistent drift.
  • Hidden states K – number of regimes the mixture tracks (1 to 3). Three states often map well to up-trend, chop, down-trend.
  • Band width ×σ – multiplier on the entropy-weighted standard deviation. Smaller values (1.5-2) give more trades; larger values (2.5-3) give fewer, higher-conviction trades.
  • Offline μ,σ pairs (optional) – paste component means and sigmas from an offline LGMM (format: mu1,sigma1;mu2,sigma2;…). Leave blank to let the script use its built-in approximation.


Quick Start

  1. Add the indicator to a chart and wait until the initial Training window has filled.
  2. Watch for green BUY triangles when price closes back above the lower band and red SELL triangles when price closes back below the upper band.
  3. Fine-tune:
    – Increase Training window to reduce noise.
    – Decrease Band width ×σ for more frequent signals.
    – Experiment with Hidden states K; more states capture richer behaviour but need longer windows to stay reliable.


Tips

  • Bands widen automatically in chaotic periods and tighten when one regime dominates.
  • Combine with a volume filter or a higher-time-frame trend to reduce whipsaws.
  • If you already run an LGMM in Python or Matlab, paste its component parameters for a perfect match between your back-test and the TradingView plot.
  • Works on all markets and time-frames, provided you have at least five times the Training window’s bars in history.


Happy trading!

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.