Trendlines, Support, Resistance & Fibonacci Alerts [Neblok]

NeBlok · study · 580 行 · 点赞 516 · 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
 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Neblok — free community script
//@version=6
indicator("Trendlines, Support, Resistance & Fibonacci Alerts [Neblok]", shorttitle = "TL • SR • FIB [Neblok]", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500, max_bars_back = 2000)
 
// ╔═══════════════════════════════════════════════════════════════════╗
// ║  NEBLOK — Trendlines • Support/Resistance • Fibonacci (+ Alerts)  ║
// ║  • Auto trendlines from validated swing pivots (up + down)        ║
// ║  • Range Top/Bottom S/R zones + pivot-cluster S/R zones (touches) ║
// ║  • Auto-direction Fibonacci w/ Golden Pocket + extensions         ║
// ║  • Neon styling, faux-3D depth, glow, large readable labels       ║
// ║  • Live dashboard + alertcondition() and alert() events           ║
// ╚═══════════════════════════════════════════════════════════════════╝
 
// ═════════════════════════════ INPUTS ═════════════════════════════
string G_SW = "① Swing Detection"
int pivLen   = input.int(10,  "Pivot Length",   minval = 2,  maxval = 50,   group = G_SW, tooltip = "Bars required on each side to confirm a swing high / low.")
int rangeLen = input.int(200, "Range Lookback", minval = 50, maxval = 1500, group = G_SW, tooltip = "Bars used to find the range Top / Bottom that anchor the S/R zones and the Fibonacci.")
 
string G_TL = "② Trendlines"
bool   showDn     = input.bool(true, "Down Trendlines", inline = "tl", group = G_TL)
bool   showUp     = input.bool(true, "Up Trendlines",   inline = "tl", group = G_TL)
int    maxTL      = input.int(3,   "Max Active Lines (per side)",  minval = 1,  maxval = 10,   group = G_TL)
int    pivScan    = input.int(8,   "Pivots Scanned per New Line",  minval = 2,  maxval = 30,   group = G_TL)
int    maxSpan    = input.int(400, "Max Line Span (bars)",         minval = 20, maxval = 1500, group = G_TL)
int    extBars    = input.int(20,  "Projection (bars ahead)",      minval = 1,  maxval = 200,  group = G_TL)
int    keepBroken = input.int(2,   "Keep Broken Lines (per side)", minval = 0,  maxval = 10,   group = G_TL)
string breakMode  = input.string("Close", "Break / Validation Source", options = ["Close", "Wick"], group = G_TL, tooltip = "Close: a line is valid until a candle CLOSES through it. Wick: any wick through it counts (stricter, fewer lines).")
bool   showBrkLbl = input.bool(true, "Breakout / Breakdown Markers", group = G_TL)
 
string G_SR = "③ Support / Resistance"
bool  showRange    = input.bool(true, "Range Top / Bottom Zones", group = G_SR)
float zoneATR      = input.float(0.6, "Zone Thickness (x ATR)", minval = 0.1, step = 0.1, group = G_SR)
bool  showClusters = input.bool(true, "Pivot-Cluster S/R Zones", group = G_SR, tooltip = "Groups swing highs/lows that sit at the same price into zones and ranks them by number of touches.")
float clusterTol   = input.float(0.5, "Cluster Tolerance (x ATR)", minval = 0.1, step = 0.1, group = G_SR)
int   minTouches   = input.int(3, "Min Touches for a Zone", minval = 2, maxval = 10, group = G_SR)
int   maxZones     = input.int(2, "Max Cluster Zones (per side)", minval = 1, maxval = 6, group = G_SR)
 
string G_FIB = "④ Fibonacci"
bool   showFib   = input.bool(true, "Show Fibonacci", group = G_FIB)
string fibDir    = input.string("Auto", "Direction", options = ["Auto", "Top → Down", "Bottom → Up"], group = G_FIB, tooltip = "Auto: measures from whichever extreme (Top or Bottom) printed most recently.")
bool   showGP    = input.bool(true, "Golden Pocket (61.8–65%)", group = G_FIB)
bool   showExt   = input.bool(true, "Extensions (127.2 / 161.8%)", group = G_FIB)
int    fibLblOff = input.int(35, "Fib Label Offset (bars)", minval = 5, maxval = 200, group = G_FIB)
 
string G_STY = "⑤ Neon Style & 3D"
color cRes   = input.color(#FF073A, "Range Resistance",   inline = "c1", group = G_STY)
color cSup   = input.color(#39FF14, "Range Support",      inline = "c1", group = G_STY)
color cResZ  = input.color(#FF9F00, "Cluster Resistance", inline = "c2", group = G_STY)
color cSupZ  = input.color(#1F51FF, "Cluster Support",    inline = "c2", group = G_STY)
color cDn    = input.color(#FF10F0, "Down Trendline",     inline = "c3", group = G_STY)
color cUp    = input.color(#00F0FF, "Up Trendline",       inline = "c3", group = G_STY)
color cFib   = input.color(#FFF01F, "Fib Levels",         inline = "c4", group = G_STY)
color cGP    = input.color(#BC13FE, "Golden Pocket",      inline = "c4", group = G_STY)
color cExt   = input.color(#CCFF00, "Extensions",         inline = "c5", group = G_STY)
bool  use3D  = input.bool(true, "3D Depth Effect", group = G_STY)
int   depth  = input.int(3, "3D Depth (bars)", minval = 1, maxval = 20, group = G_STY)
bool  glowOn = input.bool(true, "Neon Glow", group = G_STY)
int   zoneFillT  = input.int(78, "Zone Fill Transparency", minval = 0, maxval = 100, group = G_STY)
string lblSizeIn = input.string("Large",  "Label Size",       options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_STY)
string zoneSizeIn= input.string("Large",  "Zone Text Size",   options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_STY)
string markSizeIn= input.string("Normal", "Marker Size",      options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_STY)
 
string G_DB = "⑥ Dashboard"
bool   showDash  = input.bool(true, "Show Dashboard", group = G_DB)
string dashPos   = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Middle Right", "Bottom Right", "Bottom Left"], group = G_DB)
string tblSizeIn = input.string("Large", "Text Size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_DB)
 
string G_AL = "⑦ Alerts"
bool aTL  = input.bool(true, "Trendline Breaks",            group = G_AL)
bool aSR  = input.bool(true, "S/R Touches & Breaks",        group = G_AL)
bool aFib = input.bool(true, "Fibonacci Level Crosses",     group = G_AL)
bool useAlertFn = input.bool(true, "Send Dynamic alert() Messages", group = G_AL, tooltip = "Create ONE alert using 'Any alert() function call' to receive every enabled event with full detail.")
 
// ═════════════════════════════ HELPERS ═════════════════════════════
f_size(string s) =>
    switch s
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        "Huge"   => size.huge
        => size.normal
 
f_pos(string s) =>
    switch s
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Middle Right" => position.middle_right
        "Bottom Right" => position.bottom_right
        "Bottom Left"  => position.bottom_left
        => position.top_right
 
// Darker solid shade of a neon (used for 3D extrusion faces — no gradients)
f_shade(color c, float k, int t) => color.rgb(color.r(c) * k, color.g(c) * k, color.b(c) * k, t)
// Black or white text depending on how bright the neon is
f_txt(color c) => (0.299 * color.r(c) + 0.587 * color.g(c) + 0.114 * color.b(c)) > 140 ? color.black : color.white
f_px(float v)  => na(v) ? "—" : str.tostring(v, format.mintick)
f_pct(float a, float b) => (a - b) / b * 100.0
f_sgn(float v) => na(v) ? "—" : (v >= 0 ? "+" : "") + str.tostring(v, "0.00") + "%"
 
string lblSize  = f_size(lblSizeIn)
string zoneSize = f_size(zoneSizeIn)
string markSize = f_size(markSizeIn)
string tblSize  = f_size(tblSizeIn)
bool   wickMode = breakMode == "Wick"
 
// ═════════════════════════════ CORE SERIES ═════════════════════════════
float atrv = ta.atr(14)
float ph   = ta.pivothigh(high, pivLen, pivLen)
float pl   = ta.pivotlow(low, pivLen, pivLen)
 
float rTop  = ta.highest(high, rangeLen)
float rBot  = ta.lowest(low, rangeLen)
int   rTopX = bar_index + ta.highestbars(high, rangeLen)
int   rBotX = bar_index + ta.lowestbars(low, rangeLen)
float rng   = rTop - rBot
float zTh   = atrv * zoneATR
bool  rangeOk = not na(rTop) and not na(rBot) and not na(atrv) and rng > 0
 
bool fromTop = fibDir == "Auto" ? rTopX >= rBotX : fibDir == "Top → Down"
 
f_fibAt(float r, float t, float b, bool ft) => ft ? t - (t - b) * r : b + (t - b) * r
f_fib(float r) => f_fibAt(r, rTop, rBot, fromTop)
 
// ═════════════════════════════ TRENDLINE ENGINE ═════════════════════════════
type TLine
    int   x1
    float y1
    int   x2
    float y2
    bool  isDown
    float sOff
    line  core
    line  ext
    line  glowLn
    line  shadow
    label lbl
 
method val(TLine t, int x) => t.y1 + (t.y2 - t.y1) / (t.x2 - t.x1) * (x - t.x1)
 
method kill(TLine t) =>
    line.delete(t.core)
    line.delete(t.ext)
    line.delete(t.glowLn)
    line.delete(t.shadow)
    label.delete(t.lbl)
 
method finalize(TLine t, float v) =>
    color base = t.isDown ? cDn : cUp
    color c    = color.new(base, 55)
    t.ext.set_xy2(bar_index, v)
    t.ext.set_style(line.style_dotted)
    t.ext.set_color(c)
    t.core.set_color(c)
    if not na(t.glowLn)
        t.glowLn.set_xy2(bar_index, v)
        t.glowLn.set_color(color.new(base, 92))
    if not na(t.shadow)
        t.shadow.set_xy2(bar_index + depth, v - t.sOff)
    label.delete(t.lbl)
    t.lbl := label(na)
 
method refresh(TLine t, int xe) =>
    float ye   = t.val(xe)
    float vNow = t.val(bar_index)
    t.ext.set_xy2(xe, ye)
    if not na(t.glowLn)
        t.glowLn.set_xy2(xe, ye)
    if not na(t.shadow)
        t.shadow.set_xy2(xe + depth, ye - t.sOff)
    if not na(t.lbl)
        t.lbl.set_xy(xe, ye)
        t.lbl.set_text((t.isDown ? "◄ DOWN TREND  " : "◄ UP TREND  ") + f_px(vNow) + "  |  " + f_sgn(f_pct(vNow, close)))
 
// A line is valid if no candle (close or wick, per setting) pierced it from anchor 1 up to the current bar
f_valid(int x1, float y1, int x2, float y2, bool isDown) =>
    float m  = (y2 - y1) / (x2 - x1)
    bool  ok = true
    for b = x1 + 1 to bar_index
        if b != x2
            int   ago = bar_index - b
            float v   = y1 + m * (b - x1)
            float s   = isDown ? (wickMode ? high[ago] : close[ago]) : (wickMode ? low[ago] : close[ago])
            if isDown ? s > v : s < v
                ok := false
                break
    ok
 
f_newTL(int x1, float y1, int x2, float y2, bool isDown) =>
    color c  = isDown ? cDn : cUp
    int   xe = bar_index + extBars
    TLine t  = TLine.new(x1, y1, x2, y2, isDown, nz(atrv) * 0.25)
    float ye = t.val(xe)
    if use3D
        t.shadow := line.new(x1 + depth, y1 - t.sOff, xe + depth, ye - t.sOff, color = f_shade(c, 0.35, 25), width = 3)
    if glowOn
        t.glowLn := line.new(x1, y1, xe, ye, color = color.new(c, 80), width = 8)
    t.core := line.new(x1, y1, x2, y2, color = c, width = 2)
    t.ext  := line.new(x2, y2, xe, ye, color = c, width = 2, style = line.style_dashed)
    t.lbl  := label.new(xe, ye, "", style = label.style_label_left, color = c, textcolor = f_txt(c), size = lblSize)
    t
 
var array<float> phP = array.new<float>()
var array<int>   phB = array.new<int>()
var array<float> plP = array.new<float>()
var array<int>   plB = array.new<int>()
var array<TLine> actDn = array.new<TLine>()
var array<TLine> actUp = array.new<TLine>()
var array<TLine> brkDn = array.new<TLine>()
var array<TLine> brkUp = array.new<TLine>()
var array<label> marks = array.new<label>()
int MAXPIV = 150
 
f_mark(bool up, string txt, color c) =>
    marks.push(label.new(bar_index, up ? low : high, txt, yloc = up ? yloc.belowbar : yloc.abovebar, style = up ? label.style_label_up : label.style_label_down, color = c, textcolor = f_txt(c), size = markSize))
    if marks.size() > 60
        marks.shift().delete()
 
// ── New swing high → try to build a DOWN trendline
if not na(ph) and barstate.isconfirmed
    phP.push(ph)
    phB.push(bar_index - pivLen)
    if phP.size() > MAXPIV
        phP.shift()
        phB.shift()
    if showDn and phP.size() >= 2
        int   x2 = bar_index - pivLen
        float y2 = ph
        int   lo = math.max(0, phP.size() - 1 - pivScan)
        for i = phP.size() - 2 to lo
            int   x1 = phB.get(i)
            float y1 = phP.get(i)
            if x2 - x1 > maxSpan
                break
            if y1 > y2 and f_valid(x1, y1, x2, y2, true)
                actDn.push(f_newTL(x1, y1, x2, y2, true))
                if actDn.size() > maxTL
                    actDn.shift().kill()
                break
 
// ── New swing low → try to build an UP trendline
if not na(pl) and barstate.isconfirmed
    plP.push(pl)
    plB.push(bar_index - pivLen)
    if plP.size() > MAXPIV
        plP.shift()
        plB.shift()
    if showUp and plP.size() >= 2
        int   x2 = bar_index - pivLen
        float y2 = pl
        int   lo = math.max(0, plP.size() - 1 - pivScan)
        for i = plP.size() - 2 to lo
            int   x1 = plB.get(i)
            float y1 = plP.get(i)
            if x2 - x1 > maxSpan
                break
            if y1 < y2 and f_valid(x1, y1, x2, y2, false)
                actUp.push(f_newTL(x1, y1, x2, y2, false))
                if actUp.size() > maxTL
                    actUp.shift().kill()
                break
 
// ── Break detection (confirmed bars only → no repainting of breaks)
bool  evDnBreak = false
bool  evUpBreak = false
float dnBrkLvl  = na
float upBrkLvl  = na
 
if actDn.size() > 0 and barstate.isconfirmed
    for i = actDn.size() - 1 to 0
        TLine t = actDn.get(i)
        float v = t.val(bar_index)
        if (wickMode ? high : close) > v
            evDnBreak := true
            dnBrkLvl  := v
            t.finalize(v)
            actDn.remove(i)
            brkDn.push(t)
            if brkDn.size() > keepBroken
                brkDn.shift().kill()
 
if actUp.size() > 0 and barstate.isconfirmed
    for i = actUp.size() - 1 to 0
        TLine t = actUp.get(i)
        float v = t.val(bar_index)
        if (wickMode ? low : close) < v
            evUpBreak := true
            upBrkLvl  := v
            t.finalize(v)
            actUp.remove(i)
            brkUp.push(t)
            if brkUp.size() > keepBroken
                brkUp.shift().kill()
 
if showBrkLbl and evDnBreak
    f_mark(true, "▲ TL BREAKOUT", cUp)
if showBrkLbl and evUpBreak
    f_mark(false, "▼ TL BREAKDOWN", cDn)
 
// ═════════════════════════════ S/R + FIB EVENTS ═════════════════════════════
bool xResBrk = ta.crossover(close, rTop[1])
bool xSupBrk = ta.crossunder(close, rBot[1])
bool xResTch = ta.crossover(high, rTop[1] - zTh[1])
bool xSupTch = ta.crossunder(low, rBot[1] + zTh[1])
 
bool evResBreak = rangeOk and xResBrk and barstate.isconfirmed
bool evSupBreak = rangeOk and xSupBrk and barstate.isconfirmed
bool evResTouch = rangeOk and xResTch and close < rTop[1] and barstate.isconfirmed
bool evSupTouch = rangeOk and xSupTch and close > rBot[1] and barstate.isconfirmed
 
if showBrkLbl and evResBreak
    f_mark(true, "▲ RANGE BREAKOUT", cRes)
if showBrkLbl and evSupBreak
    f_mark(false, "▼ RANGE BREAKDOWN", cSup)
 
bool   evFib  = false
string fibMsg = ""
if rangeOk and showFib and barstate.isconfirmed and not na(rTop[1])
    array<float> rsx = array.from(0.236, 0.382, 0.5, 0.618, 0.786)
    if showExt
        rsx.push(1.272)
        rsx.push(1.618)
    for r in rsx
        float lv  = f_fib(r)
        float lvP = f_fibAt(r, rTop[1], rBot[1], fromTop[1])
        if close[1] < lvP and close >= lv
            evFib  := true
            fibMsg := "crossed ABOVE Fib " + str.tostring(r * 100, "0.0") + "% (" + f_px(lv) + ")"
        else if close[1] > lvP and close <= lv
            evFib  := true
            fibMsg := "crossed BELOW Fib " + str.tostring(r * 100, "0.0") + "% (" + f_px(lv) + ")"
 
// ═════════════════════════════ LAST-BAR DRAWING ═════════════════════════════
var array<box>   dBoxes  = array.new<box>()
var array<line>  dLines  = array.new<line>()
var array<label> dLabels = array.new<label>()
 
f_clear() =>
    while dBoxes.size() > 0
        dBoxes.pop().delete()
    while dLines.size() > 0
        dLines.pop().delete()
    while dLabels.size() > 0
        dLabels.pop().delete()
 
// Neon zone with faux-3D extrusion (offset back face + connecting edges), glow and bevel line
f_zone(int x1, int x2, float zt, float zb, color c, string txt) =>
    if use3D
        float dz = math.max(zt - zb, atrv * 0.2) * 0.45
        color sh = f_shade(c, 0.30, 20)
        dBoxes.push(box.new(x1 + depth, zt - dz, x2 + depth, zb - dz, border_color = sh, border_width = 1, bgcolor = f_shade(c, 0.30, 40)))
        dLines.push(line.new(x2, zt, x2 + depth, zt - dz, color = f_shade(c, 0.6, 0), width = 1))
        dLines.push(line.new(x2, zb, x2 + depth, zb - dz, color = f_shade(c, 0.6, 0), width = 1))
        dLines.push(line.new(x1, zb, x1 + depth, zb - dz, color = f_shade(c, 0.6, 0), width = 1))
    dBoxes.push(box.new(x1, zt, x2, zb, border_color = c, border_width = 2, bgcolor = color.new(c, zoneFillT)))
    if glowOn
        dLines.push(line.new(x1, zt, x2, zt, color = color.new(c, 80), width = 7))
    dLines.push(line.new(x1, zt, x2, zt, color = c, width = 3))
    if txt != ""
        dLabels.push(label.new(math.round((x1 + x2) / 2), (zt + zb) / 2, txt, style = label.style_label_center, color = c, textcolor = f_txt(c), size = zoneSize))
 
f_level(float lv, color c, string txt, int x0, int x1, bool solid) =>
    if use3D
        dLines.push(line.new(x0 + depth, lv - atrv * 0.12, x1 + depth, lv - atrv * 0.12, color = f_shade(c, 0.35, 30), width = 2))
    if glowOn
        dLines.push(line.new(x0, lv, x1, lv, color = color.new(c, 85), width = 6))
    dLines.push(line.new(x0, lv, x1, lv, color = c, width = 2, style = solid ? line.style_solid : line.style_dashed))
    dLabels.push(label.new(x1, lv, txt, style = label.style_label_left, color = c, textcolor = f_txt(c), size = lblSize))
 
f_collect(array<float> P, array<int> B, array<float> outP, array<int> outB) =>
    if P.size() > 0
        for i = 0 to P.size() - 1
            if bar_index - B.get(i) <= rangeLen
                outP.push(P.get(i))
                outB.push(B.get(i))
 
if barstate.islast
    f_clear()
    int xR  = bar_index + extBars
    int xF1 = bar_index + extBars + fibLblOff
 
    // Live trendline projections + labels
    for t in actDn
        t.refresh(xR)
    for t in actUp
        t.refresh(xR)
 
    if rangeOk
        // ── Range Top / Bottom zones
        if showRange
            f_zone(rTopX, xR, rTop, rTop - zTh, cRes, "RESISTANCE  " + f_px(rTop) + "  |  " + f_sgn(f_pct(rTop, close)))
            f_zone(rBotX, xR, rBot + zTh, rBot, cSup, "SUPPORT  " + f_px(rBot) + "  |  " + f_sgn(f_pct(rBot, close)))
 
        // ── Fibonacci
        if showFib
            int xF0 = math.max(rTopX, rBotX)
            f_level(rTop, cRes, "TOP  " + f_px(rTop), rTopX, xF1, true)
            f_level(rBot, cSup, "BOTTOM  " + f_px(rBot), rBotX, xF1, true)
            for r in array.from(0.236, 0.382, 0.5, 0.618, 0.786)
                float lv = f_fib(r)
                f_level(lv, cFib, str.tostring(r * 100, "0.0") + "%   " + f_px(lv), xF0, xF1, false)
            if showExt
                for r in array.from(1.272, 1.618)
                    float lv = f_fib(r)
                    f_level(lv, cExt, str.tostring(r * 100, "0.0") + "% EXT   " + f_px(lv), xF0, xF1, false)
            if showGP
                float g1 = f_fib(0.618)
                float g2 = f_fib(0.65)
                f_zone(xF0, xF1, math.max(g1, g2), math.min(g1, g2), cGP, "GOLDEN POCKET")
 
        // ── Pivot-cluster S/R zones ranked by touches
        if showClusters
            array<float> pp = array.new<float>()
            array<int>   pb = array.new<int>()
            f_collect(phP, phB, pp, pb)
            f_collect(plP, plB, pp, pb)
            if pp.size() > 0
                array<int>   idx = pp.sort_indices()
                array<float> zL  = array.new<float>()
                array<float> zH  = array.new<float>()
                array<int>   zC  = array.new<int>()
                array<int>   zX  = array.new<int>()
                float tol = atrv * clusterTol
                float cL  = na
                float cH  = na
                int   cN  = 0
                int   cX  = na
                for k = 0 to idx.size() - 1
                    int   j  = idx.get(k)
                    float p  = pp.get(j)
                    int   bx = pb.get(j)
                    if na(cL)
                        cL := p
                        cH := p
                        cN := 1
                        cX := bx
                    else if p - cL <= tol * 2
                        cH := p
                        cN += 1
                        cX := math.min(cX, bx)
                    else
                        if cN >= minTouches
                            zL.push(cL)
                            zH.push(cH)
                            zC.push(cN)
                            zX.push(cX)
                        cL := p
                        cH := p
                        cN := 1
                        cX := bx
                if not na(cL) and cN >= minTouches
                    zL.push(cL)
                    zH.push(cH)
                    zC.push(cN)
                    zX.push(cX)
 
                if zL.size() > 0
                    array<int> used = array.new<int>()
                    for side = 0 to 1
                        int taken = 0
                        while taken < maxZones
                            int best  = -1
                            int bestN = 0
                            for z = 0 to zL.size() - 1
                                float mid     = (zL.get(z) + zH.get(z)) / 2
                                bool  sideOk  = side == 0 ? mid > close : mid <= close
                                bool  overlap = zH.get(z) >= rTop - zTh or zL.get(z) <= rBot + zTh
                                if sideOk and not overlap and not used.includes(z) and zC.get(z) > bestN
                                    best  := z
                                    bestN := zC.get(z)
                            if best < 0
                                break
                            used.push(best)
                            taken += 1
                            float pad = atrv * 0.15
                            float zt  = zH.get(best) + pad
                            float zb  = zL.get(best) - pad
                            color zc  = side == 0 ? cResZ : cSupZ
                            string nm = side == 0 ? "R ZONE" : "S ZONE"
                            f_zone(zX.get(best), xR, zt, zb, zc, nm + "  •  " + str.tostring(bestN) + " TOUCHES")
 
// ═════════════════════════════ DASHBOARD ═════════════════════════════
var table dash = showDash ? table.new(f_pos(dashPos), 2, 10, bgcolor = #07070D, frame_color = cUp, frame_width = 2, border_color = #1C1C33, border_width = 1) : table(na)
 
f_row(int r, string k, string v, color vc) =>
    table.cell(dash, 0, r, k, text_color = #C8C8E0, text_size = tblSize, text_halign = text.align_left)
    table.cell(dash, 1, r, v, text_color = vc, text_size = tblSize, text_halign = text.align_right)
 
if showDash and barstate.isfirst
    table.merge_cells(dash, 0, 0, 1, 0)
 
if showDash and barstate.islast
    table.cell(dash, 0, 0, "◆ NEBLOK  •  TL / S/R / FIB", text_color = cUp, bgcolor = #10102A, text_size = tblSize)
 
    // Market structure from the last two swing highs / lows
    string bias  = "—"
    color  biasC = color.gray
    if phP.size() >= 2 and plP.size() >= 2
        bool hh = phP.get(phP.size() - 1) > phP.get(phP.size() - 2)
        bool hl = plP.get(plP.size() - 1) > plP.get(plP.size() - 2)
        bias  := hh and hl ? "BULLISH (HH+HL)" : not hh and not hl ? "BEARISH (LH+LL)" : "MIXED"
        biasC := hh and hl ? cSup : not hh and not hl ? cRes : cFib
 
    // Nearest active trendlines
    float nDn = na
    for t in actDn
        float v = t.val(bar_index)
        if na(nDn) or math.abs(v - close) < math.abs(nDn - close)
            nDn := v
    float nUp = na
    for t in actUp
        float v = t.val(bar_index)
        if na(nUp) or math.abs(v - close) < math.abs(nUp - close)
            nUp := v
 
    // Which Fib band price sits in
    string fibZone = "—"
    float  rPos    = na
    if rangeOk
        rPos := (close - rBot) / rng * 100
        array<float>  rr = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0)
        float  aV = na
        string aN = ""
        float  bV = na
        string bN = ""
        for r in rr
            float lv = f_fib(r)
            string nmr = str.tostring(r * 100, "0.0") + "%"
            if lv >= close and (na(aV) or lv < aV)
                aV := lv
                aN := nmr
            if lv < close and (na(bV) or lv > bV)
                bV := lv
                bN := nmr
        fibZone := na(aV) ? "ABOVE RANGE" : na(bV) ? "BELOW RANGE" : bN + " ↔ " + aN
 
    f_row(1, "Structure", bias, biasC)
    f_row(2, "Price", f_px(close), color.white)
    f_row(3, "Resistance", f_px(rTop) + "  " + f_sgn(f_pct(rTop, close)), cRes)
    f_row(4, "Support", f_px(rBot) + "  " + f_sgn(f_pct(rBot, close)), cSup)
    f_row(5, "Down TL", na(nDn) ? "none" : f_px(nDn) + "  " + f_sgn(f_pct(nDn, close)), cDn)
    f_row(6, "Up TL", na(nUp) ? "none" : f_px(nUp) + "  " + f_sgn(f_pct(nUp, close)), cUp)
    f_row(7, "Fib Zone", fibZone, cFib)
    f_row(8, "Range Position", na(rPos) ? "—" : str.tostring(rPos, "0.0") + "%", cGP)
    f_row(9, "ATR(14)", f_px(atrv), cExt)
 
// ═════════════════════════════ ALERTS ═════════════════════════════
alertcondition(aTL and evDnBreak,  "Down Trendline Breakout (Bullish)", "{{ticker}} {{interval}}: closed ABOVE a down trendline at {{close}} [Neblok]")
alertcondition(aTL and evUpBreak,  "Up Trendline Breakdown (Bearish)",  "{{ticker}} {{interval}}: closed BELOW an up trendline at {{close}} [Neblok]")
alertcondition(aSR and evResBreak, "Range Resistance Breakout",         "{{ticker}} {{interval}}: broke ABOVE range resistance at {{close}} [Neblok]")
alertcondition(aSR and evSupBreak, "Range Support Breakdown",           "{{ticker}} {{interval}}: broke BELOW range support at {{close}} [Neblok]")
alertcondition(aSR and evResTouch, "Resistance Zone Touch",             "{{ticker}} {{interval}}: tagged the resistance zone and closed below it [Neblok]")
alertcondition(aSR and evSupTouch, "Support Zone Touch",                "{{ticker}} {{interval}}: tagged the support zone and closed above it [Neblok]")
alertcondition(aFib and evFib,     "Fibonacci Level Cross",             "{{ticker}} {{interval}}: crossed a Fibonacci level at {{close}} [Neblok]")
alertcondition((aTL and (evDnBreak or evUpBreak)) or (aSR and (evResBreak or evSupBreak or evResTouch or evSupTouch)) or (aFib and evFib), "Any Neblok Event", "{{ticker}} {{interval}}: Neblok TL/SR/Fib event at {{close}}")
 
if useAlertFn
    string sym = syminfo.ticker + " [" + timeframe.period + "] "
    if aTL and evDnBreak
        alert(sym + "▲ Down-trendline BREAKOUT @ " + f_px(close) + " (line " + f_px(dnBrkLvl) + ") • Neblok", alert.freq_once_per_bar_close)
    if aTL and evUpBreak
        alert(sym + "▼ Up-trendline BREAKDOWN @ " + f_px(close) + " (line " + f_px(upBrkLvl) + ") • Neblok", alert.freq_once_per_bar_close)
    if aSR and evResBreak
        alert(sym + "▲ RANGE BREAKOUT above " + f_px(rTop[1]) + " @ " + f_px(close) + " • Neblok", alert.freq_once_per_bar_close)
    if aSR and evSupBreak
        alert(sym + "▼ RANGE BREAKDOWN below " + f_px(rBot[1]) + " @ " + f_px(close) + " • Neblok", alert.freq_once_per_bar_close)
    if aSR and evResTouch
        alert(sym + "Resistance zone touch (" + f_px(rTop[1]) + "), closed below @ " + f_px(close) + " • Neblok", alert.freq_once_per_bar_close)
    if aSR and evSupTouch
        alert(sym + "Support zone touch (" + f_px(rBot[1]) + "), closed above @ " + f_px(close) + " • Neblok", alert.freq_once_per_bar_close)
    if aFib and evFib
        alert(sym + fibMsg + " @ " + f_px(close) + " • Neblok", alert.freq_once_per_bar_close)
 
// Data-window values (hidden on chart)
plot(rangeOk ? rTop : na, "Range Top",    color = cRes, display = display.data_window)
plot(rangeOk ? rBot : na, "Range Bottom", color = cSup, display = display.data_window)

← 返回列表