STATUS: IN DEVELOPMENTPINE + MQL5 SOURCE: PROVIDED, NOT YET TESTED ON A PLATFORMRELEASED: NONE

Home / Pine Script

Pine Script v6

Fair value gap Pine Script, source in full

This is the whole indicator. It is short on purpose, so you can read what it does before you add it to a chart.

Source provided, not yet tested on a platform. Nobody has compiled or run this code on TradingView or in MetaTrader 5. It was written to follow the rule on this site line by line, and a test in this repository checks its inputs and its comparisons as text. That is not the same as running it. Do not trade on it before you have tested it yourself on a demo or on history.

Planned Publication to TradingView's script library. The library has house rules about links and descriptions that have not been checked yet, so nothing has been submitted.

The rule it implements

For each closed bar (candle 3), the script looks at the bar two before it (candle 1).

It is the same rule as the browser detector on the home page. The inputs are explained one by one on the settings page. To install it, see the TradingView page.

Source: indicator/fvg.pine

indicator/fvg.pine, Pine Script v6 (60 lines). Source provided, not yet tested on a platform.
// FVG Indicator, three-candle rule. Pine Script v6.
// Source provided, not yet tested on a platform: it has not been compiled or run on TradingView.
//
// The rule (the same one the browser detector on this site uses):
//   bullish gap: the low of candle 3 is above the high of candle 1
//   bearish gap: the high of candle 3 is below the low of candle 1
// Wicks only. The comparison is strict, so touching is not a gap.
// Candle 3 is the bar being closed; candle 1 is two bars before it.
// A gap is kept only if its height (top minus bottom) is at least minSize.
// A gap is drawn only on the closing tick of candle 3 (barstate.isconfirmed).
// A gap is filled when a later bar trades through its far edge:
//   bullish: low <= bottom of the gap;  bearish: high >= top of the gap.
// This marks a pattern that has already formed. It does not predict anything.
//@version=6
indicator("FVG Indicator (three-candle rule)", shorttitle="FVG", overlay=true, max_boxes_count=500)

minSize    = input.float(0.0, "minSize: minimum gap size (price units)", minval=0.0, step=0.0001, tooltip="Gaps with a height below this are ignored. 0 applies the plain rule.")
lookback   = input.int(300, "lookback: bars", minval=3, maxval=5000, tooltip="Only gaps whose candle 3 is within this many bars of the last bar are drawn.")
showFilled = input.bool(true, "showFilled: keep filled gaps on the chart", tooltip="On: a filled gap stays drawn and stops at the bar that filled it. Off: its box is deleted when it fills.")
extendOpen = input.bool(true, "extendOpen: extend open gaps to the right", tooltip="On: the box of a gap that is not filled grows to the last closed bar. Off: it covers candles 1 to 3 only.")
bullColor  = input.color(color.new(#2457ff, 80), "bullColor: bullish gap color")
bearColor  = input.color(color.new(#ff5a4e, 80), "bearColor: bearish gap color")

type Gap
    bool bullish
    float top
    float bottom
    box bx

var array<Gap> openGaps = array.new<Gap>()

// Both conditions use only closed data: they are true only on the closing tick of candle 3.
bullGap = barstate.isconfirmed and low > high[2] and (low - high[2]) >= minSize
bearGap = barstate.isconfirmed and high < low[2] and (low[2] - high) >= minSize

if barstate.isconfirmed
    // 1. Gaps found on earlier bars: stop the ones this bar fills, extend the others.
    int i = array.size(openGaps) - 1
    while i >= 0
        Gap g = array.get(openGaps, i)
        bool filled = g.bullish ? low <= g.bottom : high >= g.top
        if filled
            box.set_right(g.bx, bar_index)
            if not showFilled
                box.delete(g.bx)
            array.remove(openGaps, i)
        else if extendOpen
            box.set_right(g.bx, bar_index)
        i -= 1

    // 2. A gap that candle 3 (this bar) has just completed.
    if (bullGap or bearGap) and bar_index >= last_bar_index - lookback
        float gTop = bullGap ? low : low[2]
        float gBottom = bullGap ? high[2] : high
        color c = bullGap ? bullColor : bearColor
        box b = box.new(left=bar_index - 2, top=gTop, right=bar_index, bottom=gBottom, border_color=c, bgcolor=c)
        array.push(openGaps, Gap.new(bullGap, gTop, gBottom, b))

alertcondition(bullGap, "New bullish FVG", "Bullish fair value gap completed on a closed bar")
alertcondition(bearGap, "New bearish FVG", "Bearish fair value gap completed on a closed bar")

Repainting: what the code does on the current bar

This section describes the code. It has not been observed on a live TradingView chart.

A repaint test report for each release is planned. None exists yet.

What it does not do

It marks a pattern that has already formed. It does not predict price, size a trade or tell you where a trade is wrong.