FVG Magnet Engine 3D [Neblok]

NeBlok · study · 399 行 · 点赞 228 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NeBlok
 
//@version=6
indicator("FVG Magnet Engine 3D [Neblok]", shorttitle = "FVG Magnet 3D [Neblok]", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500, max_polylines_count = 100)
 
// =====================================================================
//  FVG Magnet Engine 3D [Neblok]
//  Fair value gaps scored by their OWN measured fill rate on this chart.
//
//  Every gap is sorted into a bucket (direction x size in ATRs). When a
//  gap fills or expires, the outcome is recorded for its bucket. The odds
//  shown on each live gap are CONDITIONAL on its age:
//    "Of past gaps in this bucket that were still unfilled at this age,
//     what share went on to fill before the fill window expired?"
//  Odds use a Laplace (+1/+2) correction so small samples are not
//  overstated, and are hidden until a minimum sample size is reached.
// =====================================================================
 
// ── Input groups ──────────────────────────────────────────────────────
string G_DET  = "Detection"
string G_STAT = "Fill Statistics"
string G_DISP = "Display"
string G_3D   = "3D Rendering"
string G_COL  = "Neon Colors"
string G_TBL  = "Odds Dashboard"
string G_ALR  = "Alerts"
 
// ── Detection ─────────────────────────────────────────────────────────
minSizeAtr   = input.float(0.25, "Min Gap Size (× ATR)", minval = 0.0, step = 0.05, group = G_DET, tooltip = "Gaps smaller than this many ATRs are ignored.")
dispAtr      = input.float(0.0, "Min Displacement Body (× ATR)", minval = 0.0, step = 0.1, group = G_DET, tooltip = "Middle candle's body must be at least this many ATRs. 0 = off.")
atrLen       = input.int(14, "ATR Length", minval = 1, group = G_DET)
confirmClose = input.bool(true, "Confirm Gaps On Bar Close", group = G_DET, tooltip = "Prevents gaps from appearing and disappearing on the live bar.")
 
// ── Statistics ────────────────────────────────────────────────────────
fillMode   = input.string("Full Fill", "Fill Definition", options = ["Full Fill", "50% (CE)"], group = G_STAT, tooltip = "Full Fill = price trades through the far edge. 50% (CE) = price reaches the gap midpoint (consequent encroachment).")
horizon    = input.int(150, "Fill Window (bars)", minval = 5, maxval = 2000, group = G_STAT, tooltip = "A gap must fill within this many bars to count as a fill. Unfilled gaps are retired at the end of the window.")
memLen     = input.int(300, "Stats Memory (gaps per bucket)", minval = 20, maxval = 5000, group = G_STAT, tooltip = "Only the most recent N outcomes per bucket are used, so odds adapt to current conditions.")
minSamples = input.int(15, "Min Samples Before Showing Odds", minval = 1, group = G_STAT)
hiThrPct   = input.float(70.0, "High-Odds Threshold (%)", minval = 1.0, maxval = 99.0, group = G_STAT)
hiThr      = hiThrPct / 100.0
 
// ── Display ───────────────────────────────────────────────────────────
showBull      = input.bool(true, "Show Bullish Gaps", group = G_DISP)
showBear      = input.bool(true, "Show Bearish Gaps", group = G_DISP)
maxShow       = input.int(10, "Max Gaps On Chart", minval = 1, maxval = 20, group = G_DISP)
sortMode      = input.string("Nearest to price", "Which Gaps To Show", options = ["Nearest to price", "Most recent"], group = G_DISP)
extendBars    = input.int(20, "Extend Right (bars)", minval = 5, maxval = 300, group = G_DISP)
showGhost     = input.bool(true, "Show Original Gap Outline", group = G_DISP)
showCE        = input.bool(true, "Show 50% (CE) Line", group = G_DISP)
showMagnet    = input.bool(true, "Show Magnet Pull Lines", group = G_DISP)
showFillMarks = input.bool(true, "Mark Bars Where Gaps Filled", group = G_DISP)
labelSizeIn   = input.string("Large", "Label Size", options = ["Normal", "Large", "Huge"], group = G_DISP)
 
// ── 3D ────────────────────────────────────────────────────────────────
show3D    = input.bool(true, "3D Blocks", group = G_3D)
depthBars = input.int(4, "3D Depth (bars)", minval = 1, maxval = 30, group = G_3D)
depthAtr  = input.float(0.35, "3D Lift (× ATR)", minval = 0.0, step = 0.05, group = G_3D)
 
// ── Colors (flat neon, no gradients) ──────────────────────────────────
bullCol  = input.color(#39FF14, "Bullish Neon", group = G_COL)
bearCol  = input.color(#FF1E8C, "Bearish Neon", group = G_COL)
hiCol    = input.color(#FFF01F, "High-Odds Outline", group = G_COL)
magCol   = input.color(#00F0FF, "Magnet Line", group = G_COL)
panelCol = input.color(#05060D, "Label / Panel Background", group = G_COL)
 
// ── Dashboard ─────────────────────────────────────────────────────────
showTable   = input.bool(true, "Show Odds Dashboard", group = G_TBL)
tablePosIn  = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right"], group = G_TBL)
tableSizeIn = input.string("Large", "Text Size", options = ["Normal", "Large", "Huge"], group = G_TBL)
 
// ── Alerts ────────────────────────────────────────────────────────────
alertNew  = input.bool(true, "Alert: New FVG", group = G_ALR)
alertTap  = input.bool(true, "Alert: First Tap Of A High-Odds FVG", group = G_ALR)
alertFill = input.bool(true, "Alert: FVG Filled", group = G_ALR)
 
// =====================================================================
//  Types & state
// =====================================================================
type FVG
    float top
    float bot
    float oTop
    float oBot
    int   xL
    int   born
    bool  bull
    int   bucket
    float sizeAtr
    bool  touched
 
type Stat
    array<int> outcomes   // fill age in bars, or horizon + 1 for a miss
 
var array<FVG>  gaps  = array.new<FVG>()
var array<Stat> stats = array.new<Stat>()
if stats.size() == 0
    for k = 0 to 7
        stats.push(Stat.new(array.new<int>()))
 
var array<box>      drawnBoxes  = array.new<box>()
var array<line>     drawnLines  = array.new<line>()
var array<label>    drawnLabels = array.new<label>()
var array<polyline> drawnPolys  = array.new<polyline>()
var table           tbl         = na
 
// =====================================================================
//  Helpers
// =====================================================================
f_size(string s) =>
    switch s
        "Normal" => size.normal
        "Large"  => size.large
        "Huge"   => size.huge
        => size.large
 
f_pos(string s) =>
    switch s
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        "Bottom Left"  => position.bottom_left
        "Middle Right" => position.middle_right
        => position.top_right
 
// Size buckets: <0.5, 0.5-1, 1-2, 2+ ATR
f_bucket(float m) =>
    m < 0.5 ? 0 : m < 1.0 ? 1 : m < 2.0 ? 2 : 3
 
// Stat index: bullish 0-3, bearish 4-7
f_key(bool isBull, int b) =>
    (isBull ? 0 : 4) + b
 
f_record(int k, int v) =>
    Stat s = stats.get(k)
    s.outcomes.push(v)
    if s.outcomes.size() > memLen
        s.outcomes.shift()
    true
 
// Conditional fill odds for a gap of bucket k that has survived `age` bars
f_odds(int k, int age) =>
    Stat s = stats.get(k)
    int n = 0
    int h = 0
    for v in s.outcomes
        if v > age
            n += 1
            if v <= horizon
                h += 1
    float p = n > 0 ? (h + 1.0) / (n + 2.0) : na
    [p, n]
 
f_avgFill(int k) =>
    Stat s = stats.get(k)
    float sum = 0.0
    int c = 0
    for v in s.outcomes
        if v <= horizon
            sum += v
            c += 1
    c > 0 ? sum / c : na
 
f_oddsTxt(int k, int age) =>
    [p, n] = f_odds(k, age)
    n >= minSamples ? str.tostring(p * 100, "#") + "%" : "learning (n=" + str.tostring(n) + ")"
 
// =====================================================================
//  Core engine
// =====================================================================
float atr         = ta.atr(atrLen)
bool  bullFillBar = false
bool  bearFillBar = false
string alertMsg   = ""
string tfTxt      = syminfo.ticker + " " + timeframe.period
 
// ── 1) Update live gaps: shrink, detect taps, record fills / expiries ─
if gaps.size() > 0
    for i = gaps.size() - 1 to 0
        FVG g   = gaps.get(i)
        int age = bar_index - g.born
        if age > 0
            int   k      = f_key(g.bull, g.bucket)
            float mid    = (g.oTop + g.oBot) / 2.0
            bool  filled = false
            bool  tapped = false
            if g.bull
                if low < g.top
                    g.top := math.max(low, g.bot)
                    if not g.touched
                        g.touched := true
                        tapped    := true
                float target = fillMode == "Full Fill" ? g.oBot : mid
                filled := low <= target
            else
                if high > g.bot
                    g.bot := math.min(high, g.top)
                    if not g.touched
                        g.touched := true
                        tapped    := true
                float target = fillMode == "Full Fill" ? g.oTop : mid
                filled := high >= target
 
            if tapped and not filled and alertTap
                [tp, tn] = f_odds(k, age - 1)
                if tn >= minSamples and tp >= hiThr
                    alertMsg += (g.bull ? "BULL" : "BEAR") + " FVG first tap (high odds " + str.tostring(tp * 100, "#") + "%) " + tfTxt + " | " + str.tostring(g.oBot, format.mintick) + " – " + str.tostring(g.oTop, format.mintick) + "\n"
 
            if filled
                f_record(k, age)
                if g.bull
                    bullFillBar := true
                else
                    bearFillBar := true
                if alertFill
                    alertMsg += (g.bull ? "BULL" : "BEAR") + " FVG filled after " + str.tostring(age) + " bars " + tfTxt + " | " + str.tostring(g.oBot, format.mintick) + " – " + str.tostring(g.oTop, format.mintick) + "\n"
                gaps.remove(i)
            else if age >= horizon
                f_record(k, horizon + 1)
                gaps.remove(i)
 
// ── 2) Detect new gaps ────────────────────────────────────────────────
bool canDetect = confirmClose ? barstate.isconfirmed : true
if canDetect and not na(atr) and atr > 0 and bar_index >= 2
    float body1  = math.abs(close[1] - open[1])
    bool  dispOk = body1 >= dispAtr * atr
 
    if low > high[2] and dispOk
        float sz = low - high[2]
        if sz >= minSizeAtr * atr
            float m = sz / atr
            int   b = f_bucket(m)
            gaps.push(FVG.new(low, high[2], low, high[2], bar_index - 1, bar_index, true, b, m, false))
            if alertNew
                alertMsg += "New BULL FVG " + tfTxt + " | " + str.tostring(high[2], format.mintick) + " – " + str.tostring(low, format.mintick) + " | " + str.tostring(m, "#.##") + "× ATR | fill odds " + f_oddsTxt(f_key(true, b), 0) + "\n"
 
    if high < low[2] and dispOk
        float sz = low[2] - high
        if sz >= minSizeAtr * atr
            float m = sz / atr
            int   b = f_bucket(m)
            gaps.push(FVG.new(low[2], high, low[2], high, bar_index - 1, bar_index, false, b, m, false))
            if alertNew
                alertMsg += "New BEAR FVG " + tfTxt + " | " + str.tostring(high, format.mintick) + " – " + str.tostring(low[2], format.mintick) + " | " + str.tostring(m, "#.##") + "× ATR | fill odds " + f_oddsTxt(f_key(false, b), 0) + "\n"
 
if gaps.size() > 600
    gaps.shift()
 
// =====================================================================
//  Rendering (last bar only — redrawn cleanly each update)
// =====================================================================
if barstate.islast
    while drawnBoxes.size() > 0
        drawnBoxes.pop().delete()
    while drawnLines.size() > 0
        drawnLines.pop().delete()
    while drawnLabels.size() > 0
        drawnLabels.pop().delete()
    while drawnPolys.size() > 0
        drawnPolys.pop().delete()
 
    float dy     = show3D ? nz(atr) * depthAtr : 0.0
    int   dx     = show3D ? depthBars : 0
    int   xRight = bar_index + extendBars
    lblSize      = f_size(labelSizeIn)
 
    // Pick which gaps to draw
    array<float> keys = array.new<float>()
    array<int>   idxs = array.new<int>()
    for [i, g] in gaps
        bool want = g.bull ? showBull : showBear
        if want
            float d = close > g.top ? close - g.top : close < g.bot ? g.bot - close : 0.0
            keys.push(sortMode == "Nearest to price" ? d : float(-g.born))
            idxs.push(i)
    array<int> sorted = keys.sort_indices(order.ascending)
    int cnt = math.min(maxShow, sorted.size())
 
    // Nearest magnet targets above / below price
    float upD     = na
    float upEdge  = na
    float upP     = na
    bool  upValid = false
    float dnD     = na
    float dnEdge  = na
    float dnP     = na
    bool  dnValid = false
 
    if cnt > 0
        for j = 0 to cnt - 1
            FVG   g      = gaps.get(idxs.get(sorted.get(j)))
            int   k      = f_key(g.bull, g.bucket)
            int   age    = bar_index - g.born
            [p, n]       = f_odds(k, age)
            bool  valid  = n >= minSamples and not na(p)
            bool  hi     = valid and p >= hiThr
            color neon   = g.bull ? bullCol : bearCol
            color edge   = hi ? hiCol : neon
            float mid0   = (g.oTop + g.oBot) / 2.0
            float range0 = g.oTop - g.oBot
            float fillPct = range0 > 0 ? 1.0 - (g.top - g.bot) / range0 : 0.0
 
            // Original gap outline
            if showGhost
                drawnBoxes.push(box.new(left = g.xL, top = g.oTop, right = xRight, bottom = g.oBot, border_color = color.new(neon, 35), border_width = 1, border_style = line.style_dotted, bgcolor = color.new(neon, 94)))
 
            // 3D faces (top = lit face, side = shadow face)
            if show3D
                array<chart.point> topPts = array.new<chart.point>()
                topPts.push(chart.point.from_index(g.xL, g.top))
                topPts.push(chart.point.from_index(xRight, g.top))
                topPts.push(chart.point.from_index(xRight + dx, g.top + dy))
                topPts.push(chart.point.from_index(g.xL + dx, g.top + dy))
                drawnPolys.push(polyline.new(points = topPts, curved = false, closed = true, xloc = xloc.bar_index, line_color = edge, fill_color = color.new(neon, 55), line_width = 1))
 
                array<chart.point> sidePts = array.new<chart.point>()
                sidePts.push(chart.point.from_index(xRight, g.top))
                sidePts.push(chart.point.from_index(xRight + dx, g.top + dy))
                sidePts.push(chart.point.from_index(xRight + dx, g.bot + dy))
                sidePts.push(chart.point.from_index(xRight, g.bot))
                drawnPolys.push(polyline.new(points = sidePts, curved = false, closed = true, xloc = xloc.bar_index, line_color = edge, fill_color = color.new(neon, 80), line_width = 1))
 
            // Front face = remaining unfilled part of the gap
            drawnBoxes.push(box.new(left = g.xL, top = g.top, right = xRight, bottom = g.bot, border_color = edge, border_width = hi ? 2 : 1, bgcolor = color.new(neon, 68)))
 
            if showCE
                drawnLines.push(line.new(x1 = g.xL, y1 = mid0, x2 = xRight, y2 = mid0, color = color.new(neon, 15), style = line.style_dashed, width = 1))
 
            // Info label
            string dirTxt  = g.bull ? "▲ BULL FVG" : "▼ BEAR FVG"
            string starTxt = hi ? "  ★ HIGH ODDS" : ""
            string oddsTxt = valid ? "FILL ODDS " + str.tostring(p * 100, "#") + "%  (n=" + str.tostring(n) + ")" : "LEARNING  (n=" + str.tostring(n) + "/" + str.tostring(minSamples) + ")"
            string infoTxt = str.tostring(g.sizeAtr, "#.##") + "× ATR · " + str.tostring(fillPct * 100, "#") + "% filled · " + str.tostring(age) + "/" + str.tostring(horizon) + " bars"
            drawnLabels.push(label.new(x = xRight + dx + 2, y = (g.top + g.bot) / 2.0 + dy / 2.0, text = dirTxt + starTxt + "\n" + oddsTxt + "\n" + infoTxt, xloc = xloc.bar_index, color = color.new(panelCol, 10), style = label.style_label_left, textcolor = edge, size = lblSize, textalign = text.align_left, text_font_family = font.family_monospace))
 
            // Track nearest gap above and below price for magnet lines
            if g.bot > close and (na(upD) or g.bot - close < upD)
                upD     := g.bot - close
                upEdge  := g.bot
                upP     := p
                upValid := valid
            if g.top < close and (na(dnD) or close - g.top < dnD)
                dnD     := close - g.top
                dnEdge  := g.top
                dnP     := p
                dnValid := valid
 
    // Magnet pull lines: thicker = higher fill odds
    if showMagnet
        int xMag = bar_index + math.round(extendBars * 0.5)
        if not na(upD)
            int w = upValid ? 1 + math.round(upP * 5) : 1
            drawnLines.push(line.new(x1 = bar_index, y1 = close, x2 = xMag, y2 = upEdge, color = magCol, style = upValid ? line.style_arrow_right : line.style_dashed, width = w))
            string t = "MAGNET ▲ " + (upValid ? str.tostring(upP * 100, "#") + "%" : "learning") + "\n+" + str.tostring(upD, format.mintick) + " pts · " + str.tostring(upD / nz(atr, 1), "#.##") + " ATR"
            drawnLabels.push(label.new(x = bar_index + 1, y = (close + upEdge) / 2.0, text = t, xloc = xloc.bar_index, color = color.new(panelCol, 10), style = label.style_label_left, textcolor = magCol, size = lblSize, textalign = text.align_left, text_font_family = font.family_monospace))
        if not na(dnD)
            int w = dnValid ? 1 + math.round(dnP * 5) : 1
            drawnLines.push(line.new(x1 = bar_index, y1 = close, x2 = xMag, y2 = dnEdge, color = magCol, style = dnValid ? line.style_arrow_right : line.style_dashed, width = w))
            string t = "MAGNET ▼ " + (dnValid ? str.tostring(dnP * 100, "#") + "%" : "learning") + "\n-" + str.tostring(dnD, format.mintick) + " pts · " + str.tostring(dnD / nz(atr, 1), "#.##") + " ATR"
            drawnLabels.push(label.new(x = bar_index + 1, y = (close + dnEdge) / 2.0, text = t, xloc = xloc.bar_index, color = color.new(panelCol, 10), style = label.style_label_left, textcolor = magCol, size = lblSize, textalign = text.align_left, text_font_family = font.family_monospace))
 
    // ── Odds dashboard ────────────────────────────────────────────────
    if showTable
        tSize = f_size(tableSizeIn)
        if na(tbl)
            tbl := table.new(f_pos(tablePosIn), 5, 6, bgcolor = panelCol, frame_color = magCol, frame_width = 2, border_color = color.new(magCol, 70), border_width = 1)
            table.merge_cells(tbl, 0, 0, 4, 0)
        table.cell(tbl, 0, 0, "FVG MAGNET ENGINE 3D  ·  FILL ODDS ≤ " + str.tostring(horizon) + " BARS  ·  " + fillMode, text_color = magCol, text_size = tSize, bgcolor = panelCol)
        table.cell(tbl, 0, 1, "SIZE (ATR)", text_color = color.white, text_size = tSize, bgcolor = panelCol)
        table.cell(tbl, 1, 1, "< 0.5", text_color = color.white, text_size = tSize, bgcolor = panelCol)
        table.cell(tbl, 2, 1, "0.5 – 1", text_color = color.white, text_size = tSize, bgcolor = panelCol)
        table.cell(tbl, 3, 1, "1 – 2", text_color = color.white, text_size = tSize, bgcolor = panelCol)
        table.cell(tbl, 4, 1, "2 +", text_color = color.white, text_size = tSize, bgcolor = panelCol)
        for d = 0 to 1
            bool  isBull = d == 0
            color dCol   = isBull ? bullCol : bearCol
            int   rOdds  = 2 + d * 2
            int   rAvg   = 3 + d * 2
            table.cell(tbl, 0, rOdds, isBull ? "▲ BULL odds" : "▼ BEAR odds", text_color = dCol, text_size = tSize, bgcolor = panelCol)
            table.cell(tbl, 0, rAvg, "   avg bars", text_color = dCol, text_size = tSize, bgcolor = panelCol)
            for b = 0 to 3
                int k = f_key(isBull, b)
                [p, n] = f_odds(k, 0)
                bool  ok    = n >= minSamples and not na(p)
                color cCol  = ok and p >= hiThr ? hiCol : dCol
                string oTxt = ok ? str.tostring(p * 100, "#") + "% · n" + str.tostring(n) : "— · n" + str.tostring(n)
                float avg   = f_avgFill(k)
                string aTxt = na(avg) ? "—" : str.tostring(avg, "#.#")
                table.cell(tbl, b + 1, rOdds, oTxt, text_color = cCol, text_size = tSize, bgcolor = panelCol)
                table.cell(tbl, b + 1, rAvg, aTxt, text_color = dCol, text_size = tSize, bgcolor = panelCol)
 
// =====================================================================
//  Fill markers & alerts
// =====================================================================
plotshape(showFillMarks and bullFillBar, title = "Bull FVG Filled", style = shape.diamond, location = location.belowbar, color = bullCol, size = size.small)
plotshape(showFillMarks and bearFillBar, title = "Bear FVG Filled", style = shape.diamond, location = location.abovebar, color = bearCol, size = size.small)
 
if alertMsg != ""
    alert(alertMsg, alert.freq_once_per_bar_close)

← 返回列表