We use cookies to understand how the site is used and improve it. We only load analytics after you accept. Privacy Policy
Tracks fair value gaps and reports how often each size and direction was filled within a fixed bar horizon.
Published by AlgoNexta, who builds and documents these tools so you can inspect the logic before using it. About AlgoNexta · Trading Scripts · AlgoNexta Research
FVG Fill Stats is a TradingView overlay that finds three-bar fair value gaps, follows subsequent price movement through each gap, and summarizes fills by direction and ATR-relative size. It is a research indicator: its boxes and alerts are observations, not orders.
01 · When a gap is detected
After the bar closes, the script compares its low with the high two bars earlier for a bullish gap, or its high with the low two bars earlier for a bearish gap. A gap must meet the default 0.15 × ATR(14) minimum before a colored box is created; unavailable or nonpositive ATR prevents detection.
02 · How a fill is recognized
On later confirmed bars, the default Wick setting tests a bullish gap with the low and a bearish gap with the high. Reaching the near edge marks a touch, reaching the midpoint marks half, and reaching the far edge marks full; Close can be selected instead.
03 · When statistics appear
At the default 50-bar horizon, each gap is counted once in its bullish or bearish small, medium, or large bucket using outcomes reached by then. The table and open-box text display fill statistics, with percentages withheld until the default 20-gap sample minimum; these displays are not trade or risk projections.
This script's logic could not be confidently reconstructed into an automated reference backtest, so no performance figures are shown rather than estimated ones.
Verify it in TradingView's Strategy Tester (or the equivalent tool for tradingview) before relying on it.
Vision Grade requires a reference backtest with enough reconstructed entries to measure — not available for this script yet.
Published by AlgoNexta, an evidence-first platform for building, reviewing, and documenting algorithmic trading tools. This page explains the script's actual logic and intended use; it does not promise profitability or replace verification in TradingView.
FVG Fill Stats is a TradingView overlay that finds three-bar fair value gaps, follows subsequent price movement through each gap, and summarizes fills by direction and ATR-relative size. It is a research indicator: its boxes and alerts are observations, not orders.
fvg-fill-stats-algonexta.pine · 222 lines · 12.3 KB
Not yet verified on the native platform — test it before relying on it.
The original uploaded source.
// ============================================================================
// © algonexta
// This script is for education and research; it does not guarantee results.
// Verify this source in TradingView before use.
// ============================================================================
// FVG Fill Stats. Research indicator, not a strategy: it tracks every fair value gap on the chart,
// records how often and how fast gaps of each size were filled, and shows that record on the open
// gaps. It does not place, manage or simulate orders.
//@version=6
indicator("FVG Fill Stats [AlgoNexta]", shorttitle = "FVG Stats", overlay = true, max_boxes_count = 500, max_labels_count = 100, max_lines_count = 100)
//──────────────────────────────────────────────────────────────────────────────
// Inputs
//──────────────────────────────────────────────────────────────────────────────
string G_GAP = "1. Gap detection"
string G_STA = "2. Statistics"
string G_VIS = "3. Display"
float minSizeAtr = input.float(0.15, "Minimum gap size (ATR)", minval = 0.0, step = 0.05, group = G_GAP)
float smallMax = input.float(0.35, "Small gaps are below (ATR)", minval = 0.05, step = 0.05, group = G_GAP)
float largeMin = input.float(0.80, "Large gaps start at (ATR)", minval = 0.10, step = 0.05, group = G_GAP)
string fillSrc = input.string("Wick", "Price used for fills", options = ["Wick", "Close"], group = G_GAP, tooltip = "Wick: a gap is touched or filled when the high or low reaches the level. Close: the bar must close at or beyond the level.")
int horizon = input.int(50, "Fill horizon (bars)", minval = 5, maxval = 500, group = G_STA, tooltip = "Every gap enters the statistics when it is this many bars old, with what happened by then. Younger gaps are not counted yet.")
int minN = input.int(20, "Gaps needed before a rate is shown", minval = 5, maxval = 500, group = G_STA)
bool showFilled = input.bool(true, "Keep filled gaps on chart", group = G_VIS)
int maxShowAge = input.int(300, "Remove unfilled gaps from the chart after (bars)", minval = 20, maxval = 2000, group = G_VIS, tooltip = "Statistics are not affected: every gap is counted when it reaches the fill horizon.")
bool showRate = input.bool(true, "Show the fill rate inside open gaps", group = G_VIS)
bool showTable = input.bool(true, "Show statistics table", group = G_VIS)
string tblPos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = G_VIS)
color bullCol = input.color(#22ab94, "Bullish gap", group = G_VIS)
color bearCol = input.color(#f23645, "Bearish gap", group = G_VIS)
if barstate.isfirst and smallMax >= largeMin
runtime.error("FVG Stats: the small-gap limit must be below the large-gap limit.")
//──────────────────────────────────────────────────────────────────────────────
// State
//──────────────────────────────────────────────────────────────────────────────
type Gap
float top
float bot
int bar
bool bull
int bucket
bool touched
bool half
bool full
int fullBars
bool counted
box bx
type Stat
int n
int touched
int half
int full
array<int> bars
var array<Gap> gaps = array.new<Gap>()
var array<Stat> stats = array.new<Stat>()
// Index 0-2: bullish small, medium, large. Index 3-5: bearish small, medium, large.
if barstate.isfirst
for i = 0 to 5
stats.push(Stat.new(0, 0, 0, 0, array.new<int>()))
float atr = ta.atr(14)
float h2 = high[2]
float l2 = low[2]
f_pos(string s) =>
s == "Top left" ? position.top_left : s == "Bottom right" ? position.bottom_right : s == "Bottom left" ? position.bottom_left : position.top_right
f_idx(Gap g) =>
(g.bull ? 0 : 3) + g.bucket
f_rateText(Stat s) =>
s.n >= minN ? "fill rate " + str.tostring(100.0 * s.full / s.n, "#") + "%, n=" + str.tostring(s.n) : "n=" + str.tostring(s.n)
//──────────────────────────────────────────────────────────────────────────────
// Bar logic: confirmed bars only
//──────────────────────────────────────────────────────────────────────────────
bool newBull = false
bool newBear = false
bool bullFill = false
bool bearFill = false
int showLimit = math.max(maxShowAge, horizon)
if barstate.isconfirmed
// 1. Update the gaps that are already open
if gaps.size() > 0
for i = gaps.size() - 1 to 0
Gap g = gaps.get(i)
int age = bar_index - g.bar
float mid = (g.top + g.bot) / 2.0
bool was = g.full
if not was
if g.bull
float p = fillSrc == "Wick" ? low : close
if p <= g.top
g.touched := true
if p <= mid
g.half := true
if p <= g.bot
g.full := true
g.fullBars := age
else
float p = fillSrc == "Wick" ? high : close
if p >= g.bot
g.touched := true
if p >= mid
g.half := true
if p >= g.top
g.full := true
g.fullBars := age
// Every gap is counted once, when it is exactly "horizon" bars old, with what happened
// by then. A filled gap waits like an unfilled one, so recent fills cannot push the
// rates up before the unfilled gaps of the same period are known.
if not g.counted and age >= horizon
Stat s = stats.get(f_idx(g))
s.n := s.n + 1
if g.touched
s.touched := s.touched + 1
if g.half
s.half := s.half + 1
if g.full
s.full := s.full + 1
s.bars.push(g.fullBars)
if s.bars.size() > 500
s.bars.shift()
g.counted := true
if g.full and not was
if g.bull
bullFill := true
else
bearFill := true
if showFilled
g.bx.set_right(bar_index)
g.bx.set_bgcolor(color.new(color.gray, 93))
g.bx.set_border_color(color.new(color.gray, 85))
g.bx.set_text("")
else
g.bx.delete()
if g.full
// Kept in the list (not on the chart) until it has been counted.
if g.counted
gaps.remove(i)
else if age > showLimit
// Already counted in the statistics; removed from the chart to keep it readable.
g.bx.delete()
gaps.remove(i)
else
g.bx.set_right(bar_index)
// 2. Detect a new gap on the bar that just closed
float bullSize = low - h2
float bearSize = l2 - high
if not na(atr) and atr > 0 and not na(h2)
if bullSize > 0 and bullSize >= minSizeAtr * atr
float sz = bullSize / atr
int bk = sz < smallMax ? 0 : sz < largeMin ? 1 : 2
box b = box.new(bar_index - 1, low, bar_index, h2, border_color = color.new(bullCol, 55), bgcolor = color.new(bullCol, 82), text_color = color.new(chart.fg_color, 20), text_size = size.small, text_halign = text.align_right, text_valign = text.align_center)
gaps.push(Gap.new(low, h2, bar_index, true, bk, false, false, false, na, false, b))
newBull := true
if bearSize > 0 and bearSize >= minSizeAtr * atr
float sz = bearSize / atr
int bk = sz < smallMax ? 0 : sz < largeMin ? 1 : 2
box b = box.new(bar_index - 1, l2, bar_index, high, border_color = color.new(bearCol, 55), bgcolor = color.new(bearCol, 82), text_color = color.new(chart.fg_color, 20), text_size = size.small, text_halign = text.align_right, text_valign = text.align_center)
gaps.push(Gap.new(l2, high, bar_index, false, bk, false, false, false, na, false, b))
newBear := true
//──────────────────────────────────────────────────────────────────────────────
// Fill rate inside the open gaps, and the table
//──────────────────────────────────────────────────────────────────────────────
var table tb = table.new(f_pos(tblPos), 6, 8, bgcolor = color.new(chart.bg_color, 10), border_color = color.new(chart.fg_color, 85), border_width = 1, frame_color = color.new(chart.fg_color, 70), frame_width = 1)
f_cell(int col, int row, string txt, color c) =>
table.cell(tb, col, row, txt, text_color = c, text_size = size.small, text_halign = text.align_left)
f_pct(int part, int whole) =>
whole >= minN ? str.tostring(100.0 * part / whole, "#") + "%" : "-"
f_row(int row, string name, Stat s, color c) =>
f_cell(0, row, name, c)
f_cell(1, row, f_pct(s.touched, s.n), chart.fg_color)
f_cell(2, row, f_pct(s.half, s.n), chart.fg_color)
f_cell(3, row, f_pct(s.full, s.n), chart.fg_color)
f_cell(4, row, s.bars.size() >= 5 ? str.tostring(s.bars.median(), "#") : "-", chart.fg_color)
f_cell(5, row, str.tostring(s.n), color.new(chart.fg_color, 40))
if barstate.islast
if showRate
for g in gaps
if not g.full
g.bx.set_text(f_rateText(stats.get(f_idx(g))) + " ")
if showTable
color fg = chart.fg_color
f_cell(0, 0, "FVG Fill Stats", fg)
f_cell(1, 0, "Touched", color.new(fg, 40))
f_cell(2, 0, "Half", color.new(fg, 40))
f_cell(3, 0, "Full", color.new(fg, 40))
f_cell(4, 0, "Median bars", color.new(fg, 40))
f_cell(5, 0, "n", color.new(fg, 40))
f_row(1, "Bullish small", stats.get(0), bullCol)
f_row(2, "Bullish medium", stats.get(1), bullCol)
f_row(3, "Bullish large", stats.get(2), bullCol)
f_row(4, "Bearish small", stats.get(3), bearCol)
f_row(5, "Bearish medium", stats.get(4), bearCol)
f_row(6, "Bearish large", stats.get(5), bearCol)
f_cell(0, 7, "Within " + str.tostring(horizon) + " bars of the gap, " + (fillSrc == "Wick" ? "by wick" : "by close"), color.new(fg, 40))
table.merge_cells(tb, 0, 7, 5, 7)
//──────────────────────────────────────────────────────────────────────────────
// Alerts
//──────────────────────────────────────────────────────────────────────────────
alertcondition(newBull, "New bullish gap", "FVG Stats: new bullish fair value gap on {{ticker}} {{interval}}")
alertcondition(newBear, "New bearish gap", "FVG Stats: new bearish fair value gap on {{ticker}} {{interval}}")
alertcondition(bullFill, "Bullish gap fully filled", "FVG Stats: a bullish fair value gap was fully filled on {{ticker}} {{interval}}")
alertcondition(bearFill, "Bearish gap fully filled", "FVG Stats: a bearish fair value gap was fully filled on {{ticker}} {{interval}}")A confirmed bar forms a bullish gap when its low exceeds the high two bars earlier, or a bearish gap when its high is below the low two bars earlier. Its size must meet the default 0.15 × 14-bar ATR minimum.
It is the share of previously detected gaps in the same direction-and-size bucket that were fully filled within the selected 50-bar horizon. The script shows only the sample count until that bucket reaches the default minimum of 20 gaps.
The default **Price used for fills** is **Wick**, using the low for bullish gaps and high for bearish gaps. Select **Close** to require a bar to close at or beyond each touch, midpoint, or full-fill level.
Gap detection and fill checks run on confirmed chart bars, not unfinished bars. The code specifies no market or timeframe, but its statistics depend on the available history and on gaps reaching the selected horizon.
No. It provides four TradingView alert conditions: new bullish or bearish gap, and full fill of a bullish or bearish gap. An alert reports an event, not an order or a complete trading plan.
No. They describe observed gaps, not trade returns or live performance, and are not financial advice. For a separately defined trading strategy, TradingView Strategy Tester is the source of truth for its historical test results; paper and live results require separate observation.
Describe the change in plain English and AlgoNexta rebuilds it as a fresh Pine v6 script — with its own explanation, evidence, and walkthrough.
Build your own versionEducational content only — not financial advice. TradingView is the source of truth for live data, compilation, and Strategy Tester results.