Indicator · Pine Script v5 · On the chart

GARY Fair-Value GAPS

by g-moe· code credited to goodgains

This indicator identifies and plots bullish and bearish Fair Value Gaps (FVGs) directly on the price chart using box drawings.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code151
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

151 lines of Pine Script v5 from Tradingview/gary-fair-value-gaps.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

gary-fair-value-gaps.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("GARY Fair-Value GAPS", overlay = true, max_labels_count = 500, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 1000)

// @keywords: fair-value-gaps, fvg, bullish-fvg, bearish-fvg, gap-boxes, imbalance-zones, body-sensitivity, gap-merge, gap-fill, filled-fvg, zone-management, bar-coloring, supply-demand-gap, gap-detection, box-array, gap-tracking, price-imbalance, gap-outline, gap-minimize, gap-delete

//--FAIR VALUE GAP INPUTS--//
show_fvg = input(true, 'Fair Value Gaps', group = 'Fair-Value Gaps', inline = '1')
fvg_sensitivity = input.int(150, title = 'FVG Sensitivity', group = 'Fair-Value Gaps', inline = '2', minval = 100, maxval = 500, step = 25)

bull_fvg_css = input.color(color.new(#000000, 70), 'Bull FVG BG', group = 'Fair-Value Gaps', inline = '3')
bear_fvg_css = input.color(color.new(#000000, 70), 'Bear FVG BG', group = 'Fair-Value Gaps', inline = '3')

bull_fvgb_css = input.color(color.new(#41ff00, 20), 'Bull FVG Outline', group = 'Fair-Value Gaps', inline = '4')
bear_fvgb_css = input.color(color.new(#ff0000, 20), 'Bear FVG Outline', group = 'Fair-Value Gaps', inline = '4')

fvg_minimize_onfill = input.bool(true, title = 'Adjust FVG Box As It Fills', group = 'Fair-Value Gaps', inline = '5')
fvg_delete_filled = input.bool(false, title = 'Delete Filled FVGs', group = 'Fair-Value Gaps', inline = '6')
//--FAIR VALUE GAP INPUTS--//


//-----FUNCTIONS-----//
//--FUNCTION TO ADD NEW AND REMOVE LAST IN ARRAY--//
f_array_add_pop(array, new_value_to_add) =>
    array.unshift(array, new_value_to_add)
    array.pop(array)
//--FUNCTION TO ADD NEW AND REMOVE LAST IN ARRAY--//

//--FUNCTION TO DRAW NEW FVGS--//
f_new_fvg(box_array, box_type) =>
    box_left = bar_index - 1
    box_right = bar_index + 3

    box_top = box_type == 1 ? low : math.max( (high[1] + low[1])/2, low[2] ) 
    box_bottom = box_type == 1 ? math.min( (high[1] + low[1])/2, high[2] ) : high
    
    if box_type == 1
        box.delete( array.get(box_array, array.size(box_array) - 1) )
        f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = bull_fvgb_css,
          bgcolor = bull_fvg_css, extend = extend.none, text = 'FVG', text_halign = text.align_center, text_valign = text.align_bottom, 
          text_color = color.new(bull_fvgb_css,20), text_size = size.small, xloc = xloc.bar_index, border_style = line.style_solid, border_width  = 1))
    if box_type == -1
        f_array_add_pop(box_array, box.new( left = box_left, top = box_top, right = box_right, bottom = box_bottom, border_color = bear_fvgb_css,
          bgcolor = bear_fvg_css, extend = extend.none, text = 'FVG', text_halign = text.align_center, text_valign = text.align_top,
          text_color = color.new(bear_fvgb_css,20), text_size = size.small, xloc = xloc.bar_index, border_style = line.style_solid, border_width  = 1))
//--FUNCTION TO DRAW NEW FVGS--//

//--FUNCTION TO COMBINE FVGS--//
f_merge_fvg(box_array, box_type) =>
    if box_type == 1
        box.set_top(array.get(box_array,0), low)
    if box_type == -1
        box.set_bottom(array.get(box_array,0), high)
//--FUNCTION TO COMBINE FVGS--//


//--FUNCTION TO MANAGE FVGS--//
f_manage_fvg(box_array, finished_box_array, box_type) =>
    // loop to move broken fvgs to finished and extend unbroken ones
    for i = 0 to array.size(box_array) - 1
        box_top = box.get_top(array.get(box_array,i))
        box_bottom = box.get_bottom(array.get(box_array,i))

        if box_type == 1
            if low < box_bottom
                if not fvg_delete_filled
                    copied_box = box.copy(array.get(box_array,i))
                    f_array_add_pop(finished_box_array, copied_box)

                    box.set_extend(array.get(finished_box_array,0), extend.none)
                    box.set_right(array.get(finished_box_array,0),bar_index)
                    box.set_text(array.get(finished_box_array,0), 'FVG')
                    box.set_text_color(array.get(finished_box_array,0), color.new(bull_fvgb_css, 40))
                    box.set_border_color(array.get(finished_box_array,0), color.new(bull_fvgb_css, 40))
                    box.set_border_width(array.get(finished_box_array,0),0)
                    box.set_text_size(array.get(finished_box_array,0), size.small)
                box.delete( array.get(box_array, i) )
            else if fvg_minimize_onfill and low < box_top
                box.set_top( array.get(box_array,i), low)
            else
                box.set_right(array.get(box_array,i),bar_index)

        if box_type == -1
            if high > box_top
                if not fvg_delete_filled
                    copied_box = box.copy(array.get(box_array,i))
                    f_array_add_pop(finished_box_array, copied_box)

                    box.set_extend(array.get(finished_box_array,0), extend.none)
                    box.set_right(array.get(finished_box_array,0),bar_index)
                    box.set_text(array.get(finished_box_array,0), 'FVG')
                    box.set_text_color(array.get(finished_box_array,0), color.new(bear_fvgb_css, 40))
                    box.set_border_color(array.get(finished_box_array,0), color.new(bear_fvgb_css, 40))
                    box.set_border_width(array.get(finished_box_array,0),0)
                    box.set_text_size(array.get(finished_box_array,0), size.small)
                box.delete( array.get(box_array, i) )
            else if fvg_minimize_onfill and high > box_bottom
                box.set_bottom( array.get(box_array,i), high)
            else
                box.set_right(array.get(box_array,i),bar_index)
//--FUNCTION TO MANAGE FVGS--//

//-----FUNCTIONS-----//





// fvg logic
abody = math.round_to_mintick(ta.sma(math.abs(close-open), 20))
abody_pct_value = math.round(math.abs(close-open) / abody * 100,0)
abody_bull = abody_pct_value > fvg_sensitivity and close > open ? 1 : 0 
abody_bear = abody_pct_value > fvg_sensitivity and close < open ? 1 : 0 

var bool fvg_bull = false 
var bool fvg_bear = false 

fvg_bull := abody_bull[1] and low > (high[1] + low[1]) / 2 and low > high[2]
fvg_bear := abody_bear[1] and high < (high[1] + low[1]) / 2 and high < low[2]
barcolor(fvg_bull ? color.green : fvg_bear ? color.red : na, offset = -1)


//--ARRAYS FOR FVGS--//
var fvg_bull_box = array.new_box(20, na)
var fvg_bear_box = array.new_box(20, na)

var fvg_finishedbull_box = array.new_box(100, na)
var fvg_finishedbear_box = array.new_box(100, na)
//--ARRAYS FOR FVGS--//

if show_fvg
    if fvg_bull 
        if not fvg_bull[1]
            f_new_fvg(fvg_bull_box, 1)
        else
            f_merge_fvg(fvg_bull_box, 1)

    if fvg_bear
        if not fvg_bear[1]
            f_new_fvg(fvg_bear_box, -1)
        else
            f_merge_fvg(fvg_bear_box, -1)

f_manage_fvg(fvg_bull_box, fvg_finishedbull_box, 1) 
f_manage_fvg(fvg_bear_box, fvg_finishedbear_box, -1) 


// fvg logic

Overview

GARY Fair-Value GAPS detects three-bar price imbalances, adjusting sensitivity via user settings. The script can dynamically minimize box boundaries as price intrudes into the imbalance and optionally remove boxes once they are completely filled. Users can customize background and border colors for both bullish and bearish zones, toggle gap visibility, and manage how historical gaps are tracked.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Fair Value Gapsbooltrueshow_fvg
FVG Sensitivityint150fvg_sensitivity
Bull FVG BGcolorcolor.new(#000000, 70)bull_fvg_css
Bear FVG BGcolorcolor.new(#000000, 70)bear_fvg_css
Bull FVG Outlinecolorcolor.new(#41ff00, 20)bull_fvgb_css
Bear FVG Outlinecolorcolor.new(#ff0000, 20)bear_fvgb_css
Adjust FVG Box As It Fillsbooltruefvg_minimize_onfill
Delete Filled FVGsboolfalsefvg_delete_filled
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows

DE-tracker

by Allen1212
303 stars

This indicator plots labels on the TradingView price chart to mark ARK Invest's buy and sell trades for the DE stock. Buy transactions appear in green with X1 or X2 multipliers showing the number of ARK ETFs involved on the same day; sells appear in red. The m

Indicator v4 Support & resistanceSMC / ICT / price action
Updated 2 years ago · None stated GitHub repository of DE-tracker

This Pine Script indicator plots a ZigZag on the price chart. It identifies reversal points when price moves more than a set percentage from the last pivot. The script calculates forecasts using zigzag data and labels detected geometric patterns including harm

Indicator v4 TrendSupport & resistance

Golden Bullet Strategy

by TradersPost
228 stars

The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh

Indicator v6 SMC / ICT / price actionSessions & time
Updated 3 months ago · None stated GitHub repository of Golden Bullet Strategy

Open Source Fractal

by cantolab
183 stars

This indicator implements the Fractal Model concept to plot market structure shifts, liquidity sweeps, and Change in State of Delivery (CISD) levels. It overlays custom higher-timeframe candle previews, open prices, and time markers directly on the price chart

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MIT GitHub repository of Open Source Fractal

Swing Failure Signals

by Ganador1
154 stars

This indicator calculates pivot highs and pivot lows to detect liquidity sweeps and Swing Failure Patterns. It identifies when price breaks past recent key swing highs or lows within a configurable historical window, then looks for a quick reversal or change i

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · Apache-2.0 GitHub repository of Swing Failure Signals

More SMC / ICT scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers