Percent Gainer - Multi-Timeframe FVG Layers

Percent_Gainer · study · 170 行 · 点赞 12 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Straight Line Trades Inc. & Percent Gainers
 
//@version=6
indicator("Percent Gainer - Multi-Timeframe FVG Layers", shorttitle="PG FVG MTF", overlay=true, max_lines_count=500, max_boxes_count=500)
 
//────────────────────────────── Layer inputs ──────────────────────────────
groupMulti = "Multi-TF FVG Layers"
tfTip = "Timeframe this layer's gaps are detected on. Blank = chart timeframe.\n\nA layer only draws when its timeframe is the SAME as or HIGHER than the chart's:\n• 4H layer on a 1H chart = shown (higher timeframe gaps on a lower chart)\n• 5-minute layer on a 1H chart = hidden\n\nWhy: one 1H candle contains twelve 5-minute candles, and a 1H chart cannot read each of them, so most 5-minute gaps would be missed and the ones shown would be incomplete. Rather than show partial data, the layer is skipped.\n\nThe layer reappears automatically when you switch to a chart at or below its timeframe."
 
en4H  = input.bool(true,  "Show FVG 1", tooltip=tfTip,               group=groupMulti)
ft4H  = input.bool(false, "filter by time", group=groupMulti)
fs4H  = input.session("0800-1000", "  time window", group=groupMulti)
 
tf4H  = input.timeframe("240", "  Timeframe",        group=groupMulti)
col4H = input.color(color.new(color.yellow, 90), "  Color", group=groupMulti)
max4H = input.int(50, "    show last N gaps", minval=1, maxval=150, group=groupMulti)
 
en1H  = input.bool(true,  "Show FVG 2", tooltip=tfTip,              group=groupMulti)
ft1H  = input.bool(false, "filter by time", group=groupMulti)
fs1H  = input.session("0800-1000", "  time window", group=groupMulti)
 
tf1H  = input.timeframe("60",  "  Timeframe",        group=groupMulti)
col1H = input.color(color.new(color.red, 90),  "  Color", group=groupMulti)
max1H = input.int(50, "  show last N gaps", minval=1, maxval=150, group=groupMulti)
 
en15  = input.bool(true,  "Show FVG 3", tooltip=tfTip,             group=groupMulti)
ft15  = input.bool(false, "filter by time", group=groupMulti)
fs15  = input.session("0800-1000", "  time window", group=groupMulti)
 
tf15  = input.timeframe("15",  "  Timeframe",       group=groupMulti)
col15 = input.color(color.new(color.blue, 90), "  Color", group=groupMulti)
max15 = input.int(150, "  show last N gaps", minval=1, maxval=150, group=groupMulti)
 
// Shared timezone for all three time-window filters
tzFilter = input.string("America/New_York", "  Time window timezone", group=groupMulti, tooltip="Eastern time (auto EST/EDT). Use America/Chicago for CT, or Exchange for the symbol's exchange time.")
 
//────────────────────────────── Mitigation & display ──────────────────────────────
groupMit = "Mitigation & Display"
 
mitTrigger = input.string("Close", "  Mitigate on",
             options=["Wick", "Close"], group=groupMit,
             tooltip="Wick = the HTF candle's high/low reaching the level. Close = the HTF candle closing at/through it.")
mitDepth   = input.string("Full (distal edge)", "  Mitigation threshold",
             options=["Tap (proximal edge)", "Midpoint (50%)", "Full (distal edge)"], group=groupMit,
             tooltip="How far price must travel into the gap before it's considered mitigated and removed.")
showMid    = input.bool(true, "Show FVG centerline", group=groupMit)
colMid     = input.color(color.new(color.gray, 40), "  Centerline color", group=groupMit)
keepMitigated = input.bool(true,  "Keep mitigated gaps (show history)", group=groupMit, tooltip="On: a gap that closes through stays on the chart as a record of the level, frozen at its mitigation point, instead of being deleted. Off: original behaviour (removed on fill).")
markMit       = input.bool(true,  "Mark mitigated gaps (solid border)", group=groupMit, tooltip="Give filled gaps a solid border so they read differently from live (dashed) gaps at a glance.")
 
useWick = mitTrigger == "Wick"
 
//────────────────────────────── Zone type ──────────────────────────────
type fvgZone
    box   bx
    line  ml       // centerline (na when hidden)
    float top
    float bot
    float mid
    int   dir      // 1 = bullish gap (support), -1 = bearish gap (resistance)
    bool  mitigated = false   // true once price has closed/wicked through to the chosen depth
//────────────────────────────── State (separate arrays per layer) ──────────────────────────────
var array<fvgZone> zones4H = array.new<fvgZone>()
var array<fvgZone> zones1H = array.new<fvgZone>()
var array<fvgZone> zones15 = array.new<fvgZone>()
 
//────────────────────────────── Helpers ──────────────────────────────
// The price level that must be reached for mitigation, per threshold + direction.
f_mitLevel(z) =>
    prox = z.dir == 1 ? z.top : z.bot   // edge nearest returning price
    dist = z.dir == 1 ? z.bot : z.top   // far edge (full fill)
    switch mitDepth
        "Tap (proximal edge)" => prox
        "Midpoint (50%)"      => z.mid
        => dist
 
// Has the completed HTF candle (hi/lo/cl) mitigated this zone to the chosen depth?
f_isMitigated(z, hi, lo, cl) =>
    lvl = f_mitLevel(z)
    z.dir == 1 ? (useWick ? lo <= lvl : cl <= lvl) : (useWick ? hi >= lvl : cl >= lvl)
 
// Delete a zone's drawings.
f_kill(z) =>
    box.delete(z.bx)
    if not na(z.ml)
        line.delete(z.ml)
 
//────────────────────────────── Layer engine ──────────────────────────────
f_layer(tf, enabled, maxCount, boxCol, useTime, sess, _zones) =>
    // Lower-than-chart timeframes are skipped: request.security would only return the last intrabar and miss gaps
    bool tfOk = tf == "" or timeframe.in_seconds(tf) >= timeframe.in_seconds(timeframe.period)
    if enabled and tfOk
        // First bar of a new HTF candle on the current chart
        newTFbar = ta.change(time(tf)) != 0
 
        // In-window flag for creation (per-layer filter, shared timezone).
        // When the filter is off, this is always true.
        // Window is tested on the gap's 3rd candle (read with [1] at creation, exactly like the gap itself)
        bool gapInSess = request.security(syminfo.tickerid, tf, not na(time(timeframe.period, sess, tzFilter)), barmerge.gaps_off, barmerge.lookahead_off)
        inWindow = not useTime or gapInSess[1]
 
        // ── Detection (HTF-native, evaluated on closed values) ──
        top_pattern = request.security(syminfo.tickerid, tf, low[2] <= open[1] and high >= close[1], barmerge.gaps_off, barmerge.lookahead_off)
        bot_pattern = request.security(syminfo.tickerid, tf, high[2] >= open[1] and low  <= close[1], barmerge.gaps_off, barmerge.lookahead_off)
        top_sizeok  = request.security(syminfo.tickerid, tf, (low[2] - high)     > 0, barmerge.gaps_off, barmerge.lookahead_off)
        bot_sizeok  = request.security(syminfo.tickerid, tf, (low     - high[2]) > 0, barmerge.gaps_off, barmerge.lookahead_off)
        top_sig = top_pattern and top_sizeok
        bot_sig = bot_pattern and bot_sizeok
 
        // Gap boundaries (fixed at TF close)
        top_up = request.security(syminfo.tickerid, tf, low[2],  barmerge.gaps_off, barmerge.lookahead_off) // bearish gap upper
        top_lo = request.security(syminfo.tickerid, tf, high,    barmerge.gaps_off, barmerge.lookahead_off) // bearish gap lower
        bot_up = request.security(syminfo.tickerid, tf, low,     barmerge.gaps_off, barmerge.lookahead_off) // bullish gap upper
        bot_lo = request.security(syminfo.tickerid, tf, high[2], barmerge.gaps_off, barmerge.lookahead_off) // bullish gap lower
 
        // ── Mitigation reference: this layer's own H/L/C (non-repaint) ──
        [mHigh, mLow, mClose] = request.security(syminfo.tickerid, tf, [high, low, close], barmerge.gaps_off, barmerge.lookahead_off)
 
        // On each completed HTF candle: mitigate first, then create new gaps
        if newTFbar
            // 1) Mitigation pass on existing zones (uses the just-closed HTF candle)
            if array.size(_zones) > 0
                for i = array.size(_zones) - 1 to 0 by 1
                    z = array.get(_zones, i)
                    if not z.mitigated and f_isMitigated(z, mHigh[1], mLow[1], mClose[1])
                        if keepMitigated
                            z.mitigated := true
                            box.set_right(z.bx, bar_index)   // freeze the gap at its mitigation point
                            if markMit
                                box.set_border_style(z.bx, line.style_solid)
                            if not na(z.ml)
                                line.set_x2(z.ml, bar_index)
                        else
                            f_kill(z)
                            array.remove(_zones, i)
 
            // 2) Creation (a zone born this bar is skipped by the pass above)
            if top_sig[1] and inWindow
                topP = top_up[1]
                botP = top_lo[1]
                midP = math.avg(topP, botP)
                b  = box.new(bar_index, topP, bar_index, botP, bgcolor=boxCol, border_color=color.new(boxCol, 0), border_style=line.style_dashed)
                ml = showMid ? line.new(bar_index, midP, bar_index, midP, color=colMid, style=line.style_dashed) : na
                if array.size(_zones) >= maxCount
                    f_kill(array.shift(_zones))
                array.push(_zones, fvgZone.new(b, ml, topP, botP, midP, -1))
            if bot_sig[1] and inWindow
                topP = bot_up[1]
                botP = bot_lo[1]
                midP = math.avg(topP, botP)
                b  = box.new(bar_index, topP, bar_index, botP, bgcolor=boxCol, border_color=color.new(boxCol, 0), border_style=line.style_dashed)
                ml = showMid ? line.new(bar_index, midP, bar_index, midP, color=colMid, style=line.style_dashed) : na
                if array.size(_zones) >= maxCount
                    f_kill(array.shift(_zones))
                array.push(_zones, fvgZone.new(b, ml, topP, botP, midP, 1))
 
        // ── Extend live (unmitigated) zones to the current bar ──
        if array.size(_zones) > 0
            for i = 0 to array.size(_zones) - 1
                z = array.get(_zones, i)
                if not z.mitigated
                    box.set_right(z.bx, bar_index)
                    if not na(z.ml)
                        line.set_x2(z.ml, bar_index)
 
//────────────────────────────── Run layers (lowest TF first, so higher TFs paint on top) ──────────────────────────────
f_layer(tf15, en15, max15, col15, ft15, fs15, zones15)
f_layer(tf1H, en1H, max1H, col1H, ft1H, fs1H, zones1H)
f_layer(tf4H, en4H, max4H, col4H, ft4H, fs4H, zones4H)

← 返回列表