Fair Value Gaps - Fill Rate Statistics [Dots3Red]

Dots3Red · study · 198 行 · 点赞 325 · TradingView 原页

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

Pine Script

//@version=6
indicator("Fair Value Gaps - Fill Rate Statistics [Dots3Red]",
          shorttitle = "FVG Stats [D3R]",
          overlay    = true,
          max_boxes_count = 500)
 
// Inputs
C_TXT   = color.new(#f8fafc, 0)
C_DIM   = color.new(#94a3b8, 0)
C_BG    = color.new(#131722, 5)
C_BORD  = color.new(#334155, 0)
C_AMBER = color.new(#ffb700, 0)
C_UP    = color.new(#00f0ff, 0)
C_DN    = color.new(#ff00aa, 0)
 
GRP_DET = "🔍 Gap Detection"
GRP_ENG = "🎯 Fill Tracking"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
 
min_atr = input.float(0.25, "Min Gap Size (× ATR)", minval=0.0, step=0.05, group=GRP_DET,
          tooltip="Gaps smaller than this fraction of ATR are ignored as noise.")
atr_len = input.int(14, "ATR Length", minval=2, group=GRP_DET)
 
horizon = input.int(50, "Track Each Gap For (bars)", minval=5, maxval=200, group=GRP_ENG,
          tooltip="Each gap is followed for this many bars. Whatever it reached in that window (touched, half filled, fully filled) is what gets counted.")
min_n   = input.int(15, "Min Gaps to Show Rates", minval=5, group=GRP_ENG)
 
show_bull   = input.bool(true, "Draw Bullish Gaps", group=GRP_VIS)
show_bear   = input.bool(true, "Draw Bearish Gaps", group=GRP_VIS)
col_bull    = input.color(#00f0ff, "Bullish Gap Color", group=GRP_VIS)
col_bear    = input.color(#ff00aa, "Bearish Gap Color", group=GRP_VIS)
box_tr      = input.int(82, "Active Gap Transparency", minval=40, maxval=98, group=GRP_VIS)
keep_closed = input.bool(true, "Keep Filled / Expired Gaps", group=GRP_VIS,
              tooltip="Filled gaps stay on the chart, faded. Gaps that expired unfilled get a dotted border.")
closed_tr   = input.int(94, "Filled / Expired Transparency", minval=60, maxval=100, group=GRP_VIS)
 
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos  = input.string("Top Right", "Position",
           options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
 
type Gap
    float top
    float bot
    int   born
    bool  touched   = false
    bool  half      = false
    int   touch_bar = -1
    box   bx
 
var array<Gap> bulls = array.new<Gap>()
var array<Gap> bears = array.new<Gap>()
 
var float[] st_n    = array.new<float>(2, 0.0)
var float[] st_t    = array.new<float>(2, 0.0)
var float[] st_h    = array.new<float>(2, 0.0)
var float[] st_f    = array.new<float>(2, 0.0)
var float[] st_bars = array.new<float>(2, 0.0)
 
float atr = ta.atr(atr_len)
 
f_process(array<Gap> gaps, bool bull) =>
    int  side  = bull ? 0 : 1
    bool new_t = false
    int  i     = array.size(gaps) - 1
    while i >= 0
        Gap g = array.get(gaps, i)
        if bar_index > g.born
            float mid = (g.top + g.bot) / 2.0
            bool  t   = bull ? low <= g.top : high >= g.bot
            bool  hf  = bull ? low <= mid   : high >= mid
            bool  f   = bull ? low <= g.bot : high >= g.top
            if t and not g.touched
                g.touched   := true
                g.touch_bar := bar_index
                new_t       := true
            if hf
                g.half := true
            bool expired = bar_index - g.born >= horizon
            if f or expired
                array.set(st_n, side, array.get(st_n, side) + 1.0)
                if g.touched
                    array.set(st_t, side, array.get(st_t, side) + 1.0)
                    array.set(st_bars, side, array.get(st_bars, side) + (g.touch_bar - g.born))
                if g.half
                    array.set(st_h, side, array.get(st_h, side) + 1.0)
                if f
                    array.set(st_f, side, array.get(st_f, side) + 1.0)
                if not na(g.bx)
                    if keep_closed
                        box.set_right(g.bx, bar_index)
                        box.set_bgcolor(g.bx, color.new(bull ? col_bull : col_bear, closed_tr))
                        box.set_border_color(g.bx, color.new(bull ? col_bull : col_bear, math.min(100, closed_tr - 10)))
                        if not f
                            box.set_border_style(g.bx, line.style_dotted)
                    else
                        box.delete(g.bx)
                array.remove(gaps, i)
            else if not na(g.bx)
                box.set_right(g.bx, bar_index + 1)
        i -= 1
    new_t
 
bool bull_gap = not na(atr) and low > high[2] and (low - high[2]) >= min_atr * atr
bool bear_gap = not na(atr) and high < low[2] and (low[2] - high) >= min_atr * atr
 
bool touch_bull = false
bool touch_bear = false
bool new_bull   = false
bool new_bear   = false
 
if barstate.isconfirmed
    touch_bull := f_process(bulls, true)
    touch_bear := f_process(bears, false)
 
    if bull_gap
        box b = show_bull ? box.new(bar_index - 1, low, bar_index + 1, high[2],
                                    border_color=color.new(col_bull, math.max(0, box_tr - 30)),
                                    bgcolor=color.new(col_bull, box_tr)) : na
        array.push(bulls, Gap.new(low, high[2], bar_index, false, false, -1, b))
        new_bull := true
 
    if bear_gap
        box b = show_bear ? box.new(bar_index - 1, low[2], bar_index + 1, high,
                                    border_color=color.new(col_bear, math.max(0, box_tr - 30)),
                                    bgcolor=color.new(col_bear, box_tr)) : na
        array.push(bears, Gap.new(low[2], high, bar_index, false, false, -1, b))
        new_bear := true
 
f_rate(int side, float[] arr) =>
    float n = array.get(st_n, side)
    n >= min_n ? str.tostring(math.round(array.get(arr, side) / n * 100.0)) + "%" : "…"
 
f_avg_bars(int side) =>
    float c = array.get(st_t, side)
    c >= min_n ? str.tostring(array.get(st_bars, side) / c, "#.#") : "…"
 
// Dashboard
f_hud_pos(string s) =>
    switch s
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        =>                position.bottom_left
 
var table hud = table.new(f_hud_pos(hud_pos), 6, 6,
                          bgcolor=C_BG, border_color=C_BORD,
                          border_width=1, frame_color=C_BORD, frame_width=2)
 
f_side_row(int row, int side, color bg) =>
    table.cell(hud, 0, row, side == 0 ? "▲ Bullish" : "▼ Bearish", text_color = side == 0 ? C_UP : C_DN, bgcolor=bg, text_size=size.small)
    table.cell(hud, 1, row, str.tostring(int(array.get(st_n, side))), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, row, f_rate(side, st_t), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 3, row, f_rate(side, st_h), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 4, row, f_rate(side, st_f), text_color=C_AMBER, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 5, row, f_avg_bars(side), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
 
if show_hud and barstate.islast
    color bgA = color.new(#1e293b, 40)
    color bgB = color.new(#0f172a, 40)
 
    table.cell(hud, 0, 0, "FVG STATS [D3R]", text_color=C_TXT,
               bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 0, 0, 5, 0)
 
    table.cell(hud, 0, 1, "Side",    text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 1, "Gaps",    text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, 1, "Touched", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of gaps that price came back into at least once within " + str.tostring(horizon) + " bars.")
    table.cell(hud, 3, 1, "50%",     text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of gaps where price reached the midpoint within " + str.tostring(horizon) + " bars.")
    table.cell(hud, 4, 1, "Filled",  text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of gaps that were fully filled (price reached the far edge) within " + str.tostring(horizon) + " bars.")
    table.cell(hud, 5, 1, "Bars to Touch", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Average number of bars from gap formation to the first touch, for gaps that were touched.")
 
    f_side_row(2, 0, bgA)
    f_side_row(3, 1, bgB)
 
    table.cell(hud, 0, 4, "Active Now", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 4, str.tostring(array.size(bulls)) + " bullish · " + str.tostring(array.size(bears)) + " bearish",
               text_color=C_TXT, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 4, 5, 4)
 
    table.cell(hud, 0, 5, "Window / Min Size", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 5, str.tostring(horizon) + " bars / " + str.tostring(min_atr) + "× ATR",
               text_color=C_TXT, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 5, 5, 5)
 
// Visual
plotshape(new_bull and show_bull, "New Bullish FVG", shape.triangleup,   location.belowbar, color.new(col_bull, 30), size=size.tiny, offset=-1)
plotshape(new_bear and show_bear, "New Bearish FVG", shape.triangledown, location.abovebar, color.new(col_bear, 30), size=size.tiny, offset=-1)
 
// Alert
alertcondition(new_bull,   "New Bullish FVG",       "D3R FVG Stats: new bullish fair value gap on {{ticker}}")
alertcondition(new_bear,   "New Bearish FVG",       "D3R FVG Stats: new bearish fair value gap on {{ticker}}")
alertcondition(touch_bull, "Price Entered Bullish FVG", "D3R FVG Stats: price came back into a bullish fair value gap on {{ticker}}")
alertcondition(touch_bear, "Price Entered Bearish FVG", "D3R FVG Stats: price came back into a bearish fair value gap on {{ticker}}")

← 返回列表