ABW Multi-Timeframe FVG

Asif_Brain_Waves · study · 158 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("ABW Multi-Timeframe FVG", overlay = true, max_boxes_count = 500, dynamic_requests = true)
 
// Bullish: candle 3 low > candle 1 high. Bearish: candle 3 high < candle 1 low.
// Equal wick prices do not form a gap. Candle 2 has no body or color requirement.
// Left-edge assumption: source candle 1 open. Right edge: source candle 3 close.
// Box geometry stays fixed; later price movement does not extend, shrink, or mitigate it.
// A confirmed HTF gap becomes available at the first update of the next HTF period.
// It is then drawn over its earlier source interval; the gap was not known before confirmation.
// Only standard time-based charts are supported. Synthetic and tick charts raise an error.
// Defaults: 5 bullish + 5 bearish chart boxes, and 3 bullish + 3 bearish HTF boxes, separately.
// Recency uses source candle 3 close time, not the arrival time of delayed HTF data.
 
const string GROUP_LAYERS = "Visibility"
const string GROUP_STYLE = "Appearance"
const string GROUP_RETENTION = "FVG Retention"
const string GROUP_HTF = "Higher Timeframe"
 
bool showChart = input.bool(true, "Show chart timeframe FVGs", group = GROUP_LAYERS)
bool showHigher = input.bool(true, "Show higher timeframe FVGs", group = GROUP_LAYERS)
bool showLabels = input.bool(true, "Show source timeframe labels", group = GROUP_STYLE)
int fillTransparency = input.int(85, "Fill transparency (0 opaque, 100 transparent)", minval = 0, maxval = 100, group = GROUP_STYLE)
bool recentOnly = input.bool(true, "Show recent FVGs only", group = GROUP_RETENTION,
     tooltip = "Limits apply separately to each timeframe layer and direction. Chart gaps do not remove higher timeframe boxes. Recency is determined by source candle 3 close time.")
int currentBullCount = input.int(5, "Chart: bullish FVG limit", minval = 0, maxval = 125, group = GROUP_RETENTION, active = recentOnly,
     tooltip = "Set to 0 to hide chart timeframe bullish FVGs. Each limit supports up to 125 boxes, keeping the four limits within 500 boxes in total.")
int currentBearCount = input.int(5, "Chart: bearish FVG limit", minval = 0, maxval = 125, group = GROUP_RETENTION, active = recentOnly,
     tooltip = "Set to 0 to hide chart timeframe bearish FVGs. Each limit supports up to 125 boxes, keeping the four limits within 500 boxes in total.")
int higherBullCount = input.int(3, "Higher timeframe: bullish FVG limit", minval = 0, maxval = 125, group = GROUP_RETENTION, active = recentOnly,
     tooltip = "Set to 0 to hide higher timeframe bullish FVGs. Each limit supports up to 125 boxes, keeping the four limits within 500 boxes in total.")
int higherBearCount = input.int(3, "Higher timeframe: bearish FVG limit", minval = 0, maxval = 125, group = GROUP_RETENTION, active = recentOnly,
     tooltip = "Set to 0 to hide higher timeframe bearish FVGs. Each limit supports up to 125 boxes, keeping the four limits within 500 boxes in total.")
int retainedLimit = input.int(500, "Historical mode: total FVG limit", minval = 1, maxval = 500, group = GROUP_RETENTION, active = not recentOnly,
     tooltip = "Applies when recent-only mode is off. The oldest box by source candle 3 close time is removed first. Later mitigation does not remove boxes.")
bool useManualHigher = input.bool(false, "Use a manual higher timeframe", group = GROUP_HTF)
string manualHigher = input.timeframe("60", "Manual higher timeframe", group = GROUP_HTF,
     tooltip = "Must be strictly higher than the chart timeframe. Tick timeframes are not supported.")
 
// Pine expresses hourly timeframes in minutes: 60 = 1 hour, 240 = 4 hours.
f_autoHigher() =>
    string result = ""
    if timeframe.isminutes
        result := switch timeframe.multiplier
            1 => "15"
            3 => "30"
            5 => "60"
            15 => "240"
            60 => "1D"
            240 => "1W"
            => ""
    else if timeframe.isdaily and timeframe.multiplier == 1
        result := "1M"
    result
 
f_timeframeLabel(string sourceTf) =>
    switch sourceTf
        "1" => "One Minute"
        "3" => "Three Minutes"
        "5" => "5M"
        "15" => "15M"
        "30" => "Thirty Minutes"
        "60" => "1H"
        "240" => "4H"
        "1D" => "1D"
        "1W" => "W"
        "1M" => "M"
        => sourceTf
 
string higherTf = useManualHigher ? manualHigher : f_autoHigher()
 
if barstate.isfirst
    if not chart.is_standard or timeframe.isticks
        runtime.error("Use a standard time-based chart. Heikin Ashi, Renko, Range, other synthetic charts, and tick charts are not supported.")
    if showHigher and useManualHigher
        if manualHigher == "" or str.endswith(manualHigher, "T")
            runtime.error("The manual higher timeframe must be time-based and strictly higher than the chart timeframe.")
        if timeframe.in_seconds(manualHigher) <= timeframe.in_seconds(timeframe.period)
            runtime.error("Select a manual timeframe strictly higher than the chart timeframe.")
 
// Arrays are sorted by ascending source close time; index 0 contains the oldest box.
// Recent mode uses a separate array for each layer and direction to prevent cross-layer eviction.
var array<box> currentBulls = array.new<box>()
var array<box> currentBears = array.new<box>()
var array<box> higherBulls = array.new<box>()
var array<box> higherBears = array.new<box>()
var array<box> historyBoxes = array.new<box>()
 
f_addBox(int leftTime, int rightTime, float topPrice, float bottomPrice, bool bullish, string sourceTf, bool higherLayer) =>
    array<box> activeBoxes = historyBoxes
    int activeLimit = retainedLimit
    if recentOnly
        if higherLayer
            activeBoxes := bullish ? higherBulls : higherBears
            activeLimit := bullish ? higherBullCount : higherBearCount
        else
            activeBoxes := bullish ? currentBulls : currentBears
            activeLimit := bullish ? currentBullCount : currentBearCount
    box createdBox = na
    if activeLimit > 0
        bool keepCandidate = true
        if array.size(activeBoxes) >= activeLimit
            // An older delayed HTF gap cannot displace newer boxes in its own retention group.
            keepCandidate := rightTime >= box.get_right(array.get(activeBoxes, 0))
        if keepCandidate
            // Delete before creating to keep the total within the 500-box object limit.
            if array.size(activeBoxes) >= activeLimit
                box.delete(array.shift(activeBoxes))
            color zoneColor = bullish ? color.blue : color.red
            string zoneText = showLabels ? f_timeframeLabel(sourceTf) : ""
            createdBox := box.new(left = leftTime, top = topPrice, right = rightTime, bottom = bottomPrice,
                 xloc = xloc.bar_time, extend = extend.none, border_color = na,
                 border_width = 0, bgcolor = color.new(zoneColor, fillTransparency),
                 text = zoneText, text_color = chart.fg_color, text_size = size.tiny,
                 text_halign = text.align_right, text_valign = text.align_top,
                 text_formatting = text.format_none)
            int insertAt = array.size(activeBoxes)
            if array.size(activeBoxes) > 0
                for i = 0 to array.size(activeBoxes) - 1
                    if rightTime < box.get_right(array.get(activeBoxes, i))
                        insertAt := i
                        break
            array.insert(activeBoxes, insertAt, createdBox)
    createdBox
 
// Normal var persistence allows the timestamp and drawing to be recreated after realtime rollback.
// Using varip here could preserve the timestamp while losing the first tick's drawing to rollback.
var int lastHigherClose = na
 
if showHigher and higherTf != ""
    // Every source series is offset by at least one HTF bar to avoid future data leakage.
    // Candle 3 = [1], candle 1 = [3]. Only confirmed source candles are requested.
    [higherLow3, higherHigh3, higherHigh1, higherLow1, higherLeft, higherRight] = request.security(
         syminfo.tickerid, higherTf, [low[1], high[1], high[3], low[3], time[3], time_close[1]],
         gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
    bool newHigherCandle = not na(higherRight) and (na(lastHigherClose) or higherRight != lastHigherClose)
    if newHigherCandle
        lastHigherClose := higherRight
        if not na(higherLeft) and not na(higherHigh1) and not na(higherLow1)
            if higherLow3 > higherHigh1
                f_addBox(higherLeft, higherRight, higherLow3, higherHigh1, true, higherTf, true)
            else if higherHigh3 < higherLow1
                f_addBox(higherLeft, higherRight, higherLow1, higherHigh3, false, higherTf, true)
 
// Chart FVGs are created on candle 3's closing update, never before confirmation.
if showChart and barstate.isconfirmed and bar_index >= 2
    if low > high[2]
        f_addBox(time[2], time_close, low, high[2], true, timeframe.period, false)
    else if high < low[2]
        f_addBox(time[2], time_close, low[2], high, false, timeframe.period, false)
 
// Unmapped chart intervals do not silently select an arbitrary higher timeframe.
var table status = table.new(position.top_right, 1, 1)
if barstate.islast
    if showHigher and higherTf == ""
        table.cell(status, 0, 0, "No automatic higher timeframe is defined for this chart interval.\nChoose a manual higher timeframe in Inputs, or disable the higher timeframe layer.",
             text_color = chart.fg_color, bgcolor = color.new(chart.bg_color, 10), text_size = size.small)
    else
        table.clear(status, 0, 0)

← 返回列表