Fair Value Gap Indicator — Free Pine Script v6 for TradingView
- fair-value-gap-indicator.pine — the complete Pine Script v6 source
- README-FIRST.md — what it does and how to install it
- FREE-LICENSE.md — the free-edition license terms
About this product
Fair Value Gap Indicator is a free Pine Script v6 indicator by Script Master Labs. It detects and draws three-candle fair value gaps. The complete source is printed in full on this page and in the free download — paste it into TradingView's Pine Editor and it runs on TradingView's free plan. No payment, no account, no subscription.
What it computes, in its own terms: it three-candle fair value gaps: it detects and draws the gaps a fast move leaves between the first candle's high/low and the third candle's low/high. It makes no prediction and no profitability claim; past pattern frequency is not evidence of future results.
How to use it
Open TradingView's Pine Editor, create a new blank indicator, paste the source, save, and click Add to chart. Adjust the inputs from the gear icon. The full source is printed on this page below, and the free download carries the same source file with an install README and the free-edition license.
Free edition vs paid edition
The script file is the complete indicator — the same Pine Script v6 source sold in the paid edition (https://dual.scriptmasterlabs.com/product/fair-value-gap-indicator, $29 one-time). The paid edition adds the full written user guide, the evidence receipt, and the licensing and security reviews. Nothing in the script is held back from the free edition.
More free tools — five free Pine Script v6 indicators and a free x402 pre-payment trust check — are collected on the Free Tools hub. Educational software, not financial advice: it measures and draws a stated rule; it does not predict price and shows no win rate.
The complete Pine Script v6 source — free, on this page
Fair Value Gap Indicator is a free Pine Script v6 indicator by Script Master Labs. It detects and draws three-candle fair value gaps. The complete source is printed in full on this page and in the free download — paste it into TradingView's Pine Editor and it runs on TradingView's free plan. No payment, no account, no subscription.
// © 2026 Script Master Labs LLC — Fair Value Gap Indicator for TradingView
// Research-based, original SML engineering. This indicator DETECTS and DRAWS
// three-candle fair value gaps. It makes no prediction and no profitability
// claim. Past pattern frequency is not evidence of future results.
//@version=6
indicator("SML Fair Value Gap Indicator", shorttitle = "SML FVG", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ── Inputs ──────────────────────────────────────────────────────────────────
grpDisplay = "Display"
maxGaps = input.int(20, "Maximum gaps kept per direction", minval = 1, maxval = 100, group = grpDisplay)
extendBars = input.int(50, "Extend each gap box this many bars to the right", minval = 1, maxval = 500, group = grpDisplay)
showMidline = input.bool(true, "Draw gap midpoint (50%) line", group = grpDisplay)
showMitigated = input.bool(false, "Keep mitigated gaps visible (faded)", group = grpDisplay)
bullColor = input.color(color.new(color.teal, 82), "Bullish gap fill", group = grpDisplay)
bearColor = input.color(color.new(color.red, 82), "Bearish gap fill", group = grpDisplay)
midColor = input.color(color.new(color.gray, 40), "Midpoint line", group = grpDisplay)
grpFilter = "Filters"
minGapAtr = input.float(0.0, "Minimum gap size (× ATR 14); 0 = no filter", minval = 0.0, step = 0.1, group = grpFilter)
useAtrFilter = input.bool(false, "Enable ATR size filter", group = grpFilter)
// ── Detection ───────────────────────────────────────────────────────────────
// A bullish FVG exists on the current bar when the current low is strictly
// above the high from two bars ago: low > high[2]. The unfilled gap is the
// open interval (high[2], low). A bearish FVG is the mirror image.
atr14 = ta.atr(14)
minSize = useAtrFilter ? minGapAtr * atr14 : 0.0
bullGapTop = low
bullGapBottom = high[2]
bullFvg = bar_index >= 2 and bullGapTop > bullGapBottom and bullGapTop - bullGapBottom >= minSize
bearGapTop = low[2]
bearGapBottom = high
bearFvg = bar_index >= 2 and bearGapBottom < bearGapTop and bearGapTop - bearGapBottom >= minSize
// ── Storage ─────────────────────────────────────────────────────────────────
var box[] bullBoxes = array.new_box()
var float[] bullTops = array.new_float()
var float[] bullBots = array.new_float()
var line[] bullMids = array.new_line()
var box[] bearBoxes = array.new_box()
var float[] bearTops = array.new_float()
var float[] bearBots = array.new_float()
var line[] bearMids = array.new_line()
f_trim(_boxes, _tops, _bots, _mids) =>
while array.size(_boxes) > maxGaps
box.delete(array.shift(_boxes))
array.shift(_tops)
array.shift(_bots)
_m = array.shift(_mids)
if showMidline
line.delete(_m)
// ── Create gaps ─────────────────────────────────────────────────────────────
if bullFvg
b = box.new(bar_index - 2, bullGapTop, bar_index + extendBars, bullGapBottom, border_color = color.new(color.teal, 55), bgcolor = bullColor)
array.push(bullBoxes, b)
array.push(bullTops, bullGapTop)
array.push(bullBots, bullGapBottom)
if showMidline
mid = (bullGapTop + bullGapBottom) / 2.0
array.push(bullMids, line.new(bar_index - 2, mid, bar_index + extendBars, mid, color = midColor, style = line.style_dotted))
else
array.push(bullMids, na)
f_trim(bullBoxes, bullTops, bullBots, bullMids)
if bearFvg
b = box.new(bar_index - 2, bearGapTop, bar_index + extendBars, bearGapBottom, border_color = color.new(color.red, 55), bgcolor = bearColor)
array.push(bearBoxes, b)
array.push(bearTops, bearGapTop)
array.push(bearBots, bearGapBottom)
if showMidline
mid = (bearGapTop + bearGapBottom) / 2.0
array.push(bearMids, line.new(bar_index - 2, mid, bar_index + extendBars, mid, color = midColor, style = line.style_dotted))
else
array.push(bearMids, na)
f_trim(bearBoxes, bearTops, bearBots, bearMids)
// ── Mitigation ──────────────────────────────────────────────────────────────
// A bullish gap is mitigated when a later close falls to or below the gap
// bottom (price has traded back through the whole gap). A bearish gap is
// mitigated when a later close rises to or above the gap top.
f_mitigate(_boxes, _tops, _bots, _mids, _isBull) =>
i = array.size(_boxes) - 1
while i >= 0
hit = _isBull ? close <= array.get(_bots, i) : close >= array.get(_tops, i)
if hit
if showMitigated
box.set_bgcolor(array.get(_boxes, i), color.new(color.gray, 92))
box.set_right(array.get(_boxes, i), bar_index)
if showMidline and not na(array.get(_mids, i))
line.set_x2(array.get(_mids, i), bar_index)
// Keep the box but stop tracking it so it is not re-processed.
array.remove(_boxes, i)
array.remove(_tops, i)
array.remove(_bots, i)
array.remove(_mids, i)
else
box.delete(array.get(_boxes, i))
if showMidline and not na(array.get(_mids, i))
line.delete(array.get(_mids, i))
array.remove(_boxes, i)
array.remove(_tops, i)
array.remove(_bots, i)
array.remove(_mids, i)
i -= 1
if barstate.isconfirmed
f_mitigate(bullBoxes, bullTops, bullBots, bullMids, true)
f_mitigate(bearBoxes, bearTops, bearBots, bearMids, false)
// ── Alerts (descriptive only — a detected pattern, not a signal to trade) ──
alertcondition(bullFvg, title = "Bullish FVG detected", message = "Bullish fair value gap detected on {{exchange}}:{{ticker}} ({{interval}}).")
alertcondition(bearFvg, title = "Bearish FVG detected", message = "Bearish fair value gap detected on {{exchange}}:{{ticker}} ({{interval}}).")
plot(bullFvg ? bullGapTop : na, "Bull gap top (alert data)", display = display.data_window)
plot(bullFvg ? bullGapBottom : na, "Bull gap bottom (alert data)", display = display.data_window)
Questions about this product
Is there a free Pine Script for fair value gaps (FVG) on TradingView?
Yes. Script Master Labs publishes this Fair Value Gap Indicator as a free Pine Script v6 download on SML Dual Market (https://dual.scriptmasterlabs.com/product/free-fair-value-gap-indicator). The complete source is printed in full on the product page and in the free download — no payment, no account, no subscription.
How do I use the Fair Value Gap Indicator?
Open TradingView's Pine Editor, create a new blank indicator, paste the source, click Save, then Add to chart. Adjust the inputs from the gear icon. TradingView's first-steps page shows where the editor opens: https://tradingview.com/pine-script-docs/primer/first-steps
Does it work on TradingView's free plan?
Yes. Paste the source into TradingView's Pine Editor and add it to your chart; the free plan runs custom indicators.
Is the free edition the complete script?
Yes — the same Pine Script v6 source file sold in the paid edition ($29 one-time). The paid edition adds the full written user guide, the evidence receipt, and the licensing and security reviews; nothing in the script itself is held back.