OPEN-SOURCE SCRIPT

FVG Maxing - Fair Value Gaps, Equilibrium, and Candle Patterns

203

What this script does
This open-source indicator highlights 3-candle fair value gaps (FVGs) on the active chart timeframe, draws their midpoint ("equilibrium") line, tracks when each gap is mitigated, and optionally marks simple candle patterns (engulfing and doji) for confluence. It is intended as an educational tool to study how price interacts with imbalances.

  • 3-candle bullish and bearish FVG zones drawn as forward-extending boxes.
  • Equilibrium line at 50% of each gap.
  • Different styling for mitigated vs unmitigated gaps.
  • Compact statistics panel showing how many gaps are currently active and filled.
  • Optional overlays for bullish/bearish engulfing patterns and doji candles.


1. FVG logic (3-candle gaps)
The script focuses on a strict 3-candle definition of a fair value gap:

  • Three consecutive candles with the same body direction.
  • The wick of candle 3 is separated from the wick of candle 1 (no overlap).
  • A bullish gap is created when price moves up fast enough to leave a gap between candle 1 and 3. A bearish gap is the mirror case to the downside.


In Pine, the core detection looks like this:

Pine Script®
// Three candles with the same body direction bull_seq = close > open and close[1] > open[1] and close[2] > open[2] bear_seq = close < open and close[1] < open[1] and close[2] < open[2] // Wick gap between candle 1 and candle 3 bull_gap = bull_seq and low > high[2] bear_gap = bear_seq and high < low[2] // Final FVG flags is_bull_fvg = bull_gap is_bear_fvg = bear_gap


For each detected FVG:

  • Bullish FVG range: from high[2] up to low (gap below current price).
  • Bearish FVG range: from low[2] down to high (gap above current price).
  • Each zone is stored in a custom FVGData structure so it can be updated when price later trades back inside it.


2. Equilibrium line (0.5 of the gap)
Every FVG box gets an optional equilibrium line plotted at the midpoint between its top and bottom:

Pine Script®
eq_level = (top + bottom) / 2.0 right_index = extend_boxes ? bar_index + extend_length_bars : bar_index bx = box.new(bar_index - 2, top, right_index, bottom) eq_ln = line.new(bar_index - 2, eq_level, right_index, eq_level) line.set_style(eq_ln, line.style_dashed) line.set_color(eq_ln, eq_color)


You can use this line as a neutral “fair value” reference inside the zone, or as a simple way to think in terms of premium/discount within each gap.

3. Mitigation rules and styling
Each FVG stays active until price trades back into the gap:

  • Bullish FVG is considered mitigated when the low touches or moves below the top of the gap.
  • Bearish FVG is considered mitigated when the high touches or moves above the bottom of the gap.


When that happens, the script:

  • Marks the internal FVGData entry as mitigated.
  • Softens the box fill and border colors.
  • Optionally updates the label text from "BULL EQ / BEAR EQ" to "BULL FILLED / BEAR FILLED".
  • Can hide mitigated zones almost completely if you only want to see unfilled imbalances.


This allows you to distinguish between current areas of interest and zones that have already been traded through.

4. Candle pattern overlays (engulfing and doji)
For additional confluence, the script can mark simple candle patterns on top of the FVG view:

  • Bullish engulfing — current candle body fully wraps the previous bearish body and is larger in size.
  • Bearish engulfing — current candle body fully wraps the previous bullish body and is larger in size.
  • Doji — candles where the real body is small relative to the full range (high–low).


The detection is based on basic body and range geometry:

Pine Script®
curr_body = math.abs(close - open) prev_body = math.abs(close[1] - open[1]) curr_range = high - low body_ratio = curr_range > 0 ? curr_body / curr_range : 1.0 bull_engulfing = close > open and close[1] < open[1] and open <= close[1] and close >= open[1] and curr_body > prev_body bear_engulfing = close < open and close[1] > open[1] and open >= close[1] and close <= open[1] and curr_body > prev_body is_doji = curr_range > 0 and body_ratio <= doji_body_ratio


On the chart, they appear as:

  • Small triangle markers below bullish engulfing candles.
  • Small triangle markers above bearish engulfing candles.
  • Small circles above doji candles.


All three overlays are optional and can be turned on or off and recolored in the CANDLE PATTERNS group of inputs.

5. Inputs overview
The script organizes settings into clear groups:

  • DISPLAY SETTINGS: Show bullish/bearish FVGs, show/hide mitigated zones, box extension length, box border width, and maximum number of boxes.
  • EQUILIBRIUM: Toggle equilibrium lines, color, and line width.
  • LABELS: Enable labels, choose whether to label unmitigated and/or mitigated zones, and select label size.
  • BULLISH COLORS / BEARISH COLORS: Separate fill and border colors for bullish and bearish gaps.
  • MITIGATED STYLE: Opacity used when a gap is marked as mitigated.
  • STATISTICS: Toggle the on-chart FVG statistics panel.
  • CANDLE PATTERNS: Show engulfing patterns, show dojis, colors, and the body-to-range threshold that defines a doji.


6. Statistics panel
An optional table in the corner of the chart summarizes the current state of all tracked gaps:

  • Total number of FVGs still being tracked.
  • Number of bullish vs bearish FVGs.
  • Number of unfilled vs mitigated FVGs.
  • Simple fill rate: percentage of tracked FVGs that have been marked as mitigated.


This can help you study how a particular market tends to treat gaps over time.

7. How you might use it (examples)
These are usage ideas only, not recommendations:

  • Study how often your symbol mitigates gaps and where inside the zone price tends to react.
  • Use higher-timeframe context and then refine entries near the equilibrium line on your trading timeframe.
  • Combine FVG zones with basic candle patterns (engulfing/doji) as an extra visual anchor, if that fits your process.


Hope you enjoy, give your feedback in the comments!

- officialjackofalltrades

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.