OPEN-SOURCE SCRIPT

Liquidity Gravity SMC Engine [Jayadev Rana]

809
LIQUIDITY GRAVITY SMC ENGINE

WHAT IT IS

An experimental Smart Money Concepts toolkit that does not treat every order block and fair value gap as equal. Each unmitigated zone is modeled as a gravitational mass acting on price: heavy zones (born from strong displacement on high relative volume) matter more, old zones fade away, and the combined pull of all active zones is compressed into a single bounded bias reading. On top of that field sit a composite trend engine, a quality-scored buy/sell signal engine, and a pyramid re-add engine that only accepts fresh continuation zones.

THE GRAVITY MODEL (CORE ORIGINAL IDEA)

Every zone is minted with a mass at birth:

mass = displacement impulse (in ATR units) x (0.5 + volume percentile / 100)

The mass decays exponentially with a configurable half-life, so a zone that sits unmitigated for a long time gradually loses influence:

effective mass = mass x 0.5 ^ (age in bars / half-life)

Each bar, the script sums the pull of every active bullish zone and every active bearish zone on the current close using a softened inverse-square law (the softening epsilon prevents the value from exploding when price trades inside a zone):

pull = effective mass / (distance in ATR ^ 2 + epsilon ^ 2)

The two fields collapse into a bounded oscillator:

gravity bias = 100 x (bull field - bear field) / (bull field + bear field)

A reading near +100 means the active demand field dominates; near -100 means overhead supply dominates. The raw bull/bear field values, zone counts, and the bias are all exposed in the data window and on the dashboard.

ZONE DETECTION

- Order blocks: a break of a confirmed swing pivot with a displacement candle (body larger than a configurable ATR multiple) anoints the most recent opposite-color candle as the origin. Body-only or full-range zones are selectable.
- Fair value gaps: classic three-candle imbalance, filtered by a minimum gap height in ATR units and an optional minimum displacement body on the middle candle.
- Lifecycle: zones are marked on first touch, removed on a decisive close through the far side, expired after a maximum age, and capped per side (oldest dropped first).

TREND ENGINE

A composite score in -100..+100 blends three independent measurements with user weights:
1. Market structure regime from a BOS / CHoCH state machine on confirmed swing pivots.
2. Signed Kaufman efficiency ratio (how directional the recent path is versus noise).
3. ATR-normalized Hull MA slope, clamped to -1..+1.
The score passes through a hysteresis gate with separate entry and exit thresholds, so the published trend state (LONG / SHORT / NEUTRAL) does not flicker around zero.

SIGNALS AND THE RE-ADD ENGINE

A primary BUY prints when price trades into an active bullish zone and closes back above its top (a sweep and reject), while the trend state and, optionally, the gravity bias agree. Every candidate is scored with a 0-100 confluence quality:

quality = 100 x (0.35 x trend alignment + 0.25 x gravity alignment + 0.20 x zone mass rank + 0.20 x rejection strength)

Only candidates above the quality threshold print. After a primary entry, the re-add engine arms: an ADD signal requires a FRESH zone (born after the previous entry bar), the same trend direction, and a reduced quality threshold, with a hard cap on adds per leg. Legs terminate on an opposing trend flip or an opposing CHoCH, which prints an EXIT marker. SELL logic mirrors the buy side.

INPUT GROUPS

1. Market Structure - pivot length, BOS/CHoCH lines, swing labels.
2. Order Blocks - lookback, body-only toggle, displacement filter.
3. Fair Value Gaps - minimum gap size, middle-candle displacement filter.
4. Gravity Model - mass half-life, softening epsilon, volume percentile lookback, zone expiry, zone cap.
5. Trend Engine - efficiency ratio length, Hull MA length and slope lookback, component weights, entry/exit thresholds.
6. Signals and Re-Add - primary and re-add quality thresholds, max adds per leg, cooldown, gravity alignment toggle, bar-close confirmation.
7. Visuals - zone boxes, signal labels, trend ribbon, gravity gradient bar coloring, colors.
8. Dashboard - position and text size.

ALERTS

Ten alert conditions: primary BUY, primary SELL, re-add long, re-add short, trend flip long, trend flip short, bullish zone created, bearish zone created, bullish zone touched, bearish zone touched.

INTENDED MARKETS AND TIMEFRAMES

Any symbol with volume data (crypto, futures, liquid stocks, major forex pairs). The volume percentile term falls back gracefully where volume is absent, but zone masses become less differentiated. Designed for intraday through daily timeframes; defaults were chosen on 5-minute to 1-hour charts.

NON-REPAINTING BEHAVIOR

Swing pivots confirm after the pivot length (standard pivot lag). BOS/CHoCH events are evaluated on closes against already-confirmed pivots. With "confirm on bar close" enabled (default), signals and alerts fire only on confirmed bars. No security() calls and no lookahead are used.

LIMITATIONS

- The gravity model is a heuristic, not a physical law; the inverse-square form and the half-life are modeling choices you can tune.
- Zone quality depends on the displacement and volume filters; thin or gappy instruments produce fewer, noisier zones.
- Signals are informational markers, not orders; the leg/adds bookkeeping is a visual state machine, not broker position tracking.

This is an educational tool, not financial advice. Test any settings on your own market and timeframe before relying on them.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.