本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("HTF FVG POI", shorttitle = "HTF POI", overlay = true, max_boxes_count = 50, max_labels_count = 50)
// Shows higher-timeframe fair value gaps ONLY when price is close to them.
// Everything else is tracked silently in memory, so the chart stays clean.
//#region [INPUTS]
grpGen = 'General'
biasFilter = input.string('Neutral', 'Bias Filter', options = ['Neutral', 'Bullish', 'Bearish'], group = grpGen, tooltip = 'Bullish = only bullish FVGs are shown. Bearish = only bearish FVGs.')
sideOnly = input.bool(false, 'Trade-Side Only', group = grpGen, tooltip = 'ON = bullish gaps only while below price, bearish gaps only while above price (or touching it). Cuts clutter further.')
showTable = input.bool(true, 'Show Active Timeframes', group = grpGen, tooltip = 'Small note in the corner listing which timeframes are being scanned on this chart.')
grpTf = 'Timeframes'
autoTf = input.bool(true, 'Auto Timeframes', group = grpTf, tooltip = 'Picks the FVG timeframes from your chart timeframe:\n1m -> 15m, 1H, 4H\n3m -> 30m, 1H, 4H\n5m -> 15m, 1H, 4H\n15m -> 1H, 4H, 1D\n30m -> 1H, 4H, 1D\n1H -> 4H, 1D, 1W\n4H -> 1D, 1W\nOther chart timeframes use the custom timeframes below.')
use1 = input.bool(true, 'Slot 1', group = grpTf, inline = 's1')
cTf1 = input.timeframe('60', 'Custom', group = grpTf, inline = 's1')
use2 = input.bool(true, 'Slot 2', group = grpTf, inline = 's2')
cTf2 = input.timeframe('240', 'Custom', group = grpTf, inline = 's2')
use3 = input.bool(true, 'Slot 3', group = grpTf, inline = 's3')
cTf3 = input.timeframe('1D', 'Custom', group = grpTf, inline = 's3')
grpZone = 'Zones'
bullClr = input.color(color.new(color.green, 92), 'Colors', group = grpZone, inline = 'clr')
bearClr = input.color(color.new(color.red, 92), '', group = grpZone, inline = 'clr')
prox = input.float(1.5, 'Show Within (x ATR)', minval = 0.1, step = 0.25, group = grpZone, tooltip = 'A zone is only drawn when price is within this many chart-timeframe ATRs of it. Lower = quieter chart.')
exitProx = input.float(2.5, 'Hide Beyond (x ATR)', minval = 0.1, step = 0.25, group = grpZone, tooltip = 'Once a zone is showing, it stays until price is further than this. Keep it bigger than Show Within so zones do not blink on and off at the edge.')
atrLen = input.int(14, 'ATR Length', minval = 1, group = grpZone)
minSize = input.float(0.3, 'Min Gap Size (x ATR)', minval = 0, step = 0.05, group = grpZone, tooltip = 'Ignores tiny gaps, measured against chart-timeframe ATR.')
maxShow = input.int(3, 'Max Zones Shown', minval = 1, maxval = 10, group = grpZone, tooltip = 'Hard cap on zones drawn at once. The nearest ones win.')
mitMode = input.string('Close Through', 'Mitigated When', options = ['Close Through', 'Wick Through', '50% (CE) Touch'], group = grpZone, tooltip = 'Close Through = a candle closes beyond the far side of the gap. Wick Through = any wick trades beyond the far side. 50% (CE) Touch = price reaches the midpoint. Tapping or sitting inside the zone does NOT remove it under the first two options. Mitigated zones disappear completely.')
extendBars = input.int(10, 'Extend Right (bars)', minval = 0, group = grpZone)
memory = input.int(80, 'Memory (zones)', minval = 10, maxval = 300, group = grpZone, tooltip = 'How many gaps are remembered in total. Oldest are dropped first.')
showLabel = input.bool(true, 'Zone Label', group = grpZone, inline = 'lbl')
labelClr = input.color(color.new(#2e2e2e, 0), '', group = grpZone, inline = 'lbl')
labelSize = input.string('Small', '', options = ['Tiny', 'Small', 'Normal', 'Large'], group = grpZone, inline = 'lbl')
grpAlert = 'Alerts'
alertNear = input.bool(true, 'Alert When Price Approaches a Zone', group = grpAlert, tooltip = 'Create the alert with condition "Any alert() function call".')
alertTap = input.bool(true, 'Alert on First Tap of a Zone', group = grpAlert, tooltip = 'Fires once per zone, the first time price touches it. Message includes the zone timeframe and direction.')
//#endregion
//#region [TYPES + HELPERS]
type Poi
float top
float bot
int t
bool bull
string tf
bool mit = false
bool near = false
bool tapped = false
bool shown = false
allowBull = biasFilter != 'Bearish'
allowBear = biasFilter != 'Bullish'
tfLabel(string tf) =>
float mins = str.tonumber(tf)
string result = tf
if not na(mins)
if mins >= 60 and mins % 60 == 0
result := str.tostring(int(mins / 60)) + 'H'
else
result := str.tostring(int(mins)) + 'm'
else if tf == 'D' or tf == '1D'
result := '1D'
else if tf == 'W' or tf == '1W'
result := '1W'
else if tf == 'M' or tf == '1M'
result := '1M'
result
textSize(string s) =>
switch s
'Tiny' => size.tiny
'Small' => size.small
'Normal' => size.normal
=> size.large
// Auto timeframe per slot, keyed on the chart timeframe. 'x' = chart timeframe not covered (use custom).
autoSlot(int slot) =>
string p = timeframe.period
string r = 'x'
if p == '1' or p == '5'
r := slot == 1 ? '15' : slot == 2 ? '60' : '240'
else if p == '3'
r := slot == 1 ? '30' : slot == 2 ? '60' : '240'
else if p == '15' or p == '30'
r := slot == 1 ? '60' : slot == 2 ? '240' : '1D'
else if p == '60'
r := slot == 1 ? '240' : slot == 2 ? '1D' : '1W'
else if p == '240'
r := slot == 1 ? '1D' : slot == 2 ? '1W' : ''
r
// Runs inside request.security on the HTF. Reads only the last three CLOSED HTF candles,
// so the result is stable until the next HTF candle closes (no repainting).
f_fvgScan() =>
float h3 = high[1]
float l3 = low[1]
float h1 = high[3]
float l1 = low[3]
bool bull = l3 > h1
bool bear = h3 < l1
[bull, bear, bull ? l3 : l1, bull ? h1 : h3, time[2], time[1]]
//#endregion
//#region [DATA]
string a1 = autoSlot(1)
string a2 = autoSlot(2)
string a3 = autoSlot(3)
string tf1 = autoTf and a1 != 'x' ? a1 : cTf1
string tf2 = autoTf and a2 != 'x' ? a2 : cTf2
string tf3 = autoTf and a3 != 'x' ? a3 : cTf3
// A slot is only active when it has a timeframe that is higher than the chart timeframe
bool ok1 = use1 and tf1 != '' and timeframe.in_seconds(tf1) > timeframe.in_seconds()
bool ok2 = use2 and tf2 != '' and timeframe.in_seconds(tf2) > timeframe.in_seconds()
bool ok3 = use3 and tf3 != '' and timeframe.in_seconds(tf3) > timeframe.in_seconds()
string tf1s = ok1 ? tf1 : timeframe.period
string tf2s = ok2 ? tf2 : timeframe.period
string tf3s = ok3 ? tf3 : timeframe.period
[p1Bull, p1Bear, p1Top, p1Bot, p1Mid, p1New] = request.security(syminfo.tickerid, tf1s, f_fvgScan())
[p2Bull, p2Bear, p2Top, p2Bot, p2Mid, p2New] = request.security(syminfo.tickerid, tf2s, f_fvgScan())
[p3Bull, p3Bear, p3Top, p3Bot, p3Mid, p3New] = request.security(syminfo.tickerid, tf3s, f_fvgScan())
var array<Poi> poiArr = array.new<Poi>()
var array<box> poiBoxes = array.new<box>()
var array<label> poiLbls = array.new<label>()
var table infoTbl = table.new(position.top_right, 1, 1)
float atr = ta.atr(atrLen)
//#endregion
//#region [LOGIC]
poiAllowed(Poi p) =>
bool allowed = p.bull ? allowBull : allowBear
if allowed and sideOnly
allowed := p.bull ? close >= p.bot : close <= p.top
allowed
// Distance from this bar's range to the zone. 0 = the bar is touching / inside the zone.
poiDist(Poi p) =>
low > p.top ? low - p.top : high < p.bot ? p.bot - high : 0.0
addPoi(array<Poi> arr, bool bull, float top, float bot, int t, string tf) =>
if (top - bot) >= minSize * atr
arr.unshift(Poi.new(top = top, bot = bot, t = t, bull = bull, tf = tf))
while arr.size() > memory
arr.pop()
true
// Mitigation, approach alerts and first-tap alerts. Runs on confirmed bars only.
updatePois(array<Poi> arr) =>
if barstate.isconfirmed and arr.size() > 0 and not na(atr)
for p in arr
if not p.mit
float mid = (p.top + p.bot) / 2
bool m = false
if mitMode == 'Wick Through'
m := p.bull ? low < p.bot : high > p.top
else if mitMode == '50% (CE) Touch'
m := p.bull ? low <= mid : high >= mid
else
m := p.bull ? close < p.bot : close > p.top
if m
p.mit := true
p.near := false
p.shown := false
else
float d = poiDist(p)
bool isNear = d <= prox * atr
// hysteresis: appears inside 'prox', only disappears beyond 'exitProx'
p.shown := p.shown ? d <= exitProx * atr : isNear
bool touch = d == 0
string dir = p.bull ? 'Bullish' : 'Bearish'
if poiAllowed(p)
if alertNear and isNear and not p.near
alert(dir + ' ' + tfLabel(p.tf) + ' FVG approaching | ' + syminfo.ticker + ' @ ' + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if alertTap and touch and not p.tapped
alert(dir + ' ' + tfLabel(p.tf) + ' FVG TAP | ' + syminfo.ticker + ' @ ' + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
p.near := isNear
if touch
p.tapped := true
true
// New gaps are stored once the HTF candle that completes them has closed.
if barstate.isconfirmed
if ok1 and p1New != p1New[1] and (p1Bull or p1Bear)
addPoi(poiArr, p1Bull, p1Top, p1Bot, p1Mid, tf1s)
if ok2 and p2New != p2New[1] and (p2Bull or p2Bear)
addPoi(poiArr, p2Bull, p2Top, p2Bot, p2Mid, tf2s)
if ok3 and p3New != p3New[1] and (p3Bull or p3Bear)
addPoi(poiArr, p3Bull, p3Top, p3Bot, p3Mid, tf3s)
updatePois(poiArr)
//#endregion
//#region [DRAW]
// Last bar only: wipe and redraw just the nearest few zones that pass every filter.
drawPois(array<Poi> arr, array<box> boxes, array<label> lbls) =>
for bx in boxes
box.delete(bx)
boxes.clear()
for lb in lbls
label.delete(lb)
lbls.clear()
if arr.size() > 0 and not na(atr)
array<int> idx = array.new<int>()
array<float> dist = array.new<float>()
for i = 0 to arr.size() - 1
Poi c = arr.get(i)
if not c.mit and poiAllowed(c)
float d = poiDist(c)
if c.shown or d <= prox * atr
idx.push(i)
dist.push(d)
int shown = 0
int barMs = timeframe.in_seconds() * 1000
int rightT = time + extendBars * barMs
while idx.size() > 0 and shown < maxShow
int mi = dist.indexof(dist.min())
Poi p = arr.get(idx.get(mi))
bool inside = dist.get(mi) == 0
color base = p.bull ? bullClr : bearClr
color fill = inside ? color.new(base, math.max(color.t(base) - 8, 0)) : base
boxes.push(box.new(p.t, p.top, rightT, p.bot, xloc = xloc.bar_time, border_color = inside ? color.new(base, 60) : na, bgcolor = fill))
if showLabel
string txt = tfLabel(p.tf) + (p.bull ? ' Bull FVG' : ' Bear FVG') + (inside ? ' (in)' : '')
lbls.push(label.new(rightT, (p.top + p.bot) / 2, txt, xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_left, color = na, textcolor = labelClr, size = textSize(labelSize), text_font_family = font.family_default))
idx.remove(mi)
dist.remove(mi)
shown += 1
true
if barstate.islast
drawPois(poiArr, poiBoxes, poiLbls)
if showTable
array<string> act = array.new<string>()
if ok1
act.push(tfLabel(tf1))
if ok2
act.push(tfLabel(tf2))
if ok3
act.push(tfLabel(tf3))
string tblTxt = 'FVG TFs: ' + (act.size() > 0 ? array.join(act, ' | ') : 'none')
table.cell(infoTbl, 0, 0, tblTxt, text_color = labelClr, text_size = size.small, bgcolor = na)
//#endregion