Library · Pine Script v5 · Library

FVGDetectorLibrary

by alvinng214· code credited to TFlab

This Pine Script library detects and plots Smart Money Concept Fair Value Gaps (FVGs) for both demand and supply zones directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code125
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago5 Aug 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 library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

125 lines of Pine Script v5 from ICT Balanced Price Range [TradingFinder] BPR FVG + IFVG/TFlabFVGDetectorLibrary1.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

TFlabFVGDetectorLibrary1.txt
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TFlab

//@version=5

// @description TODO: add library description here
library("FVGDetectorLibrary" , overlay = true)

//FVG Detector Function
export FVGDetector(string FVGFilter, string FVGFilterType, bool ShowDeFVG, bool ShowSuFVG)=>
    var float DDFVG = 0.0 //Demand Distal Fair Value Gap
    var float DPFVG = 0.0 //Demand Proximal Fair Value Gap
    var int BarDFVG = 0   //Bar Index Demand Fair Value Gap
    
    var float SDFVG = 0.0 //Supply Distal Fair Value Gap
    var float SPFVG = 0.0 //Supply Proximal Fair Value Gap
    var int BarSFVG = 0   //Bar Index Supply Fair Value Gap

    var line DDistal = na
    var line DProximal = na
    var linefill Dfillline = na

    var line SDistal = na
    var line SProximal = na
    var linefill Sfillline = na

    var label DFVGLabel = na
    var label SFVGLabel = na

    var bool DMitigated = true
    var bool SMitigated = true

    var bool DConditionFVG = false
    var bool SConditionFVG = false
    //ATR
    ATR = ta.atr(55)
    //Demand FVG Filter Method {Choose and switch}
    DConditionFVG := if FVGFilter == 'Off' 
        if (low > high[2] )
            true
        else
            false

    else if FVGFilter == 'On'
        if FVGFilterType =='Very Aggressive' and  (low > high[2] ) and (high > high[1] ) 
            true
        else if FVGFilterType =='Aggressive' and ((low > high[2] )and ((high[1] - low[1]) >= (1.0 * ATR)) and (high > high[1] )) 
            true
        else if FVGFilterType == 'Defensive' and ((low > high[2] )and ((high[1] - low[1]) >= (1.5 * ATR)) and (high > high[1] )) 
         and ((close[2] - open[2] > 0 and close[1] - open[1] > 0) or math.abs((close[1] - open[1])/(high[1] - low[1])) > 0.7) 
            true         
        else if FVGFilterType =='Very Defensive' and ((low > high[2] )and ((high[1] - low[1]) >= (1.5 * ATR)) and (high > high[1] )) 
         and ((close[2] - open[2] > 0 and close[1] - open[1] > 0) and math.abs((close[1] - open[1])/(high[1] - low[1])) > 0.7)
         and math.abs((close[2] - open[2])/(high[2] - low[2])) > 0.35 and math.abs((close - open)/(high - low)) > 0.35
            true
        else
            false

    //Supply FVG Filter Method {Choose and switch}
    SConditionFVG := if FVGFilter == 'Off' 
        if (high < low[2] )
            true
        else
            false
    else if FVGFilter == 'On'
        if FVGFilterType == 'Very Aggressive' and  (high < low[2] ) and (low[1] > low )  
            true
        else if FVGFilterType == 'Aggressive' and ((high < low[2] )and ((high[1] - low[1]) >= (1.0 * ATR)) and (low[1] > low ))  
            true
        else if FVGFilterType == 'Defensive' and ((high < low[2] )and ((high[1] - low[1]) >= (1.5 * ATR)) and (low[1] > low ))  
         and ((close[2] - open[2] < 0 and close[1] - open[1] < 0) or math.abs((close[1] - open[1])/(high[1] - low[1])) > 0.7) 
            true
        else if FVGFilterType == 'Very Defensive' and ((high < low[2] )and ((high[1] - low[1]) >= (1.5 * ATR)) and (low[1] > low )) 
         and ((close[2] - open[2] < 0 and close[1] - open[1] < 0) and math.abs((close[1] - open[1])/(high[1] - low[1])) > 0.7)
         and math.abs((close[2] - open[2])/(high[2] - low[2])) > 0.35 and math.abs((close - open)/(high - low)) > 0.35
            true
        else
            false
    //Supply and Demand Fair Value Gap Detector
    //Demand FVG Detector
    if DConditionFVG
        DDFVG  := high[2]
        DPFVG := low
        BarDFVG := bar_index

    //Supply FVG Detector
    if SConditionFVG
        SDFVG := low[2]
        SPFVG := high
        BarSFVG:= bar_index


    if DConditionFVG and ShowDeFVG
        DDistal := line.new(BarDFVG, DDFVG, BarDFVG   , DDFVG , color = color.rgb(0, 0, 0) , style = line.style_dotted , width = 1)
        DProximal := line.new(BarDFVG, DPFVG, BarDFVG   , DPFVG , color = color.rgb(0, 0, 0) , style = line.style_dotted , width = 1)
        Dfillline := linefill.new(DDistal ,DProximal ,color = #a9d60580 )
        DFVGLabel := label.new(BarDFVG,DDFVG , text = 'FVG', style = label.style_label_up , 
         color = color.rgb(255, 255, 255, 100) , textcolor = #000000 , 
         textalign = text.align_center , size = size.small )  
    if ((low > DDFVG ) or DDFVG == DDFVG[1]) and DMitigated == true
        line.set_x2(DDistal, bar_index + 1 )
        line.set_x2(DProximal,  bar_index + 1)
        label.set_x(DFVGLabel , math.round((BarDFVG + bar_index) /2)  )
    if ((low[1] >= DPFVG ) and (low <= DPFVG ))
        DMitigated := false
    if DConditionFVG
        DMitigated := true
    //Supply FVG Detector
    if SConditionFVG and ShowSuFVG

        SDistal := line.new(BarSFVG, SDFVG, BarSFVG + 10  , SDFVG , color = color.rgb(0, 0, 0) , style = line.style_dotted , width = 1)
        SProximal := line.new(BarSFVG, SPFVG, BarSFVG + 10  , SPFVG , color = color.rgb(0, 0, 0) , style = line.style_dotted , width = 1)
        Sfillline := linefill.new(SDistal ,SProximal ,color = #ff7d044d )
        SFVGLabel := label.new(BarSFVG,SDFVG , text = 'FVG', style = label.style_label_down , 
         color = color.rgb(255, 255, 255, 100) , textcolor = #000000 , 
         textalign = text.align_center , size = size.small )
    if ((high < SDFVG) or SDFVG == SDFVG[1]) and SMitigated == true
        line.set_x2(SDistal, bar_index + 1 )
        line.set_x2(SProximal,  bar_index + 1)
        label.set_x(SFVGLabel , math.round((BarSFVG + bar_index) /2)  )
    if ((high >= SPFVG) and (high[1] <= SPFVG))
        SMitigated := false
    if SConditionFVG
        SMitigated := true
    [DConditionFVG, DDFVG, DPFVG, BarDFVG, SConditionFVG, SDFVG, SPFVG, BarSFVG]

Overview

FVGDetectorLibrary exports a detection function that accepts configurable filter thresholds ranging from standard three-bar imbalances to ATR- and candle-body-filtered setups (aggressive, defensive, and very defensive). When active, it manages visual distal and proximal boundary lines, zone fills, and mitigation tracking.

Details

Published by

alvinng214 on GitHub

Code credit

TFlab, as written in the script's header

Draws

Library (no plots)

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.

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

SMC Engine Private

by skipp-dev
1 stars

This Pine Script v6 library serves as an internal engine for Smart Money Concepts (SMC), providing types, tracking, and rendering logic for order blocks, fair value gaps, and market structure. It calculates order block parameters including quality scores, heig

Library v6 SMC / ICT / price actionSupport & resistance
Updated 4 days ago · None stated GitHub repository of SMC Engine Private

SFA FlowLiquidity v1

by genesisgrubio39
1 stars

The SFA FlowLiquidity library computes structural flow and liquidity events from price data. It detects sweep ups and downs with associated scores, identifies liquidity levels, and applies session and regime filters. Persistent arrays track liquidity prices, b

Library v6 SMC / ICT / price actionSupport & resistance
Updated 10 months ago · None stated GitHub repository of SFA FlowLiquidity v1

AuthLib

by alboogycOdR
6 stars

AuthLib is a Pine Script utility library providing access control and state management structures for technical indicators. It checks callers against a hardcoded array of authorized TradingView usernames before executing operations. The library exports user-de

Library v5 SMC / ICT / price actionSessions & time
Updated 7 months ago · None stated GitHub repository of AuthLib

PineDraw

by 345-strategies
5 stars

PineDraw is a Pine Script v6 library that provides object primitives for drawing and updating price levels on the chart. It enables indicators and strategies to maintain lines, boxes, and labels in place by updating existing objects instead of deleting and rec

Library v6 Support & resistance
Updated 4 days ago · MPL-2.0 GitHub repository of PineDraw

ZigZag

by rubystar0512
5 stars

This library calculates ZigZag pivots and draws connecting lines on the price chart. It changes direction when price moves beyond a percentage deviation after a set pivot depth, then stores each pivot with optional labels for reversal price, cumulative volume,

Library v5 Support & resistance
Updated 2 years ago · None stated GitHub repository of ZigZag

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

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