// ============================================================================ // © 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 bars var array gaps = array.new() var array stats = array.new() // 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())) 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}}")