本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KomiTrade — Pavel
//
// Gap Fill vs Mirror [KomiTrade]
// Version 0.1.1 (2026-11-19) · compiled in TV 2026-11-13 (MNQ1! 1h, no errors).
//
// Originality check (2026-10-29): gap fill statistics already exist on TradingView (e.g. "Gap Closure Stats", CME gap
// trackers), but without a control group. This script adds a control level (the mirror at the same distance) and a paired test.
//
// What it does: "gaps always fill" - really, or does price just travel that far in any direction? For every gap (default:
// weekend gap of futures = first bar of a new week vs last close of the previous week; optional daily gap) it draws the gap zone
// and a MIRROR LEVEL at the same distance on the other side of the open. Until the next gap it records whether price reached the
// gap fill (previous close) and whether it reached the mirror. The table compares the share of filled gaps with the share of
// reached mirror levels over the last N gaps, with a paired test (only gaps where exactly one of the two was reached;
// z = (b - c) / sqrt(b + c), up / down when |z| >= 2, otherwise approx.).
//
// Design: presets via input.enum, labels in English / Czech / German / Spanish, theme from chart.bg_color, layer switches,
// calculations on confirmed bars only (no request.security, varip, timenow or calc_on_every_tick), compact mode for phones.
// Text never covers bars: past gaps keep a small dot on each level (result in its tooltip); the current gap is labelled to the
// right of the last bar, where there are no bars.
//
// Limitations: each gap is measured until the next gap only (one week / one day); a touch = the bar's high / low reached the
// level (no slippage); on the first bar after the gap a touch inside that bar also counts (its open is known at its start);
// gaps below the minimum size are skipped; results come from the chart history. A description of price behaviour, not a signal.
//
// Changelog:
// 0.1.0 (2026-10-29) first version.
// 0.1.1 (2026-11-19) text no longer covers bars: dots with tooltips on past gaps, labels of the current gap right of the
// last bar; close levels share one label; larger table text (normal, compact = small); comments in English.
//@version=6
indicator("Gap Fill vs Mirror [KomiTrade]", shorttitle = "Gap Mirror", overlay = true, max_boxes_count = 100, max_lines_count = 100, max_labels_count = 100)
// ───────────────────────── Inputs ─────────────────────────
enum Preset
weekend = "Weekend gap (futures, CME crypto)"
daily = "Daily gap (stocks, futures sessions)"
enum Lang
en = "English"
cs = "Czech (CZ)"
de = "German (DE)"
es = "Spanish (ES)"
enum Theme
automatic = "Auto (chart background)"
dark = "Dark"
light = "Light"
const string G_GEN = "General"
const string G_LAY = "Layers"
Preset iPreset = input.enum(Preset.weekend, "Gap type", group = G_GEN, tooltip = "Weekend: first bar of a new week vs last close of the previous week. Daily: first bar of a new trading day vs previous close.")
float iMinPct = input.float(0.05, "Minimum gap (% of price)", minval = 0.0, maxval = 5.0, step = 0.01, group = G_GEN)
int iStatN = input.int(52, "Statistics: last N gaps", minval = 10, maxval = 500, group = G_GEN)
Lang iLang = input.enum(Lang.en, "Language", group = G_GEN, tooltip = "Language of labels and table. Input names stay in English.")
Theme iTheme = input.enum(Theme.automatic, "Theme", group = G_GEN)
bool iCompact = input.bool(false, "Compact mode (phone)", group = G_GEN)
bool iShowZone = input.bool(true, "Show gap zone", group = G_LAY)
bool iShowMir = input.bool(true, "Show mirror level", group = G_LAY)
bool iShowLbl = input.bool(true, "Show labels", group = G_LAY)
int iKeep = input.int(4, "Gaps to keep on chart", minval = 1, maxval = 30, group = G_LAY)
bool iShowTbl = input.bool(true, "Show table", group = G_LAY)
string iTblPos = input.string(position.top_right, "Table position", options = [position.top_right, position.bottom_right, position.top_left, position.bottom_left], group = G_LAY)
tx(string en, string cs, string de, string es) =>
switch iLang
Lang.cs => cs
Lang.de => de
Lang.es => es
=> en
// ───────────────────────── Theme ─────────────────────────
color bg = chart.bg_color
float bgLum = (0.299 * color.r(bg) + 0.587 * color.g(bg) + 0.114 * color.b(bg)) / 255.0
bool isDark = iTheme == Theme.automatic ? bgLum < 0.5 : iTheme == Theme.dark
color cText = isDark ? color.rgb(226, 232, 240) : color.rgb(30, 41, 59)
color cMute = isDark ? color.rgb(148, 163, 184) : color.rgb(100, 116, 139)
color cPanel = isDark ? color.rgb(22, 30, 46) : color.rgb(241, 245, 249)
color cGap = isDark ? color.rgb(76, 141, 255) : color.rgb(29, 78, 216)
color cMir = isDark ? color.rgb(167, 139, 250) : color.rgb(109, 40, 217)
string tSize = iCompact ? size.small : size.normal // readable table and labels (Pavel 2026-11-19: table text too small)
string tNote = iCompact ? size.tiny : size.small
// ───────────────────────── Gaps (confirmed bars only) ─────────────────────────
type Gap
float fillLv
float mirLv
bool up
int x0 = 0
bool filled = false
bool mirror = false
box bx = na
line ln = na
label lf = na
label lm = na
var array<Gap> live = array.new<Gap>()
var array<bool> hF = array.new<bool>() // filled before the next gap
var array<bool> hM = array.new<bool>() // mirror reached before the next gap
var Gap g = na
bool newPeriod = iPreset == Preset.weekend ? timeframe.change("W") : timeframe.change("D")
// Level texts (✓ once reached) - in the dot tooltip and in the labels of the current gap
fillTxt(Gap x) =>
tx("Gap fill ", "Zaplnění ", "Lückenschluss ", "Cierre ") + str.tostring(x.fillLv, format.mintick) + (x.filled ? " ✓" : "")
mirTxt(Gap x) =>
tx("Mirror ", "Zrcadlo ", "Spiegel ", "Espejo ") + str.tostring(x.mirLv, format.mintick) + (x.mirror ? " ✓" : "")
// Label as a small dot on the level at the gap start (no text over bars)
dotLbl(label l, int x, float y, color c) =>
if not na(l)
l.set_xy(x, y)
l.set_color(c)
l.set_text("")
l.set_style(label.style_circle)
l.set_size(size.tiny)
markTouched(Gap x) =>
if not na(x)
if not x.filled and (x.up ? low <= x.fillLv : high >= x.fillLv)
x.filled := true
if not na(x.lf)
x.lf.set_tooltip(fillTxt(x))
if not na(x.bx)
x.bx.set_bgcolor(color.new(cGap, 94))
if not x.mirror and (x.up ? high >= x.mirLv : low <= x.mirLv)
x.mirror := true
if not na(x.lm)
x.lm.set_tooltip(mirTxt(x))
if barstate.isconfirmed and bar_index > 0
if newPeriod
// close the previous gap into the statistics
if not na(g)
hF.push(g.filled)
hM.push(g.mirror)
if hF.size() > 500
hF.shift()
hM.shift()
if not na(g.bx)
g.bx.set_right(bar_index - 1)
if not na(g.ln)
g.ln.set_x2(bar_index - 1)
dotLbl(g.lf, g.x0, g.fillLv, cGap)
dotLbl(g.lm, g.x0, g.mirLv, cMir)
g := na
float pc = close[1]
float gp = open - pc
if pc > 0 and math.abs(gp) / pc * 100 >= iMinPct
g := Gap.new(pc, open + gp, gp > 0, x0 = bar_index)
if iShowZone
g.bx := box.new(bar_index, math.max(open, pc), bar_index + 1, math.min(open, pc), border_color = color.new(cGap, 50), bgcolor = color.new(cGap, 82))
if iShowMir
g.ln := line.new(bar_index, g.mirLv, bar_index + 1, g.mirLv, color = color.new(cMir, 25), style = line.style_dashed)
if iShowLbl
g.lf := label.new(bar_index, pc, "", style = label.style_circle, size = size.tiny, color = cGap, textcolor = cText, tooltip = fillTxt(g))
if iShowMir
g.lm := label.new(bar_index, g.mirLv, "", style = label.style_circle, size = size.tiny, color = cMir, textcolor = cText, tooltip = mirTxt(g))
live.push(g)
while live.size() > iKeep
Gap o = live.shift()
box.delete(o.bx)
line.delete(o.ln)
label.delete(o.lf)
label.delete(o.lm)
if not na(g)
markTouched(g)
if not na(g.bx)
g.bx.set_right(bar_index)
if not na(g.ln)
g.ln.set_x2(bar_index)
// Current gap: labels right of the last bar (no bars there); levels closer than 1.5 ATR share one label at the gap fill
float atrV = ta.atr(14)
if barstate.islast and not na(g) and iShowLbl
int xr = bar_index + 3
bool near = not na(g.lm) and math.abs(g.mirLv - g.fillLv) < 1.5 * nz(atrV, 0)
if not na(g.lf)
g.lf.set_xy(xr, g.fillLv)
g.lf.set_style(label.style_label_left)
g.lf.set_size(tSize)
g.lf.set_color(color.new(cPanel, 10))
g.lf.set_text(near ? fillTxt(g) + "\n" + mirTxt(g) : fillTxt(g))
if not na(g.lm)
g.lm.set_xy(xr, g.mirLv)
g.lm.set_style(near ? label.style_none : label.style_label_left)
g.lm.set_size(tSize)
g.lm.set_color(color.new(cPanel, 10))
g.lm.set_text(near ? "" : mirTxt(g))
// ───────────────────────── Statistics ─────────────────────────
int nG = math.min(iStatN, hF.size())
int kF = 0
int kM = 0
int bOnly = 0 // filled, mirror not
int cOnly = 0 // mirror, not filled
if barstate.islast and nG > 0
for i = hF.size() - nG to hF.size() - 1
bool f = hF.get(i)
bool m = hM.get(i)
kF += f ? 1 : 0
kM += m ? 1 : 0
bOnly += f and not m ? 1 : 0
cOnly += m and not f ? 1 : 0
pctOf(int k, int n) =>
n > 0 ? str.tostring(math.round(100.0 * k / n)) + " %" : "—"
plot(nG > 0 ? 100.0 * kF / nG : na, "Fill rate %", display = display.data_window)
plot(nG > 0 ? 100.0 * kM / nG : na, "Mirror rate %", display = display.data_window)
var table tbl = table.new(iTblPos, 2, 6, border_width = 1, frame_width = 1, frame_color = color.new(cMute, 60))
if iShowTbl and barstate.islast
color hb = color.new(cPanel, 0)
float z = bOnly + cOnly > 0 ? (bOnly - cOnly) / math.sqrt(bOnly + cOnly) : 0.0
string verdict = nG < 20 ? tx("few gaps", "málo mezer", "wenige Lücken", "pocos huecos") : z >= 2 ? tx("gap fill more often than mirror ↑", "zaplnění častěji než zrcadlo ↑", "Lückenschluss öfter als Spiegel ↑", "cierre más que espejo ↑") : z <= -2 ? tx("mirror more often than gap fill ↓", "zrcadlo častěji než zaplnění ↓", "Spiegel öfter als Lückenschluss ↓", "espejo más que cierre ↓") : tx("no clear difference ≈", "bez jasného rozdílu ≈", "kein klarer Unterschied ≈", "sin diferencia clara ≈")
table.cell(tbl, 0, 0, (iPreset == Preset.weekend ? tx("Weekend gaps", "Víkendové mezery", "Wochenend-Lücken", "Huecos de fin de semana") : tx("Daily gaps", "Denní mezery", "Tägliche Lücken", "Huecos diarios")) + " · n " + str.tostring(nG), text_color = cText, bgcolor = hb, text_size = tSize, text_halign = text.align_left)
table.merge_cells(tbl, 0, 0, 1, 0)
table.cell(tbl, 0, 1, tx("Filled before next gap", "Zaplněno do další mezery", "Gefüllt bis zur nächsten Lücke", "Cerrado antes del siguiente"), text_color = cMute, bgcolor = hb, text_size = tSize, text_halign = text.align_left)
table.cell(tbl, 1, 1, pctOf(kF, nG), text_color = cGap, bgcolor = hb, text_size = tSize)
table.cell(tbl, 0, 2, tx("Mirror reached (same distance)", "Zrcadlo dosaženo (stejná vzdálenost)", "Spiegel erreicht (gleiche Distanz)", "Espejo alcanzado (misma distancia)"), text_color = cMute, bgcolor = hb, text_size = tSize, text_halign = text.align_left)
table.cell(tbl, 1, 2, pctOf(kM, nG), text_color = cMir, bgcolor = hb, text_size = tSize)
table.cell(tbl, 0, 3, verdict, text_color = cText, bgcolor = hb, text_size = tSize, text_halign = text.align_left)
table.merge_cells(tbl, 0, 3, 1, 3)
if not iCompact
table.cell(tbl, 0, 4, tx("Only one of the pair: fill ", "Jen jedno z dvojice: zaplnění ", "Nur eines: Schluss ", "Solo uno: cierre ") + str.tostring(bOnly) + tx(" · mirror ", " · zrcadlo ", " · Spiegel ", " · espejo ") + str.tostring(cOnly) + " · z " + str.tostring(z, "0.0"), text_color = cMute, bgcolor = hb, text_size = tNote, text_halign = text.align_left)
table.merge_cells(tbl, 0, 4, 1, 4)
table.cell(tbl, 0, 5, tx("Closed bars, chart history. Description, not a signal.", "Zavřené svíčky, historie grafu. Popis, ne signál.", "Geschlossene Kerzen, Chart-Historie. Beschreibung, kein Signal.", "Velas cerradas, historial del gráfico. Descripción, no señal."), text_color = cMute, bgcolor = hb, text_size = tNote, text_halign = text.align_left)
table.merge_cells(tbl, 0, 5, 1, 5)