OPEN-SOURCE SCRIPT
Diupdate

Liquidity Surge Forecast with Markov Chains [TechnicalZen]

1 415
Clear direction from Markov Chains confirmed projections.

Publishing this v3 with all the enhancements users desired and more. Thank you for your feedback.

What This Is

A 3D liquidity-and-momentum visualization that tells you where the market is heading right now, how long the current state is likely to hold, and when the next regime change is expected — all backed by a 2nd-order Markov chain that learns from your chart's own history.

Two independent systems — Money Flow (MFI-driven) and Price Current (Hull-VWMA or signed-ADX) — render as layered dotted carpets inside a bounded 3D box. When both systems agree on direction AND the Markov chain confirms, a whale surfaces — 🐳 bullish, 🐋 bearish. Chop gets a shark 🦈. Sideways drift gets a crab 🦀. And when the Markov chain predicts an imminent regime transition, a small hatchling whale appears before confluence forms.

The Current State row tells you, in one line, exactly what to expect next.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Built On



Same 3D engine. Same dual-system confluence as the foundation. Then: regime classification, Markov statistical learning, current-state intelligence, and a live win-rate scoreboard on top.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Clear Direction — At A Glance

Most indicators show you lines and ask you to interpret. This one tells you plainly, in a single dashboard row:

  • What regime you're in right now — 🐳 Bull, 🐋 Bear, 🦈 Chop-zone, or 🦀 Sideways
  • How long it's been held — in bars
  • Whether the regime is BALANCED or IMBALANCED — based on the Markov chain's next-bar probabilities
  • When the next regime change is expected — in bars, computed from the stay-probability
  • Which direction the market leans next — the highest-probability non-current state


Example readouts:

  • "Current State: 🐳 Bull held 5b · IMBALANCED — stay 68%, change expected in ~3b · next lean: 🦀 Sideways"
  • "Current State: 🦀 Sideways held 12b · BALANCED — change imminent · next lean: 🐳 Bull"
  • "Current State: 🐋 Bear held 2b · IMBALANCED — stay 82%, change expected in ~5b · next lean: 🦀 Sideways"


No interpretation required. You read the line, you know where you are, you know what to expect.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

New Features — And Why Each Exists

1. Current State intelligence row

Why: Confluence indicators tell you WHEN a signal fires. They don't tell you "how solid is the current regime," "is a change coming," or "how long do I have before conditions flip." The Current State row answers all three in one glance.

The balanced / imbalanced distinction matters most:

  • BALANCED — the three next-bar probabilities are close to 1/3 each. No clear direction. A regime change is imminent (could go anywhere). Trade lighter, wait for resolution.
  • IMBALANCED — one direction dominates. Regime has a preferred path. The stay-probability tells you how long it's likely to persist; the next-lean tells you which direction it will tilt when it does flip.


The expected-bars-to-change is a geometric distribution mean: 1 / (1 − P(stay)). If a regime has 80% stay probability, it's expected to persist ~5 more bars. If 33%, it's expected to flip in ~1.5 bars.

2. 2nd-order Markov chain regime predictor

Why: the original whale logic was reactive — it fires after confluence forms. Markov is predictive — it learns your instrument's transition habits and uses them to gate and anticipate whale signals.

Pure statistics, no black box:

  • 2nd-order — predicts the next regime from the pair of previous regimes, not just one. Captures patterns like "Chop → Sideways → 68% Bull next" that a 1st-order chain would miss.
  • Laplace smoothing (α=1) — every transition count gets a +1 pseudocount before normalization. Prevents "never observed → 0% forever" failure. Standard in real statistics.
  • Exponential recency decay — newer transitions count more than old ones (default 0.995/bar). Markets drift; stale history shouldn't dominate current prediction.
  • Duration conditioning — separate transition matrices for "current state held <5 bars" vs "held ≥5 bars." Regimes behave differently after they've been running. Real statistical sub-populations.
  • Confidence gating — if the current context has fewer than 10 observations, predictions are flagged low-n. No fabricated probabilities.


Maximum useful substance without gimmick. 3rd-order Markov would need thousands of regime transitions per cell to converge — doesn't happen on typical charts. 2nd-order is the ceiling before diminishing returns.

3. Dual-layer regime classification — Chop-zone 🦈 vs Sideways 🦀

Why: prior versions treated "not trending" as a single category. But there are two fundamentally different kinds of non-trending market:

  • 🦈 Chop-zone — violent range-bound circling. Detected via classic Choppiness Index. Often precedes a sharp breakout.
  • 🦀 Sideways — slow drift, flat angles across close/high/low at both short and long periods. Detected via angle consensus. Often indicates accumulation or distribution.


Showing them separately lets you read which kind of non-trending you're in. Different implications, different decisions.

4. Hatchling whales — pre-signal pre-whales

Why: the Markov chain lets us anticipate confluence before it forms. When the current state is Sideways AND Markov predicts Bull (or Bear) with confidence above the hatchling threshold, a small whale appears at the mid-forecast position — a heads-up that confluence is probabilistically coming.

Full whale (size.huge at forecast edge) = confluence is here now.
Hatchling whale (size.small at forecast mid) = confluence is probably coming soon.

Better entries on regime changes.

5. Markov-gated whale confirmation

Why: sometimes projected-confluence fires, but the instrument's historical pattern says "from this context, the opposite is more likely." That's exactly the setup a trader wants the indicator to filter out.

The gate is permissive — Markov blocks a whale only if it's confident AND its argmax points the opposite direction. Uncertainty or agreement = pass through. Reduces false confluence without over-filtering.

6. Regime-aware 4-column win-rate dashboard

Why: knowing how much time the instrument actually spends in each regime is as actionable as the signals themselves.

Four parallel columns:

  • 🐳 Bull — confluence signals and win rate
  • 🐋 Bear — same, opposite direction
  • 🦈 Chop-zone — CI chop events and % of bars
  • 🦀 Sideways — angle-sideways events and % of bars


If your instrument spends 80% of bars in Chop/Sideways, confluence will be rare — adjust timeframe or instrument. If Markov shows low-n on most bars, the matrix isn't populated yet — wait for more history before trusting predictions.

7. Session-aware for futures

Why: NQ, ES, CL and other overnight-session futures stamp their daily bar at session start, which is the previous calendar evening. Naive `dayofweek(time)` reads NQ's "Friday session" as Thursday. The indicator uses `time_close("D")` — the close of the daily bar, always on the trading date — so regime classification is correct for both cash equities (TSLA, SPY) and overnight futures (NQ, ES). Same indicator, any asset class.

8. Honest evaluation — next-signal MFE or directional close

Why: "close-at-N-bars" is dishonest. Price can move 2×ATR favorably then retrace — close-at-N logs that as a loss. MFE logs it as what it actually was.

Each signal is held pending until the next signal fires. It's a win if either:

  • The close at next-signal bar was directionally favorable vs entry, OR
  • The Maximum Favorable Excursion between the two signals reached the ATR-scaled threshold (default 0.5×ATR at entry bar)


Either qualifies. Transparent. Computed live. Disclaimer embedded in the dashboard footer.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

How to Read the Dashboard

[code]
┌────────────────────────────────────────────────┐
│ Liquidity Surge + Markov · Win Rate │
├────────┬────────┬─────────────┬───────────────┤
│🐳 Bull │🐋 Bear │🦈 Chop-zone │🦀 Sideways │
│42 sigs │38 sigs │7 events │12 events │
│31 wins │24 wins │120 bars │45 bars │
│73.8% │63.2% │23% │8.6% │
├────────────────────────────────────────────────┤
│Markov Forecast Next: 🐳 52% · 🦀 31% · 🐋 17% │
├────────────────────────────────────────────────┤
│Current State: 🐳 Bull held 5b · IMBALANCED │
│ stay 68%, change expected in ~3b · next: 🦀 │
├────────────────────────────────────────────────┤
│⚠ Not financial advice · Learned on chart hist │
└────────────────────────────────────────────────┘
[/code]

Reading order:
  1. Column data — how Bull/Bear signals have performed, how much time is spent in each regime
  2. Markov Forecast Next — next-bar regime probabilities (with sample-size confidence)
  3. Current State — the single-line answer to "where am I and what's next"
  4. Footer — disclaimer + config


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

How to Use

  1. Load with defaults.
  2. Wait for ~100-200 bars of history. The Markov matrix needs observations to learn.
  3. Read Current State first. It tells you what to expect.
  4. If BALANCED → expect a regime change, trade light.
  5. If IMBALANCED + change in ~N bars → plan around that window.
  6. Watch for 🐳 / 🐋 full whales at the forecast edge — confluence + Markov confirmed.
  7. Watch for small hatchling whales at forecast mid-point — Markov's early prediction of confluence coming.
  8. Respect 🦈 (chop-zone) and 🦀 (sideways). Don't fight the regime.
  9. Tune Hatchling Threshold and Win Threshold (×ATR) to your instrument and style.


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Best Paired With Smart Candle Structures [TechnicalZen]

This indicator answers whether and when to trust the flow. Smart Candle Structures answers where to act. Together: right place, right moment, measurable conviction, regime-aware.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Key Settings

  • Time Span — past bars rendered + forecast horizon (default 15)
  • Momentum Source — MFI (default) or RSI
  • Oscillator Type — Hull-VWMA (default) or signed-ADX
  • Slope Lookback — bars for slope that fires whales (default 4)
  • Evaluation Window — bars after a signal to measure MFE (default 5)
  • Win Threshold (× ATR) — minimum favorable excursion as a multiple of ATR (default 0.5)
  • Gate Whale by Choppiness — master toggle for 🦈 / 🦀 filter
  • CI Length / CI Threshold — classic Choppiness Index tuning
  • Angle Short / Long Period — angle-based sideways lookbacks
  • Angle Trend / Sideways Threshold — angle degrees defining trending vs sideways
  • Use Markov Predictor — master toggle for the 2nd-order chain
  • Count Decay per Bar — recency weighting for Markov counts (default 0.995)
  • Hatchling Threshold — minimum Markov probability to fire pre-whale (default 55%)
  • Dashboard Text Size — Tiny / Small / Normal / Large / Huge
  • Camera — yaw / pitch / scales for the 3D view


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Disclaimer

This is a visualization and analytical tool, not financial advice or a signal service. The Markov chain is trained on your chart's history — it describes what has happened on this instrument at this timeframe, not what will happen. Regime transition probabilities are learned estimates; past frequencies do not guarantee future outcomes. Markets are reflexive and can transition in ways the chain has never observed. Hatchlings, whales, sharks and crabs are visualizations of mathematical predictions — they do not constitute buy or sell recommendations. Trade with your own risk management. Every trade can lose.

The indicator echoes this disclaimer in its dashboard footer so you see it every time you read the chart. It's always there because it's always true.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Clear direction from learned regimes.
— TechnicalZen
Catatan Rilis
Improved the dashboard.
Catatan Rilis
Better Markov Chain settings.
Catatan Rilis
Dashboard esthetics
Catatan Rilis
Dashboard v3

Pernyataan Penyangkalan

Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.