本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MTF Rejection Blocks & FVGs", shorttitle="MTF RB/FVG", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
// ============================================================
// INPUTS
// ============================================================
gGen = "General"
extBars = input.int(5, "Extension from current bar", minval=0, maxval=100, group=gGen)
maxPerSide = input.int(3, "Maximum RBs per timeframe and side", minval=1, maxval=25, group=gGen,
tooltip="Oldest zones are removed first once this limit is reached.")
maxDist = input.float(5.0, "Max distance from price (x zone-timeframe ATR)", minval=0.0, step=0.5, group=gGen,
tooltip="Zones further than this from the current price are hidden, and reappear when price comes back. Set to 0 to turn the filter off.")
maxAge = input.int(50, "Max zone age (candles of its own timeframe)", minval=0, maxval=2000, group=gGen,
tooltip="Zones older than this are deleted. Set to 0 to keep zones until they are invalidated.")
barsCalc = input.int(2500, "Bars to calculate per timeframe", minval=50, maxval=20000, group=gGen,
tooltip="How far back each timeframe is scanned for zones, counted in bars of that timeframe.")
gapMem = input.int(100, "Internal gap memory per timeframe and side", minval=5, maxval=500, group=gGen,
tooltip="How many recent unfilled FVGs are remembered per timeframe and side. They are used to tag a rejection block as 'RB / FVG' when the two overlap.")
gTF = "Timeframes"
show1 = input.bool(true, "1min RB", group=gTF)
show2 = input.bool(false, "2min RB", group=gTF)
show3 = input.bool(false, "3min RB", group=gTF)
show4 = input.bool(false, "4min RB", group=gTF)
show5 = input.bool(true, "5min RB", group=gTF)
show15 = input.bool(true, "15min RB", group=gTF)
show30 = input.bool(true, "30min RB", group=gTF)
show60 = input.bool(true, "1hr RB", group=gTF)
show240 = input.bool(false, "4hr RB", group=gTF)
showD = input.bool(false, "1D RB", group=gTF)
gRB = "Rejection Blocks"
showRBs = input.bool(true, "Show RBs", group=gRB)
showBorder = input.bool(false, "Show RB border", group=gRB)
invCloseCE = input.bool(true, "Invalidate RB on close beyond CE", group=gRB)
invTouchCE = input.bool(true, "Invalidate RB on CE touch", group=gRB,
tooltip="CE = consequent encroachment, the 50% line of the zone. If both invalidation boxes are off, a zone is removed when a candle closes beyond its far edge.")
showOnHTF = input.bool(true, "Show RB on HTF chart", group=gRB,
tooltip="When off, zones are hidden if their timeframe is the same as the chart you're looking at.")
gTrig = "Entry triggers"
showLTFTrig = input.bool(false, "Show LTF entry triggers", group=gTrig)
delLTF = input.bool(false, "Delete tapped LTF entry triggers", group=gTrig)
showHTFTrig = input.bool(false, "Show HTF entry triggers", group=gTrig)
onlyHTFTrig = input.bool(false, "Show only HTF entry triggers", group=gTrig)
show1Trig = input.bool(true, "1min entry triggers", group=gTrig,
tooltip="An entry trigger is an RB or FVG forming on the candles of your current chart inside a higher-timeframe zone, in the same direction. On a 1 minute chart these are the 1min triggers, on a 2 to 14 minute chart they are LTF triggers, and on a 15 minute or higher chart they are HTF triggers.")
gInv1 = "Invalidation 1min entry triggers"
del1 = input.bool(false, "Delete tapped 1min entry triggers", group=gInv1)
inv1Touch = input.bool(false, "Invalidate 1min entry trigger on CE touch", group=gInv1)
inv1Close = input.bool(true, "Invalidate 1min entry trigger on close beyond", group=gInv1)
inv1CE = input.bool(true, "Invalidate 1min entry trigger on close beyond CE", group=gInv1)
gOpen = "Opening prices"
showOpens = input.bool(false, "Show Opening Prices", group=gOpen)
opensDays = input.int(0, "Opens Lookback (days)", minval=0, maxval=30, group=gOpen)
opensTz = input.string("America/New_York", "Timezone", group=gOpen)
on1 = input.bool(true, "", group=gOpen, inline="o1")
nm1 = input.string("MIDNIGHT", "", group=gOpen, inline="o1")
ss1 = input.session("0000-0001", "", group=gOpen, inline="o1")
oc1 = input.color(#ffffff, "", group=gOpen, inline="o1")
on2 = input.bool(true, "", group=gOpen, inline="o2")
nm2 = input.string("9:30 AM", "", group=gOpen, inline="o2")
ss2 = input.session("0930-0931", "", group=gOpen, inline="o2")
oc2 = input.color(#ffffff, "", group=gOpen, inline="o2")
on3 = input.bool(true, "", group=gOpen, inline="o3")
nm3 = input.string("10:00 AM", "", group=gOpen, inline="o3")
ss3 = input.session("1000-1001", "", group=gOpen, inline="o3")
oc3 = input.color(#fdd835, "", group=gOpen, inline="o3")
on4 = input.bool(false, "", group=gOpen, inline="o4")
nm4 = input.string("14:00 PM", "", group=gOpen, inline="o4")
ss4 = input.session("1400-1401", "", group=gOpen, inline="o4")
oc4 = input.color(#ffffff, "", group=gOpen, inline="o4")
on5 = input.bool(false, "", group=gOpen, inline="o5")
nm5 = input.string("18:00 PM", "", group=gOpen, inline="o5")
ss5 = input.session("1800-1801", "", group=gOpen, inline="o5")
oc5 = input.color(#ffffff, "", group=gOpen, inline="o5")
opensStyle = input.string("Dotted", "Line Style", options=["Solid", "Dashed", "Dotted"], group=gOpen, inline="ls")
opensW = input.int(2, "", minval=1, maxval=5, group=gOpen, inline="ls")
gCol = "RB colors by timeframe"
col1 = input.color(#4caf50, "RB 1min", group=gCol)
col5 = input.color(#00bcd4, "RB 5min", group=gCol)
col15 = input.color(#ff9800, "RB 15min", group=gCol)
col30 = input.color(#e040fb, "RB 30min", group=gCol)
col60 = input.color(#fdd835, "RB 1hr", group=gCol)
col240 = input.color(#2962ff, "RB 4hr", group=gCol)
colD = input.color(#f48fb1, "RB 1D", group=gCol)
gFvg = "Detection filters"
showFVG = input.bool(true, "Show Fair Value Gaps", group=gFvg)
minSizeATR = input.float(0.25, "Min zone size (x HTF ATR)", minval=0.0, step=0.05, group=gFvg,
tooltip="Ignores small gaps/wicks. Raise this to see fewer zones.")
maxSizeATR = input.float(1.0, "Max zone size (x HTF ATR)", minval=0.2, step=0.1, group=gFvg,
tooltip="Skips zones taller than this. Combined RB / FVG zones may be up to 1.5x this size.")
minWickPct = input.float(50.0, "Rejection wick min % of candle", minval=10.0, maxval=95.0, step=5.0, group=gFvg,
tooltip="How much of the candle's range the wick must make up to count as a rejection block.")
gStyle = "Style"
showMid = input.bool(true, "Dashed midline (CE)", group=gStyle)
fillAlpha = input.int(80, "Fill transparency", minval=0, maxval=100, group=gStyle)
alertNew = input.bool(false, "Alert on new zones and entry triggers", group=gStyle,
tooltip="After enabling, create an alert on this indicator and choose 'Any alert() function call'.")
// ============================================================
// TYPES
// ============================================================
type Zone
box bx
line mid
label lbl
bool bull
float top
float bot
color col
bool hasRB
bool hasFVG
int tfIdx
int tfSec
string tfName
string trig
float htfAtr
int born
type Gap
float top
float bot
bool bull
int tfIdx
type Trig
box bx
label arr
bool bull
float top
float bot
int cls
var array<Zone> zones = array.new<Zone>()
var array<Gap> gaps = array.new<Gap>()
var array<Trig> trigs = array.new<Trig>()
// ============================================================
// GLOBALS
// ============================================================
int chartSec = timeframe.in_seconds()
int endT = time + extBars * chartSec * 1000
int trigCls = chartSec == 60 ? 0 : chartSec < 900 ? 1 : 2
// ============================================================
// HELPERS
// ============================================================
f_style(string s) =>
s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
f_tfName(int s) =>
s < 3600 ? str.tostring(int(s / 60)) + "min" : s < 86400 ? str.tostring(int(s / 3600)) + "hr" : "1D"
f_zoneText(Zone z) =>
string kind = (z.hasRB and z.hasFVG) ? "RB / FVG" : z.hasRB ? "RB" : "FVG"
z.trig != "" ? z.trig + " RB / FVG: " + z.tfName : (z.hasRB and z.hasFVG) ? kind + ": " + z.tfName : kind + " " + z.tfName
f_htf() =>
atr = ta.atr(14)
[high[1], low[1], open[1], close[1], high[3], low[3], time[2], time[1], atr[1]]
f_del(Zone z) =>
box.delete(z.bx)
label.delete(z.lbl)
if not na(z.mid)
line.delete(z.mid)
// Show or hide a zone without deleting it
f_vis(Zone z, bool v) =>
int a = (z.hasRB and z.hasFVG) ? math.max(fillAlpha - 15, 0) : fillAlpha
box.set_bgcolor(z.bx, v ? color.new(z.col, a) : color.new(z.col, 100))
box.set_border_color(z.bx, (v and showBorder) ? z.col : color.new(z.col, 100))
label.set_color(z.lbl, v ? z.col : color.new(z.col, 100))
label.set_textcolor(z.lbl, v ? color.black : color.new(color.black, 100))
if not na(z.mid)
line.set_color(z.mid, v ? z.col : color.new(z.col, 100))
// ---------- Gap memory ----------
f_gapHit(int tfIdx, bool bull, float top, float bot, float tol) =>
bool hit = false
int n = array.size(gaps)
if n > 0
for i = 0 to n - 1
g = array.get(gaps, i)
if g.tfIdx == tfIdx and g.bull == bull and bot <= g.top + tol and top >= g.bot - tol
hit := true
hit
f_gap(int tfIdx, bool bull, float top, float bot) =>
array.push(gaps, Gap.new(top, bot, bull, tfIdx))
int cnt = 0
int n = array.size(gaps)
for i = 0 to n - 1
g = array.get(gaps, i)
if g.tfIdx == tfIdx and g.bull == bull
cnt += 1
if cnt > gapMem
bool done = false
for i = 0 to n - 1
if not done
g = array.get(gaps, i)
if g.tfIdx == tfIdx and g.bull == bull
done := true
array.remove(gaps, i)
// ---------- Cap per timeframe and side ----------
f_cap(int tfIdx, bool bull) =>
int cnt = 0
int n = array.size(zones)
if n > 0
for i = 0 to n - 1
z = array.get(zones, i)
if z.tfIdx == tfIdx and z.bull == bull
cnt += 1
if cnt > maxPerSide
bool done = false
for i = 0 to n - 1
if not done
z = array.get(zones, i)
if z.tfIdx == tfIdx and z.bull == bull
done := true
array.remove(zones, i)
f_del(z)
// ---------- Add or merge a zone ----------
f_add(int tfIdx, int tfSec, string tfName, color col, bool bull, float top, float bot, int startT, bool isRB, float maxH, float atrv) =>
tol = maxH * 0.15
bool merged = false
int n = array.size(zones)
if n > 0
for i = 0 to n - 1
z = array.get(zones, i)
if not merged and z.tfIdx == tfIdx and z.bull == bull and bot <= z.top + tol and top >= z.bot - tol
bool sameKind = (isRB and z.hasRB and not z.hasFVG) or (not isRB and z.hasFVG and not z.hasRB)
cap = sameKind ? maxH : maxH * 1.5
if math.max(z.top, top) - math.min(z.bot, bot) <= cap
merged := true
z.top := math.max(z.top, top)
z.bot := math.min(z.bot, bot)
if isRB
z.hasRB := true
z.hasFVG := z.hasFVG or f_gapHit(tfIdx, bull, top, bot, tol)
else
z.hasFVG := true
box.set_top(z.bx, z.top)
box.set_bottom(z.bx, z.bot)
box.set_left(z.bx, math.min(box.get_left(z.bx), startT))
label.set_y(z.lbl, (z.top + z.bot) / 2)
label.set_text(z.lbl, f_zoneText(z))
if not na(z.mid)
line.set_y1(z.mid, (z.top + z.bot) / 2)
line.set_y2(z.mid, (z.top + z.bot) / 2)
line.set_x1(z.mid, math.min(line.get_x1(z.mid), startT))
if not merged
b = box.new(startT, top, endT, bot, xloc=xloc.bar_time,
border_color=showBorder ? col : color.new(col, 100), bgcolor=color.new(col, fillAlpha))
ln = showMid ? line.new(startT, (top + bot) / 2, endT, (top + bot) / 2, xloc=xloc.bar_time,
color=col, style=line.style_dashed, width=1) : na
lb = label.new(endT, (top + bot) / 2, "", xloc=xloc.bar_time,
style=label.style_label_left, color=col, textcolor=color.black, size=size.small)
nz = Zone.new(b, ln, lb, bull, top, bot, col, isRB, not isRB, tfIdx, tfSec, tfName, "", atrv, time)
if isRB
nz.hasFVG := f_gapHit(tfIdx, bull, top, bot, tol)
label.set_text(lb, f_zoneText(nz))
array.push(zones, nz)
f_cap(tfIdx, bull)
if alertNew
alert(f_zoneText(nz) + (bull ? " (bullish)" : " (bearish)") + " formed on " + syminfo.ticker, alert.freq_once_per_bar)
// ---------- Detection per timeframe ----------
f_run(int idx, string tf, bool en, color col, string name) =>
[h1, l1, o1, c1, h3, l3, t2, t1, a1] = request.security(syminfo.tickerid, tf, f_htf(), lookahead=barmerge.lookahead_on)
int tfSec = timeframe.in_seconds(tf)
bool tfOk = tfSec >= chartSec and (showOnHTF or tfSec != chartSec)
bool newBar = t1 != t1[1]
bool inRange = t1 >= timenow - barsCalc * tfSec * 1000
float minH = minSizeATR * a1
float maxH = maxSizeATR * a1
if en and tfOk and newBar and inRange
bullGap = l1 - h3
bearGap = l3 - h1
if bullGap > 0 and bullGap >= minH and bullGap <= maxH
f_gap(idx, true, l1, h3)
if showFVG
f_add(idx, tfSec, name, col, true, l1, h3, t2, false, maxH, a1)
if bearGap > 0 and bearGap >= minH and bearGap <= maxH
f_gap(idx, false, l3, h1)
if showFVG
f_add(idx, tfSec, name, col, false, l3, h1, t2, false, maxH, a1)
if showRBs
rng = h1 - l1
bodyTop = math.max(o1, c1)
bodyBot = math.min(o1, c1)
lowW = bodyBot - l1
upW = h1 - bodyTop
if rng > 0 and rng >= minH
if lowW >= upW and lowW / rng * 100 >= minWickPct and lowW >= minH and lowW <= maxH
f_add(idx, tfSec, name, col, true, bodyBot, l1, t1, true, maxH, a1)
else if upW > lowW and upW / rng * 100 >= minWickPct and upW >= minH and upW <= maxH
f_add(idx, tfSec, name, col, false, h1, bodyTop, t1, true, maxH, a1)
// ---------- Entry triggers ----------
f_trig(bool bull, float top, float bot, int startT, string nm) =>
bool hit = false
int n = array.size(zones)
if n > 0
for i = 0 to n - 1
z = array.get(zones, i)
if z.bull == bull and z.tfSec > chartSec and bot <= z.top and top >= z.bot
hit := true
z.trig := nm
label.set_text(z.lbl, f_zoneText(z))
if hit
tcol = bull ? #2962ff : #f23645
b = box.new(startT, top, endT, bot, xloc=xloc.bar_time,
border_color=color.new(tcol, 100), bgcolor=color.new(tcol, 80))
a = label.new(time, bull ? low : high, "", xloc=xloc.bar_time,
style=bull ? label.style_arrowup : label.style_arrowdown, color=tcol, size=size.tiny)
array.push(trigs, Trig.new(b, a, bull, top, bot, trigCls))
if array.size(trigs) > 30
old = array.shift(trigs)
box.delete(old.bx)
label.delete(old.arr)
if alertNew
alert(nm + " entry trigger (" + (bull ? "bullish" : "bearish") + ") on " + syminfo.ticker, alert.freq_once_per_bar_close)
// ---------- Opening prices ----------
f_open(bool en, string nm, string sess, color col) =>
var array<line> lns = array.new<line>()
var array<label> lbs = array.new<label>()
var array<int> tms = array.new<int>()
bool inS = not na(time(timeframe.period, sess, opensTz))
bool st = inS and not inS[1]
if en and showOpens
if st and time >= timenow - (opensDays + 2) * 86400000
array.push(lns, line.new(time, open, endT, open, xloc=xloc.bar_time, color=col, style=f_style(opensStyle), width=opensW))
array.push(lbs, label.new(endT, open, nm, xloc=xloc.bar_time, style=label.style_label_left, color=color.new(col, 100), textcolor=col, size=size.small))
array.push(tms, time)
int cut = timestamp(opensTz, year(time, opensTz), month(time, opensTz), dayofmonth(time, opensTz), 0, 0, 0) - opensDays * 86400000
while array.size(tms) > 0 and array.get(tms, 0) < cut
line.delete(array.shift(lns))
label.delete(array.shift(lbs))
array.shift(tms)
int m = array.size(lns)
if m > 0
for i = 0 to m - 1
line.set_x2(array.get(lns, i), endT)
label.set_x(array.get(lbs, i), endT)
// ============================================================
// 1) DETECT NEW ZONES
// ============================================================
f_run(0, "1", show1, col1, "1min")
f_run(1, "2", show2, col1, "2min")
f_run(2, "3", show3, col1, "3min")
f_run(3, "4", show4, col1, "4min")
f_run(4, "5", show5, col5, "5min")
f_run(5, "15", show15, col15, "15min")
f_run(6, "30", show30, col30, "30min")
f_run(7, "60", show60, col60, "1hr")
f_run(8, "240", show240, col240, "4hr")
f_run(9, "D", showD, colD, "1D")
// ============================================================
// 2) INVALIDATE / EXPIRE / SHOW-HIDE ZONES
// ============================================================
int nzz = array.size(zones)
if nzz > 0
for i = nzz - 1 to 0
z = array.get(zones, i)
zm = (z.top + z.bot) / 2
bool mit = false
if invTouchCE or invCloseCE
mit := (invTouchCE and (z.bull ? low <= zm : high >= zm)) or (invCloseCE and (z.bull ? close < zm : close > zm))
else
mit := z.bull ? close < z.bot : close > z.top
bool stale = maxAge > 0 and (time - z.born) / (z.tfSec * 1000) > maxAge
if mit or stale
array.remove(zones, i)
f_del(z)
else
box.set_right(z.bx, endT)
label.set_x(z.lbl, endT)
if not na(z.mid)
line.set_x2(z.mid, endT)
float dist = z.bull ? close - z.top : z.bot - close
bool vis = maxDist <= 0 or dist <= maxDist * z.htfAtr
f_vis(z, vis)
// Remembered gaps are dropped once price closes through them
int ng = array.size(gaps)
if ng > 0
for i = ng - 1 to 0
g = array.get(gaps, i)
if g.bull ? close < g.bot : close > g.top
array.remove(gaps, i)
// ============================================================
// 3) INVALIDATE / UPDATE ENTRY TRIGGERS
// ============================================================
int nt = array.size(trigs)
if nt > 0
for i = nt - 1 to 0
t = array.get(trigs, i)
tm = (t.top + t.bot) / 2
tapped = t.bull ? low <= t.top : high >= t.bot
touchCE = t.bull ? low <= tm : high >= tm
beyond = t.bull ? close < t.bot : close > t.top
beyondCE = t.bull ? close < tm : close > tm
bool inv = false
if t.cls == 0
inv := (del1 and tapped) or (inv1Touch and touchCE) or (inv1Close and beyond) or (inv1CE and beyondCE)
else if t.cls == 1
inv := (delLTF and tapped) or beyond
else
inv := beyond
if inv
array.remove(trigs, i)
box.delete(t.bx)
label.delete(t.arr)
else
box.set_right(t.bx, endT)
// ============================================================
// 4) DETECT NEW ENTRY TRIGGERS (on the chart's own candles)
// ============================================================
atrC = ta.atr(14)
minHC = minSizeATR * atrC
maxHC = maxSizeATR * atrC
rngC = high - low
bodyTopC = math.max(open, close)
bodyBotC = math.min(open, close)
lowWC = bodyBotC - low
upWC = high - bodyTopC
trigName = f_tfName(chartSec)
bool trigOn = barstate.isconfirmed and (trigCls == 0 ? (show1Trig and not onlyHTFTrig) : trigCls == 1 ? (showLTFTrig and not onlyHTFTrig) : (showHTFTrig or onlyHTFTrig))
if trigOn
gUp = low - high[2]
gDn = low[2] - high
if gUp > 0 and gUp >= minHC and gUp <= maxHC
f_trig(true, low, high[2], time[2], trigName)
if gDn > 0 and gDn >= minHC and gDn <= maxHC
f_trig(false, low[2], high, time[2], trigName)
if rngC > 0 and rngC >= minHC
if lowWC >= upWC and lowWC / rngC * 100 >= minWickPct and lowWC >= minHC and lowWC <= maxHC
f_trig(true, bodyBotC, low, time, trigName)
else if upWC > lowWC and upWC / rngC * 100 >= minWickPct and upWC >= minHC and upWC <= maxHC
f_trig(false, high, bodyTopC, time, trigName)
// ============================================================
// 5) OPENING PRICES
// ============================================================
f_open(on1, nm1, ss1, oc1)
f_open(on2, nm2, ss2, oc2)
f_open(on3, nm3, ss3, oc3)
f_open(on4, nm4, ss4, oc4)
f_open(on5, nm5, ss5, oc5)