FVG MTF - Open Gaps with 50% Tracking

ThiagoCostaKF · study · 91 行 · 点赞 15 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Version 1 · 27/10/2026 · public release (English UI, timeframe never below the chart, editable touch/50% colors)
indicator("FVG MTF - Open Gaps with 50% Tracking", shorttitle="FVG MTF", overlay=true, max_boxes_count=100, max_lines_count=100)
 
// Fair Value Gaps from another timeframe, drawn on the current chart.
// Bullish: low of candle 3 above high of candle 1 · Bearish: high of candle 3 below low of candle 1.
// Only confirmed gaps (higher-timeframe candle closed) are drawn, so they do not repaint.
// Shows the N most recent OPEN gaps per side · touched = lighter box · 50% reached = midline highlighted
// Filled (close beyond the whole gap) = removed.
tfIn    = input.timeframe("5", "FVG timeframe")
nShow   = input.int(3, "Gaps per side", minval=1, maxval=20)
colUp   = input.color(color.green, "Bullish FVG")
colDn   = input.color(color.red,   "Bearish FVG")
colHalf = input.color(color.yellow, "Midline after 50% reached")
extR    = input.bool(false, "Extend right")
extL    = input.bool(false, "Extend left")
fvgExt  = extR and extL ? extend.both : extR ? extend.right : extL ? extend.left : extend.none
 
// never use a timeframe lower than the chart
tf = timeframe.in_seconds(tfIn) < timeframe.in_seconds() ? timeframe.period : tfIn
 
// computed on the FVG timeframe, shifted 1 bar (= last CLOSED bar); with lookahead_on this does not repaint
fvgCalc() =>
    bool bull = low[1] > high[3]
    bool bear = high[1] < low[3]
    [bull, low[1], high[3], bear, low[3], high[1], time[3]]
 
[bullN, bTop, bBot, bearN, sTop, sBot, tLeft] = request.security(syminfo.tickerid, tf, fvgCalc(), lookahead = barmerge.lookahead_on)
 
bool newHtf = ta.change(time(tf)) != 0
 
type Fvg
    box   bx
    line  mid
    float top
    float bot
    int   dir
    bool  touched = false
    bool  half    = false
 
var array<Fvg> ups = array.new<Fvg>()
var array<Fvg> dns = array.new<Fvg>()
 
killFvg(Fvg f) =>
    box.delete(f.bx)
    line.delete(f.mid)
 
newFvg(int dir, float top, float bot, int tl) =>
    color c  = dir == 1 ? colUp : colDn
    float md = (top + bot) / 2
    box  b = box.new(tl, top, time, bot, xloc=xloc.bar_time, border_color=color.new(c, 30), bgcolor=color.new(c, 75), extend=fvgExt)
    line m = line.new(tl, md, time, md, xloc=xloc.bar_time, color=color.new(color.gray, 20), style=line.style_dotted, extend=fvgExt)
    Fvg.new(b, m, top, bot, dir)
 
if newHtf and not na(tLeft)
    if bullN
        ups.push(newFvg(1, bTop, bBot, tLeft))
        if ups.size() > nShow
            killFvg(ups.shift())
    if bearN
        dns.push(newFvg(-1, sTop, sBot, tLeft))
        if dns.size() > nShow
            killFvg(dns.shift())
 
updateSide(array<Fvg> arr) =>
    int i = arr.size() - 1
    while i >= 0
        Fvg f = arr.get(i)
        float md = (f.top + f.bot) / 2
        bool filled = f.dir == 1 ? close < f.bot : close > f.top
        if filled
            killFvg(f)
            arr.remove(i)
        else
            color c = f.dir == 1 ? colUp : colDn
            box.set_right(f.bx, time)
            line.set_x2(f.mid, time)
            if not f.touched and (f.dir == 1 ? low <= f.top : high >= f.bot)
                f.touched := true
                box.set_bgcolor(f.bx, color.new(c, 88))
            if not f.half and (f.dir == 1 ? low <= md : high >= md)
                f.half := true
                line.set_color(f.mid, colHalf)
                line.set_style(f.mid, line.style_solid)
        i -= 1
 
updateSide(ups)
updateSide(dns)
 
alertcondition(newHtf and bullN, "Bullish FVG", "New BULLISH FVG on {{ticker}}")
alertcondition(newHtf and bearN, "Bearish FVG", "New BEARISH FVG on {{ticker}}")

← 返回列表