本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Market Strategies - FVG Breakers v2", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500, max_bars_back=5000)
//============================================================
// INPUTS
//============================================================
groupFVG = "FAIR VALUE GAP"
showRegularFVG = input.bool(true, "Show Regular FVGs", group=groupFVG)
showLast = input.int(30, "Show Last", minval=1, maxval=100, group=groupFVG)
fvgThreshold = input.float(0.0, "ATR Threshold", minval=0, maxval=2, step=0.1, group=groupFVG)
mitigationMethod = input.string("Close", "Mitigation Method", options=["Close", "Wick", "Avg"], group=groupFVG)
hideOverlap = input.bool(true, "Hide Overlap", group=groupFVG)
extendFVG = input.bool(true, "Extend Zones", group=groupFVG)
maxTracked = input.int(300, "Maximum Tracked Per Direction", minval=20, maxval=1000, group=groupFVG)
groupBreaker = "BREAKER FVG"
showBreakers = input.bool(true, "Show Breaker FVGs", group=groupBreaker)
showMidline = input.bool(true, "Show Dashed Midpoint", group=groupBreaker)
showLabels = input.bool(true, "Show Breaker Labels", group=groupBreaker)
groupStyle = "STYLE"
bullFVGColor = input.color(color.new(#089981, 80), "Bullish FVG", group=groupStyle)
bearFVGColor = input.color(color.new(#f23645, 80), "Bearish FVG", group=groupStyle)
bullBreakerColor = input.color(color.new(#00CFFF, 65), "Bullish Breaker", group=groupStyle)
bearBreakerColor = input.color(color.new(#E78282, 80), "Bearish Breaker", group=groupStyle)
//============================================================
// SMC FVG RECORD
//============================================================
type FVG
float top = na
float btm = na
int loc = na
bool isbb = false
int bbloc = na
var array<FVG> blFVG = array.new<FVG>()
var array<FVG> brFVG = array.new<FVG>()
// Rebuilt on the last bar, like SMC.
var array<box> drawnBoxes = array.new<box>()
var array<line> drawnLines = array.new<line>()
var array<label> drawnLabels = array.new<label>()
//============================================================
// ALERT FLAGS
//============================================================
bool newBullFVG = false
bool newBearFVG = false
bool bullBreakerSignal = false
bool bearBreakerSignal = false
bool bullMitigationSignal = false
bool bearMitigationSignal = false
//============================================================
// SMC OVERLAP TEST
// Matches the original four comparisons.
//============================================================
overlapsSMC(FVG stuff, FVG current) =>
(stuff.btm > current.btm and stuff.btm < current.top) or (stuff.top < current.top and stuff.btm > current.btm) or (stuff.top > current.top and stuff.btm < current.btm) or (stuff.top < current.top and stuff.top > current.btm)
//============================================================
// SMC OVERLAP FILTER
//============================================================
filterOverlap(array<FVG> bull, array<FVG> bear) =>
if bull.size() > 1
for i = bull.size() - 1 to 1
stuff = bull.get(i)
current = bull.get(0)
if overlapsSMC(stuff, current)
bull.remove(i)
if bear.size() > 1
for i = bear.size() - 1 to 1
stuff = bear.get(i)
current = bear.get(0)
if overlapsSMC(stuff, current)
bear.remove(i)
if bull.size() > 0 and bear.size() > 0
for i = bull.size() - 1 to 0
stuff = bull.get(i)
current = bear.get(0)
if overlapsSMC(stuff, current)
bull.remove(i)
if bull.size() > 0 and bear.size() > 0
for i = bear.size() - 1 to 0
stuff = bear.get(i)
current = bull.get(0)
if overlapsSMC(stuff, current)
bear.remove(i)
//============================================================
// SMC FVG DETECTION
//============================================================
atr200 = ta.atr(200)
float blth = low[1] + atr200[1] * fvgThreshold
float brth = high[1] - atr200[1] * fvgThreshold
bool cc = timeframe.change()
bool upfvg = low > high[2] and cc and close[1] > blth
bool dnfvg = low[2] > high and cc and close[1] < brth
//============================================================
// REGISTER FVG ONE BAR LATER
// Original SMC timing and coordinates.
//============================================================
if upfvg[1]
blFVG.unshift(FVG.new(low[1], high[3], time[3], false, na))
newBullFVG := true
if dnfvg[1]
brFVG.unshift(FVG.new(low[3], high[1], time[3], false, na))
newBearFVG := true
//============================================================
// BULLISH ORIGINAL FVG PROCESSING
// Converts to bearish breaker.
//============================================================
if blFVG.size() > 0
for i = blFVG.size() - 1 to 0
fvg = blFVG.get(i)
if not fvg.isbb
bool broken = switch mitigationMethod
"Close" => math.min(close, open) < fvg.btm
"Wick" => low < fvg.btm
=> low < math.avg(fvg.top, fvg.btm)
if broken
fvg.isbb := true
fvg.bbloc := time
bearBreakerSignal := true
bullMitigationSignal := true
else
bool invalid = switch mitigationMethod
"Close" => math.max(close, open) > fvg.top
"Wick" => high > fvg.top
=> high > math.avg(fvg.top, fvg.btm)
if invalid
blFVG.remove(i)
//============================================================
// BEARISH ORIGINAL FVG PROCESSING
// Converts to bullish breaker.
//============================================================
if brFVG.size() > 0
for i = brFVG.size() - 1 to 0
fvg = brFVG.get(i)
if not fvg.isbb
bool broken = switch mitigationMethod
"Close" => math.max(close, open) > fvg.top
"Wick" => high > fvg.top
=> high > math.avg(fvg.top, fvg.btm)
if broken
fvg.isbb := true
fvg.bbloc := time
bullBreakerSignal := true
bearMitigationSignal := true
else
bool invalid = switch mitigationMethod
"Close" => math.min(close, open) < fvg.btm
"Wick" => low < fvg.btm
=> low < math.avg(fvg.top, fvg.btm)
if invalid
brFVG.remove(i)
//============================================================
// SMC OVERLAP FILTER - AFTER MITIGATION
//============================================================
if hideOverlap
filterOverlap(blFVG, brFVG)
//============================================================
// MEMORY CONTROL
// Only limits extremely old tracked records.
//============================================================
while blFVG.size() > maxTracked
blFVG.pop()
while brFVG.size() > maxTracked
brFVG.pop()
//============================================================
// DRAW ONE SMC FVG RECORD
//============================================================
drawFVG(FVG fvg, bool originalBull) =>
bool breaker = fvg.isbb
bool breakerBull = not originalBull
if (breaker and showBreakers) or (not breaker and showRegularFVG)
color originalColor = originalBull ? bullFVGColor : bearFVGColor
color breakerColor = breakerBull ? bullBreakerColor : bearBreakerColor
float midpoint = math.avg(fvg.top, fvg.btm)
if breaker
// Original FVG segment.
box originalBox = box.new(left=fvg.loc, top=fvg.top, right=fvg.bbloc, bottom=fvg.btm, xloc=xloc.bar_time, border_color=na, bgcolor=originalColor)
drawnBoxes.push(originalBox)
// Converted breaker segment.
box breakerBox = box.new(left=fvg.bbloc, top=fvg.top, right=time, bottom=fvg.btm, xloc=xloc.bar_time, border_color=color.new(breakerColor, 0), border_width=1, bgcolor=breakerColor, extend=extendFVG ? extend.right : extend.none)
drawnBoxes.push(breakerBox)
if showMidline
line beforeMid = line.new(x1=fvg.loc, y1=midpoint, x2=fvg.bbloc, y2=midpoint, xloc=xloc.bar_time, color=color.new(originalColor, 0))
drawnLines.push(beforeMid)
line afterMid = line.new(x1=fvg.bbloc, y1=midpoint, x2=time, y2=midpoint, xloc=xloc.bar_time, color=color.new(breakerColor, 0), style=line.style_dashed, extend=extendFVG ? extend.right : extend.none)
drawnLines.push(afterMid)
if showLabels
label tag = label.new(x=fvg.bbloc, y=breakerBull ? fvg.btm : fvg.top, text=breakerBull ? "BULL BRK FVG" : "BEAR BRK FVG", xloc=xloc.bar_time, style=breakerBull ? label.style_label_up : label.style_label_down, color=breakerColor, textcolor=color.white, size=size.tiny)
drawnLabels.push(tag)
else
box regularBox = box.new(left=fvg.loc, top=fvg.top, right=time, bottom=fvg.btm, xloc=xloc.bar_time, border_color=na, bgcolor=originalColor, extend=extendFVG ? extend.right : extend.none)
drawnBoxes.push(regularBox)
//============================================================
// LAST BAR DRAWING
// Uses original SMC Show Last selection.
//============================================================
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 lb in drawnLabels
label.delete(lb)
drawnLabels.clear()
if blFVG.size() > 0
for i = 0 to math.min(showLast - 1, blFVG.size() - 1)
fvg = blFVG.get(i)
drawFVG(fvg, true)
if brFVG.size() > 0
for i = 0 to math.min(showLast - 1, brFVG.size() - 1)
fvg = brFVG.get(i)
drawFVG(fvg, false)
//============================================================
// FVG ALERTS
//============================================================
alertcondition(newBullFVG and barstate.isconfirmed, "Bullish FVG", "BULL FVG | {{ticker}} | {{interval}}")
alertcondition(newBearFVG and barstate.isconfirmed, "Bearish FVG", "BEAR FVG | {{ticker}} | {{interval}}")
alertcondition(bullMitigationSignal and barstate.isconfirmed, "Bullish FVG Mitigation", "BULL FVG MITIGATED | {{ticker}}")
alertcondition(bearMitigationSignal and barstate.isconfirmed, "Bearish FVG Mitigation", "BEAR FVG MITIGATED | {{ticker}}")
//============================================================
// BREAKER ALERTS
//============================================================
alertcondition(bullBreakerSignal and barstate.isconfirmed, "BULL BREAKER FVG", "BULL BRK FVG | {{ticker}} | {{interval}} | {{close}}")
alertcondition(bearBreakerSignal and barstate.isconfirmed, "BEAR BREAKER FVG", "BEAR BRK FVG | {{ticker}} | {{interval}} | {{close}}")
alertcondition((bullBreakerSignal or bearBreakerSignal) and barstate.isconfirmed, "ANY BREAKER FVG", "BREAKER FVG | {{ticker}} | {{interval}} | {{close}}")