Open-Source Pine Script v5 Guide

Fair Value Gap (FVG) Indicator

Institutional execution rules, audit-clean Pine Script v5 source code, and automated multi-ticker scanning.

What Is Fair Value Gap (FVG) Indicator?

A Fair Value Gap is a 3-candle price imbalance: when a candle moves so aggressively that its neighbors' wicks never overlap, it leaves a "gap" where almost no two-way trade occurred. A bullish FVG forms when the current candle's low is above the high from two candles back; a bearish FVG forms when the current high is below the low from two candles back. Smart Money Concepts traders treat these zones as magnets - price frequently returns to "rebalance" the inefficiency before continuing. When price trades back through a gap, the FVG is said to be mitigated (filled) and loses its significance.

Execution Rules & Setup Playbook

Institutional Playbook: Trade FVGs with the trend, not against it. In an uptrend, wait for price to pull back into an unmitigated bullish FVG and look for a rejection (long lower wick, bullish engulfing) inside the zone - that is your entry, with a stop below the gap. Fresh gaps on high volume matter most; a gap that has already been partially filled is weaker. Avoid trading every gap: on low timeframes FVGs appear constantly, so filter to the 1-hour chart and above, and prioritize gaps that line up with an order block or key level.

Pine Script v5 Source Code

TradingView Pine Editor → Paste → Add to Chart
PINE SCRIPT V5 · UNLOCKED SOURCE
//@version=5
indicator("Fair Value Gaps (FVG)", overlay=true, max_boxes_count=200)

// Inputs
showBull = input.bool(true, "Show Bullish FVGs")
showBear = input.bool(true, "Show Bearish FVGs")
extendBars = input.int(30, "Extend Boxes (bars)", minval=5)

var bullBoxes = array.new_box()
var bearBoxes = array.new_box()

// 3-candle imbalance
bullFVG = low > high[2]
bearFVG = high < low[2]

if bullFVG and showBull
    array.push(bullBoxes, box.new(bar_index - 2, low, bar_index + extendBars, high[2],
         border_color=color.new(color.green, 60), bgcolor=color.new(color.green, 85)))

if bearFVG and showBear
    array.push(bearBoxes, box.new(bar_index - 2, low[2], bar_index + extendBars, high,
         border_color=color.new(color.red, 60), bgcolor=color.new(color.red, 85)))

// Mitigation: delete a bullish FVG once a close fills the gap (trades below its bottom)
if array.size(bullBoxes) > 0
    for i = array.size(bullBoxes) - 1 to 0
        b = array.get(bullBoxes, i)
        if close < box.get_bottom(b)
            box.delete(b)
            array.remove(bullBoxes, i)

// Mitigation: delete a bearish FVG once a close trades above its top
if array.size(bearBoxes) > 0
    for i = array.size(bearBoxes) - 1 to 0
        b = array.get(bearBoxes, i)
        if close > box.get_top(b)
            box.delete(b)
            array.remove(bearBoxes, i)

Related Pine Script v5 Guides

Scan Without Coding

Skip single-chart limits — Tradestie monitors this setup across 5,000+ US stocks automatically.

Server-Side Advantage Our Smart Money Flow dashboard tracks where institutions are actually positioning - real open-interest changes and dark-pool-style accumulation signals - so you can confirm whether the "smart money" is really behind a gap before trading it.
  • Real-time multi-ticker signal scanning
  • Institutional options & GEX confirmation
  • Automated email alerts on triggers
Launch Live Scanner
Check Any Stock Right Now