OPEN-SOURCE SCRIPT
Fair Value Gaps - JC

FVG Indicator — How It Works
What It Does
Detects Fair Value Gaps (3-bar price imbalances) on any chart timeframe and draws them as semi-transparent boxes that track their full lifecycle from formation to resolution.
Detection
A FVG forms when a 3-candle pattern leaves a price gap:
- Bullish FVG: Bar 3's low > Bar 1's high — price gapped up, leaving an unfilled zone between bar 1's high (bottom) and bar 3's low (top)
- Bearish FVG: Bar 1's low > Bar 3's high — price gapped down, leaving an unfilled zone between bar 3's high (bottom) and bar 1's low (top)
Bar 2 (the middle candle) is where the box starts drawing from, but it's not used in detection — only bars 1 and 3 matter.
State Machine (7 States)
Each FVG tracks through a lifecycle:
Active states:
- Fresh (blue/orange) — Just formed, price hasn't returned to the zone yet
- Tapped (lighter shade) — Price has entered the zone but hasn't resolved it
- Respected (green/red) — Price tapped the zone then bounced away (closed back outside)
Terminal states:
- Consumed — Fill ratio reached 100% (zone fully filled by wicks)
- Invalidated — Price closed through the opposite side of the zone
- Displaced — Price closed through with a strong candle body (>= gap size × displacement multiplier)
- Timeout — Unresolved after 50 bars (configurable)
Visual Behavior
- Active FVGs extend rightward bar-by-bar until they reach a terminal state
- Terminal FVGs are deleted by default (toggle "Show terminated" to keep them visible in gray)
- Optional midpoint dashed line marks the CE (Consequent Encroachment) level — the 50% retracement of the gap
- Oldest FVGs are removed when the count exceeds the max (default 15)
Key Inputs
┌─────────────────────────┬─────────┬────────────────────────────────────────────┐
│ Setting │ Default │ Purpose │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Max FVGs │ 15 │ How many to show on chart │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Min gap ticks │ 0 │ Filter out small gaps (set to 4 for MNQ) │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Timeout bars │ 50 │ When unresolved FVGs expire │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Consumed threshold │ 1.0 │ Fill ratio to mark as consumed │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Displacement multiplier │ 1.0 │ Body size relative to gap for displacement │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Show midpoint │ ON │ CE level dashed line │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Show terminated │ OFF │ Keep resolved FVGs visible │
└─────────────────────────┴─────────┴────────────────────────────────────────────┘
What It Does
Detects Fair Value Gaps (3-bar price imbalances) on any chart timeframe and draws them as semi-transparent boxes that track their full lifecycle from formation to resolution.
Detection
A FVG forms when a 3-candle pattern leaves a price gap:
- Bullish FVG: Bar 3's low > Bar 1's high — price gapped up, leaving an unfilled zone between bar 1's high (bottom) and bar 3's low (top)
- Bearish FVG: Bar 1's low > Bar 3's high — price gapped down, leaving an unfilled zone between bar 3's high (bottom) and bar 1's low (top)
Bar 2 (the middle candle) is where the box starts drawing from, but it's not used in detection — only bars 1 and 3 matter.
State Machine (7 States)
Each FVG tracks through a lifecycle:
Active states:
- Fresh (blue/orange) — Just formed, price hasn't returned to the zone yet
- Tapped (lighter shade) — Price has entered the zone but hasn't resolved it
- Respected (green/red) — Price tapped the zone then bounced away (closed back outside)
Terminal states:
- Consumed — Fill ratio reached 100% (zone fully filled by wicks)
- Invalidated — Price closed through the opposite side of the zone
- Displaced — Price closed through with a strong candle body (>= gap size × displacement multiplier)
- Timeout — Unresolved after 50 bars (configurable)
Visual Behavior
- Active FVGs extend rightward bar-by-bar until they reach a terminal state
- Terminal FVGs are deleted by default (toggle "Show terminated" to keep them visible in gray)
- Optional midpoint dashed line marks the CE (Consequent Encroachment) level — the 50% retracement of the gap
- Oldest FVGs are removed when the count exceeds the max (default 15)
Key Inputs
┌─────────────────────────┬─────────┬────────────────────────────────────────────┐
│ Setting │ Default │ Purpose │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Max FVGs │ 15 │ How many to show on chart │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Min gap ticks │ 0 │ Filter out small gaps (set to 4 for MNQ) │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Timeout bars │ 50 │ When unresolved FVGs expire │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Consumed threshold │ 1.0 │ Fill ratio to mark as consumed │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Displacement multiplier │ 1.0 │ Body size relative to gap for displacement │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Show midpoint │ ON │ CE level dashed line │
├─────────────────────────┼─────────┼────────────────────────────────────────────┤
│ Show terminated │ OFF │ Keep resolved FVGs visible │
└─────────────────────────┴─────────┴────────────────────────────────────────────┘
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.