本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// FVG and Inversion Zones [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("FVG and Inversion Zones [Badr Edge]", shorttitle = "BE FVG", overlay = true, max_boxes_count = 300, max_labels_count = 100)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string G_G = "Gaps"
tfIn = input.timeframe("", "Gap timeframe", group = G_G, tooltip = "Leave on Chart to use the chart's own candles. Pick a higher timeframe to draw that timeframe's gaps on a lower chart. Higher-timeframe gaps appear only after the third candle has closed.")
minAtr = input.float(0.75, "Minimum gap size (x ATR 14)", minval = 0, step = 0.05, group = G_G, tooltip = "Gaps smaller than this share of the average candle range are ignored.")
maxAge = input.int(60, "Stop extending after (bars)", minval = 10, maxval = 2000, group = G_G)
maxKeep = input.int(6, "Open zones kept", minval = 2, maxval = 40, group = G_G)
const string G_V = "Visuals"
showFvg = input.bool(true, "Fair value gaps", group = G_V)
showInv = input.bool(true, "Inversion zones", group = G_V, tooltip = "When a candle closes through the far side of a gap, the gap is redrawn as an inversion zone on the other side.")
showMarks = input.bool(true, "Inversion markers", group = G_V, tooltip = "A small triangle on the candle that inverts a gap. A marker shows what happened. It is not a trade signal.")
const string G_Y = "Style"
colUp = input.color(TEAL, "Gap up / inverted up", inline = "c", group = G_Y)
colDn = input.color(ROSE, "Gap down / inverted down", inline = "c", group = G_Y)
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert when a gap is inverted", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
type Zone
box b
float top
float bot
int dir
bool inv
int born
var array<Zone> zones = array.new<Zone>()
atr = ta.atr(14)
[h1, l1, h3, l3, t1] = request.security(syminfo.tickerid, tfIn, [high[1], low[1], high[3], low[3], time[1]], lookahead = barmerge.lookahead_on)
useHtf = timeframe.in_seconds(tfIn) > timeframe.in_seconds()
newHtf = useHtf and t1 != t1[1]
gUpTop = useHtf ? l1 : low
gUpBot = useHtf ? h3 : high[2]
gDnTop = useHtf ? l3 : low[2]
gDnBot = useHtf ? h1 : high
bool fresh = useHtf ? newHtf : barstate.isconfirmed
bool gapUp = fresh and gUpTop - gUpBot > minAtr * atr
bool gapDn = fresh and gDnTop - gDnBot > minAtr * atr
int left = useHtf ? bar_index : bar_index - 2
newZone(float top, float bot, int dir) =>
color c = dir == 1 ? colUp : colDn
box bx = box.new(left, top, bar_index, bot, border_color = color.new(c, showFvg ? 70 : 100), border_width = 1, bgcolor = color.new(c, showFvg ? 90 : 100))
Zone.new(bx, top, bot, dir, false, bar_index)
if gapUp and not na(atr)
zones.push(newZone(gUpTop, gUpBot, 1))
if gapDn and not na(atr)
zones.push(newZone(gDnTop, gDnBot, -1))
if zones.size() > maxKeep
zones.shift()
bool invUp = false
bool invDn = false
if zones.size() > 0 and barstate.isconfirmed
for i = zones.size() - 1 to 0
Zone z = zones.get(i)
bool drop = bar_index - z.born > maxAge
if not drop and z.born < bar_index
if not z.inv
bool through = z.dir == 1 ? close < z.bot : close > z.top
if through
z.inv := true
z.dir := -z.dir
z.born := bar_index
color c = z.dir == 1 ? colUp : colDn
z.b.set_right(bar_index)
z.b.set_bgcolor(color.new(c, showInv ? 82 : 100))
z.b.set_border_color(color.new(c, showInv ? 35 : 100))
z.b.set_border_style(line.style_dashed)
if z.dir == 1
invUp := true
else
invDn := true
else
z.b.set_right(bar_index)
else
bool failed = z.dir == 1 ? close < z.bot : close > z.top
if failed
drop := true
else
z.b.set_right(bar_index)
if drop
zones.remove(i)
plotshape(showMarks and invUp, "Gap inverted upward", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and invDn, "Gap inverted downward", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
plot(gapUp ? gUpTop : na, "New gap up: top", display = display.data_window)
plot(gapUp ? gUpBot : na, "New gap up: bottom", display = display.data_window)
plot(gapDn ? gDnTop : na, "New gap down: top", display = display.data_window)
plot(gapDn ? gDnBot : na, "New gap down: bottom", display = display.data_window)
plot(zones.size(), "Open zones", display = display.data_window)
if useAlerts and invUp
alert(syminfo.ticker + " closed up through a fair value gap (inversion)", alert.freq_once_per_bar_close)
if useAlerts and invDn
alert(syminfo.ticker + " closed down through a fair value gap (inversion)", alert.freq_once_per_bar_close)
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)