本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MohaveTrader
// Precision GapFinder [MohaveTrader] (GapFinder)
// Version History
// v1.0 — October 2026
// • First release
//
// GapFinder draws fair value gaps found on a higher timeframe (5-min by default) and marks the bar where price
// reclaims one. It also draws the overnight gap: the prior after-hours close, the premarket open, the 50% line
// between them and, if turned on, the prior 4PM close. A gap stays on the chart until a bar on the gap timeframe
// closes below it. Gaps far from price are hidden and come back when price returns.
//
// HOW TO USE
// • Use a 1-min chart with extended hours on. Gaps are found on the FVG Timeframe.
// • A blue box is a live gap. It turns white once its Reclaim mark has printed.
// • Midpoint Reclaim: the first close back above the midpoint after price trades below it.
// • Higher Low + Break: price enters the gap, its low holds for at least one bar, then a close above the high of the bar that made that low.
// • One mark per gap. The mark shows what price did; it is not a buy signal. Use confirmation of your choice.
// • A close below the gap on the FVG Timeframe removes it.
// • Nearest Gap Only hides a lower gap while a newer gap sits above it.
// • On seconds charts a single bar close can print a mark.
//@version=6
indicator("Precision GapFinder", shorttitle = "GapFinder", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500, max_bars_back = 5000)
max_bars_back(time, 5000)
grpE = "01. Reclaim"
entryMode = input.string("Midpoint Reclaim", "Reclaim Method", options = ["Midpoint Reclaim", "Higher Low + Break"], group = grpE, tooltip = "Midpoint Reclaim: first close back above the gap midpoint after price trades below it. Higher Low + Break: after price enters the gap, its lowest low holds for at least one bar, then a close above the high of the bar that made that low. One mark per gap. The mark describes what price did; it is not a buy signal. Default: Midpoint Reclaim.")
fvgTf = input.timeframe("5", "FVG Timeframe", group = grpE, tooltip = "Gaps are found on this timeframe and closed through on its close. Reclaim marks are designed for the 1-min chart. Default: 5 minutes.")
showEntry = input.bool(true, "Show Reclaim Marks", group = grpE, tooltip = "Default: on.")
livePrev = input.bool(true, "Live Reclaim Preview", group = grpE, tooltip = "Shows the Reclaim mark on the forming bar as soon as it passes, before it closes. Nothing is committed until the bar closes; if the bar fades, the mark disappears. Default: on.")
grpG = "02. Gaps"
showGaps = input.bool(true, "Show Gaps", group = grpG, tooltip = "Default: on.")
showMid = input.bool(true, "Show Midpoint", group = grpG, tooltip = "Default: on.")
showGapLbl = input.bool(true, "Show Gap Labels", group = grpG, tooltip = "Writes Gap, the midpoint price and the size at the right end of each gap. Default: on.")
showTag = input.bool(true, "Show Size in Label", group = grpG, tooltip = "Gap height as a multiple of ATR on the FVG timeframe. Default: on.")
atrLen = input.int(14, "ATR Length", minval = 1, group = grpG, tooltip = "Default: 14.")
minSize = input.float(0.75, "Minimum Size (x ATR)", minval = 0.0, step = 0.05, group = grpG, tooltip = "Gaps smaller than this are not drawn. Default: 0.75.")
maxLive = input.int(10, "Max Gaps Shown", minval = 1, maxval = 50, group = grpG, tooltip = "When a new gap forms past this count, the oldest is removed. Default: 10.")
relevanceMult = input.float(5.0, "Relevance Distance x ATR", minval = 0.0, step = 0.5, group = grpG, tooltip = "Hide gaps whose midpoint is farther than this from price. They are kept, not deleted, and come back when price returns. Measured in ATR on the FVG timeframe, so it covers the same distance on any chart timeframe. Checked on bar close. 0 shows all. Default: 5.0.")
relBuf = input.float(10.0, "Relevance Buffer %", minval = 0.0, maxval = 50.0, step = 5.0, group = grpG, tooltip = "A hidden gap comes back only when price is this much inside the Relevance Distance, so a gap near the edge does not flicker. Default: 10.")
topStep = input.bool(true, "Nearest Gap Only", group = grpG, tooltip = "Hides a gap while a newer gap sits above it and price has not traded back into it since it formed, so the nearest floor under price is the one drawn. A hidden gap reappears when price trades into it or the gap above is closed through. Hidden gaps are kept, not deleted. Default: on.")
dupPct = input.float(50.0, "Skip Duplicate Overlap %", minval = 0.0, maxval = 100.0, step = 5.0, group = grpG, tooltip = "A new gap that overlaps a live gap by this much of the smaller gap's height is not drawn. Default: 50.")
grpO = "03. Overnight Gap"
showOn = input.bool(true, "Show Overnight Gap", group = grpO, tooltip = "Prior after-hours close to the premarket open. Draws only on an intraday chart with extended hours on. Default: on.")
showHalf = input.bool(true, "Show 50% Line", group = grpO, tooltip = "Default: on.")
halfMin = input.float(0.25, "50% Line Minimum Gap (x Daily ATR)", minval = 0.0, step = 0.05, group = grpO, tooltip = "The 50% line draws only when the gap is at least this size. Default: 0.25.")
show4pm = input.bool(false, "Show 4PM Close", group = grpO, tooltip = "Prior regular-session close. Default: off.")
grpD = "04. Lines"
lineExt = input.int(20, "Extend (bars)", minval = 0, group = grpD, tooltip = "Bars the gaps and overnight lines extend past the last bar. Default: 20.")
showOnLbl = input.bool(true, "Label Overnight Lines", group = grpD, tooltip = "Writes AH Close, PM Open, 50% and 4PM Close with their price at the right end of each line. Default: on.")
lblOff = input.int(2, "Overnight Label Offset (bars)", minval = 0, group = grpD, tooltip = "Distance of the AH Close, PM Open, 50% and 4PM Close labels past the right end. Moves only the text. Default: 2.")
gapLblOff = input.int(14, "Gap Label Offset (bars)", minval = 0, group = grpD, tooltip = "Distance of the gap labels past the right end. Set apart from the overnight labels so the two columns do not overlap. Default: 14.")
mergeTol = input.float(0.1, "Merge AH and 4PM Labels Within (x Daily ATR)", minval = 0.0, step = 0.05, group = grpD, tooltip = "When the 4PM Close is this close to the AH Close, one label shows both prices. 0 never merges. Default: 0.1.")
onLblSize = input.string(size.normal, "Label Size", options = [size.tiny, size.small, size.normal, size.large], group = grpD, tooltip = "Default: normal.")
onW = input.int(2, "Overnight Line Width", minval = 1, maxval = 4, group = grpD, tooltip = "Default: 2.")
midW = input.int(1, "Midpoint Width", minval = 1, maxval = 4, group = grpD, tooltip = "Default: 1.")
gapBdW = input.int(0, "Gap Border Width", minval = 0, maxval = 4, group = grpD, tooltip = "0 draws the gaps as fill only, so no edge line crosses the candles. Default: 0.")
grpM = "05. Markers"
cEntry = input.color(#00E676, "Reclaim Mark", group = grpM)
entStyle = input.string("▲", "Reclaim Mark Style", options = ["Text", "●", "◆", "◉", "○", "▲", "▼"], group = grpM, tooltip = "Text uses the text field below; a symbol draws that symbol. Marks the bar where price reclaimed the gap. Default: ▲.")
entText = input.string("GF", "Reclaim Mark Text", group = grpM, tooltip = "Used when Reclaim Mark Style is Text. Default: GF.")
mkSizeIn = input.string("Small", "Mark Size", options = ["Auto", "Tiny", "Small", "Normal", "Large"], group = grpM, tooltip = "Size of the Reclaim mark when its style is Text. Auto lets TradingView scale the mark with the bar width. Default: Small.")
dotSizeIn = input.string("Normal", "Dot Size", options = ["Auto", "Tiny", "Small", "Normal", "Large"], group = grpM, tooltip = "Size of the Reclaim mark when drawn as a symbol. Auto lets TradingView scale the mark with the bar width. Default: Normal.")
mkPct = input.float(5.0, "Mark Spacing (% of day range)", minval = 0.0, step = 0.5, group = grpM, tooltip = "Gap between the mark and its candle, as a percent of the day's high-to-low range. All of a day's marks share one gap, so they sit the same distance from their candles on any view of that day; today's marks are re-spaced as the range grows. Default: 5.")
grpW = "06. Warning"
warnPos = input.string(position.top_right, "Warning Position", options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group = grpW)
warnSize = input.string(size.normal, "Warning Text", options = [size.tiny, size.small, size.normal, size.large], group = grpW)
grpC = "07. Colors"
colGap = input.color(#448AFF, "Gap", group = grpC, tooltip = "Color of a live gap and its label. Default: blue #448AFF.")
gapFillT = input.int(85, "Gap Fill Transparency", minval = 0, maxval = 100, group = grpC, tooltip = "Default: 85.")
gapBdT = input.int(40, "Gap Border Transparency", minval = 0, maxval = 100, group = grpC, tooltip = "Applies to live and used gaps. Default: 40.")
colUsed = input.color(#FFFFFF, "Used Gap", group = grpC, tooltip = "A live gap whose Reclaim mark has already printed. Default: white.")
usedFillT = input.int(88, "Used Gap Fill Transparency", minval = 0, maxval = 100, group = grpC, tooltip = "Default: 88.")
colMid = input.color(#2962FF, "Midpoint", group = grpC, tooltip = "Default: blue #2962FF.")
colAh = input.color(#FF9800, "After-Hours Close", group = grpC)
colPm = input.color(#D1D4DC, "Premarket Open", group = grpC)
colHalf = input.color(color.new(#9598A1, 30), "50% Line", group = grpC)
col4pm = input.color(#B388FF, "4PM Close", group = grpC)
grpA = "08. Alerts"
alEntry = input.bool(true, "Reclaim", group = grpA, tooltip = "Fires when a Reclaim mark is committed. Create one alert on GapFinder using Any alert() function call; these switches decide which events send. All alerts fire on bar close. An existing alert keeps the settings it was created with, so recreate it after changing these. Default: on.")
alGap = input.bool(false, "New Gap", group = grpA, tooltip = "Fires when a new gap is drawn. Default: off.")
type Gap
float bot
float top
int b0
float sz
bool touched = false
bool tested = false
bool used = false
float minLow
float lowHi
int held = 0
bool hit = false
bool far = false
xExt = bar_index + lineExt
xLbl = xExt + lblOff
xGapLbl = xExt + gapLblOff
var array<int> tfBars = array.new<int>()
f_num(float v) => str.tostring(v, format.mintick)
var array<Gap> gaps = array.new<Gap>()
var array<box> drawnBoxes = array.new<box>()
var array<line> drawnLines = array.new<line>()
var array<label> drawnLabels = array.new<label>()
f_pass(Gap g, float mid) =>
entryMode == "Midpoint Reclaim" ? (g.tested or low < mid) and close > mid : g.touched and g.held >= 1 and low >= g.minLow and close > g.lowHi
tfOk = timeframe.in_seconds() <= timeframe.in_seconds(fvgTf)
onOk = timeframe.isintraday and syminfo.session == session.extended
tfChg = timeframe.change(fvgTf)
newTf = tfOk and tfChg
if tfChg
tfBars.push(bar_index)
if tfBars.size() > 4
tfBars.shift()
bool hGap = false
float hBot = na
float hTop = na
float hClose = na
float hAtr = na
int hTime = na
if tfOk
[g0, b0, t0, c0, a0, tm0] = request.security(syminfo.tickerid, fvgTf, [low[1] > high[3], high[3], low[1], close[1], ta.atr(atrLen)[1], time[2]], lookahead = barmerge.lookahead_on)
hGap := g0
hBot := b0
hTop := t0
hClose := c0
hAtr := a0
hTime := tm0
bool entryNow = false
bool entryPrev = false
bool gapNew = false
float eBot = na
float eTop = na
float eSz = na
float nBot = na
float nTop = na
if barstate.isconfirmed and tfOk
if newTf
if array.size(gaps) > 0
for i = array.size(gaps) - 1 to 0
if hClose < array.get(gaps, i).bot
array.remove(gaps, i)
if hGap and hAtr > 0 and not na(hTime) and tfBars.size() >= 3
float sz = (hTop - hBot) / hAtr
if sz >= minSize
bool dup = false
if array.size(gaps) > 0
for g in gaps
float ov = math.min(hTop, g.top) - math.max(hBot, g.bot)
float small = math.min(hTop - hBot, g.top - g.bot)
if ov > 0 and small > 0 and ov >= small * dupPct / 100
dup := true
if not dup
array.push(gaps, Gap.new(bot = hBot, top = hTop, b0 = tfBars.get(tfBars.size() - 3), sz = sz))
gapNew := true
nBot := hBot
nTop := hTop
if array.size(gaps) > maxLive
array.shift(gaps)
float relC = relevanceMult <= 0.0 or na(hAtr) ? na : hAtr * relevanceMult
if array.size(gaps) > 0
for g in gaps
if low <= g.top
g.hit := true
float dm = math.abs(close - (g.top + g.bot) / 2)
if na(relC)
g.far := false
else if not g.far and dm > relC
g.far := true
else if g.far and dm <= relC * (1 - relBuf / 100)
g.far := false
if not g.used
float mid = (g.top + g.bot) / 2
bool fire = f_pass(g, mid)
if entryMode == "Midpoint Reclaim"
if low < mid
g.tested := true
else
if low <= g.top
g.touched := true
if g.touched
if na(g.minLow) or low < g.minLow
g.minLow := low
g.lowHi := high
g.held := 0
else
g.held += 1
if fire
g.used := true
if not entryNow
eBot := g.bot
eTop := g.top
eSz := g.sz
entryNow := true
else if tfOk and livePrev and array.size(gaps) > 0
for g in gaps
if not g.used and not entryPrev
float mid = (g.top + g.bot) / 2
if f_pass(g, mid)
entryPrev := true
eBot := g.bot
eTop := g.top
eSz := g.sz
if barstate.islast
if drawnBoxes.size() > 0
for b in drawnBoxes
box.delete(b)
drawnBoxes.clear()
if drawnLines.size() > 0
for l in drawnLines
line.delete(l)
drawnLines.clear()
if drawnLabels.size() > 0
for l in drawnLabels
label.delete(l)
drawnLabels.clear()
if showGaps and tfOk and gaps.size() > 0
int n = gaps.size()
for i = 0 to n - 1
Gap g = gaps.get(i)
float m = (g.top + g.bot) / 2
bool covered = false
if topStep and not g.hit and i < n - 1
for j = i + 1 to n - 1
if gaps.get(j).bot > g.bot
covered := true
if not covered and not g.far
color c = g.used ? colUsed : colGap
int lft = math.max(g.b0, bar_index - 4999)
drawnBoxes.push(box.new(lft, g.top, xExt, g.bot, xloc = xloc.bar_index, border_color = color.new(c, gapBdT), border_width = gapBdW, bgcolor = color.new(c, g.used ? usedFillT : gapFillT)))
if showMid
drawnLines.push(line.new(lft, m, xExt, m, xloc = xloc.bar_index, color = colMid, style = line.style_dotted, width = midW))
if showGapLbl
string t = "Gap " + f_num(m) + (showTag ? " " + str.tostring(g.sz, "0.00") + "x" : "")
drawnLabels.push(label.new(xGapLbl, m, t, xloc = xloc.bar_index, style = label.style_label_left, color = color.new(#000000, 100), textcolor = c, size = onLblSize, yloc = yloc.price, textalign = text.align_left))
newDay = timeframe.change("D")
f_dAtr() =>
float a = ta.atr(14)
float c = ta.cum(ta.tr(true)) / (bar_index + 1)
na(a) ? c : a
dAtr = request.security(syminfo.tickerid, "D", f_dAtr(), lookahead = barmerge.lookahead_off)
var float rthClose = na
var float prevRth = na
var line lAh = na
var line lPo = na
var line lHf = na
var line l4 = na
var label bAh = na
var label bPo = na
var label bHf = na
var label b4 = na
f_lvl(float y, color c, int w, string st, string txt, bool withLbl) =>
line ln = line.new(bar_index, y, xExt, y, xloc = xloc.bar_index, color = c, width = w, style = st)
label lb = showOnLbl and withLbl ? label.new(xLbl, y, txt, xloc = xloc.bar_index, style = label.style_label_left, color = color.new(#000000, 100), textcolor = c, size = onLblSize, yloc = yloc.price, textalign = text.align_left) : na
[ln, lb]
f_ext(line ln, label lb) =>
if not na(ln)
line.set_x2(ln, xExt)
if not na(lb)
label.set_x(lb, xLbl)
if newDay
prevRth := rthClose
line.delete(lAh)
line.delete(lPo)
line.delete(lHf)
line.delete(l4)
label.delete(bAh)
label.delete(bPo)
label.delete(bHf)
label.delete(b4)
lAh := na
lPo := na
lHf := na
l4 := na
bAh := na
bPo := na
bHf := na
b4 := na
float ah = close[1]
float po = open
bool drawOn = showOn and onOk and not na(ah) and math.abs(po - ah) > syminfo.mintick
bool draw4 = show4pm and onOk and not na(prevRth)
bool mrgLbl = drawOn and draw4 and mergeTol > 0 and dAtr > 0 and math.abs(prevRth - ah) <= mergeTol * dAtr
if drawOn
string ahTxt = "AH Close " + f_num(ah) + (mrgLbl ? " · 4PM " + f_num(prevRth) : "")
[la, ba] = f_lvl(ah, colAh, onW, line.style_solid, ahTxt, true)
lAh := la
bAh := ba
[lp, bp] = f_lvl(po, colPm, onW, line.style_solid, "PM Open " + f_num(po), true)
lPo := lp
bPo := bp
if showHalf and dAtr > 0 and math.abs(po - ah) >= halfMin * dAtr
float hf = (ah + po) / 2
[lh, bh] = f_lvl(hf, colHalf, math.max(1, onW - 1), line.style_dashed, "50% " + f_num(hf), true)
lHf := lh
bHf := bh
if draw4
[l4n, b4n] = f_lvl(prevRth, col4pm, onW, line.style_solid, "4PM Close " + f_num(prevRth), not mrgLbl)
l4 := l4n
b4 := b4n
if session.ismarket
rthClose := close
f_ext(lAh, bAh)
f_ext(lPo, bPo)
f_ext(lHf, bHf)
f_ext(l4, b4)
f_sz(string s) => s == "Auto" ? size.auto : s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Normal" ? size.normal : size.large
f_glyph(string sty, string txt) => sty == "Text" ? txt : sty
f_markSize(string sty) => sty == "Text" ? f_sz(mkSizeIn) : f_sz(dotSizeIn)
var array<label> mkL = array.new<label>()
var array<float> mkA = array.new<float>()
var float mkDHi = na
var float mkDLo = na
f_gap(float r) => na(r) or r <= 0 ? 0.0 : r * mkPct / 100.0
f_respace(float r) =>
int n = array.size(mkL)
if n > 0
for k = 0 to n - 1
label.set_y(array.get(mkL, k), array.get(mkA, k) - f_gap(r))
true
if newDay or na(mkDHi)
f_respace(mkDHi - mkDLo)
array.clear(mkL)
array.clear(mkA)
mkDHi := high
mkDLo := low
else
mkDHi := math.max(mkDHi, high)
mkDLo := math.min(mkDLo, low)
mkRng = mkDHi - mkDLo
if showEntry and (entryNow or entryPrev)
float eMid = (eTop + eBot) / 2
string tip = "Reclaim | " + entryMode + " | gap " + f_num(eBot) + " to " + f_num(eTop) + " | midpoint " + f_num(eMid) + " | size " + str.tostring(eSz, "0.00") + "x ATR" + (entryPrev ? " | forming: committed only if the bar still passes at the close" : " | committed on this bar close")
label lbE = label.new(bar_index, low - f_gap(mkRng), f_glyph(entStyle, entText), style = label.style_none, textcolor = cEntry, size = f_markSize(entStyle), yloc = yloc.price, tooltip = tip)
array.push(mkL, lbE)
array.push(mkA, low)
if barstate.islast
f_respace(mkRng)
f_alTf() =>
int sec = timeframe.in_seconds()
sec < 60 ? str.tostring(sec) + "s" : sec < 3600 ? str.tostring(math.round(sec / 60)) + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600)) + "h" : timeframe.period
alHead = syminfo.ticker + " " + f_alTf()
alClose = " | close " + f_num(close)
if barstate.isconfirmed
if alEntry and entryNow
alert("GapFinder RECLAIM (" + entryMode + ") | " + alHead + " | gap " + f_num(eBot) + " to " + f_num(eTop) + " | midpoint " + f_num((eTop + eBot) / 2) + alClose, alert.freq_once_per_bar_close)
if alGap and gapNew
alert("GapFinder NEW GAP | " + alHead + " | " + f_num(nBot) + " to " + f_num(nTop) + alClose, alert.freq_once_per_bar_close)
var table warn = na
if barstate.islast and not tfOk
if na(warn)
warn := table.new(warnPos, 1, 1)
table.cell(warn, 0, 0, "GapFinder: set the chart timeframe at or below the FVG timeframe", text_color = #FFFFFF, bgcolor = color.new(#F23645, 20), text_size = warnSize)