Auto Levels & Trendlines + Breakout Check

Pur-Montage · study · 660 行 · 点赞 591 · TradingView 原页

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

Pine Script

//@version=6
indicator("Auto Levels & Trendlines + Breakout Check", shorttitle="AutoLevels", overlay=true,
     max_boxes_count=500, max_lines_count=500, max_labels_count=500, max_bars_back=1000)
 
// =====================================================================
// Automatic levels and trendlines with breakout validation.
//
// HORIZONTAL LEVELS
//  - Candidates come from pivots (highs and lows by wicks).
//  - Nearby pivots are merged into a ZONE (tolerance and thickness in ATR).
//  - A touch = a wick enters the zone, the candle closes on its "own" side,
//    with at least N bars between touches. Levels with touch count >= the
//    selected minimum are shown. Broken levels stay on the chart in gray.
//
// TREND LINES (sloped)
//  - Built from two pivots; no wick between them may pierce the line
//    beyond the tolerance. Touches are counted by wicks.
//  - Support (by lows) and resistance (by highs), rising and falling.
//    Lines with touch count >= the selected minimum are shown.
//
// BREAKOUT AND CONFIRMATION (same for levels and lines)
//  1) Breakout: a close beyond the boundary by the buffer amount (in ATR).
//  2) TRUE: price held beyond the boundary for the given number of
//     consecutive closes (and, if enabled, the breakout candle had
//     elevated volume).
//  3) FALSE: within the confirmation window price closed back across the boundary.
//  4) Additionally: retest. Price returned to the boundary and closed beyond
//     it again = "confirmed by retest"; closed back = "failed".
// This is a heuristic, not a guarantee: false and true breakouts are
// determined only by past bars. An analysis tool, not a recommendation.
// =====================================================================
 
// ---------- Types ----------
type Level
    float price = na
    float top = na
    float bot = na
    int side = 1
    int touches = 0
    int lastTouch = 0
    int born = 0
    bool broken = false
    box bx = na
 
type TLine
    int x1 = 0
    float y1 = na
    float slope = 0.0
    bool isHigh = false
    int touches = 0
    int lastT = 0
    bool broken = false
    line ln = na
    label lb = na
 
type Brk
    int kind = 0
    int dir = 1
    float ref = na
    TLine tl = na
    int bar = 0
    int state = 1
    int held = 1
    int retestLeft = 0
    bool volOk = false
    float volRatio = na
    string name = ""
 
// ---------- Inputs ----------
gC = "General"
swingLen = input.int(5, "Pivot length (bars on each side)", minval=2, group=gC)
atrLen   = input.int(14, "ATR length", minval=1, group=gC)
minGap   = input.int(5, "Min. bars between touches", minval=1, group=gC)
 
gL = "Horizontal levels"
showLv      = input.bool(true, "Show levels", group=gL)
minTouchLv  = input.int(3, "Minimum level touches", minval=2, maxval=15, group=gL)
tolAtr      = input.float(0.35, "Touch and merge tolerance (x ATR)", minval=0.05, step=0.05, group=gL)
padAtr      = input.float(0.10, "Zone padding around wicks (x ATR)", minval=0.0, step=0.05, group=gL)
maxWidthAtr = input.float(1.5, "Max. zone thickness (x ATR)", minval=0.2, step=0.1, group=gL)
maxLv       = input.int(20, "Max. levels on chart", minval=3, maxval=100, group=gL)
cRes        = input.color(#ef5350, "Resistance color", group=gL)
cSup        = input.color(#26a69a, "Support color", group=gL)
 
gT = "Trend lines"
showTl      = input.bool(true, "Show trend lines", group=gT)
minTouchTl  = input.int(3, "Minimum line touches", minval=2, maxval=15, group=gT)
tlTolAtr    = input.float(0.25, "Touch tolerance (x ATR)", minval=0.05, step=0.05, group=gT)
maxCand     = input.int(10, "How many past pivots to check", minval=2, maxval=30, group=gT)
maxTlBars   = input.int(300, "Max. line length (bars)", minval=20, maxval=900, group=gT)
minMoveAtr  = input.float(0.5, "Min. difference between points (x ATR)", minval=0.0, step=0.1, group=gT)
allowCnt    = input.bool(false, "Counter-trend lines (rising resistance, falling support)", group=gT)
maxTl       = input.int(6, "Max. lines on chart", minval=1, maxval=30, group=gT)
cTlRes      = input.color(#ffa726, "Resistance line color", group=gT)
cTlSup      = input.color(#42a5f5, "Support line color", group=gT)
 
gB = "Breakout and confirmation"
brkAtr     = input.float(0.15, "Breakout buffer: close beyond boundary (x ATR)", minval=0.0, step=0.05, group=gB)
confBars   = input.int(3, "Confirmation: closes beyond boundary (including the breakout close)", minval=1, maxval=10, group=gB)
needVol    = input.bool(false, "Require elevated volume on the breakout candle", group=gB)
volMult    = input.float(1.5, "Breakout volume (x of 20-bar average)", minval=1.0, step=0.1, group=gB)
useRetest  = input.bool(true, "Watch for a retest after confirmation", group=gB)
retestBars = input.int(10, "Retest window (bars)", minval=1, maxval=50, group=gB)
 
gV = "Table"
showTbl = input.bool(true, "Show table", group=gV)
posIn   = input.string("Top right", "Position", options=["Top right", "Bottom right", "Top left", "Bottom left"], group=gV)
szIn    = input.string("Small", "Text size", options=["Tiny", "Small", "Normal"], group=gV)
 
// ---------- Helper functions ----------
f_num(float x, string fmt) =>
    na(x) ? "—" : str.tostring(x, fmt)
 
f_sgn(float x, string fmt) =>
    na(x) ? "—" : (x >= 0 ? "+" : "") + str.tostring(x, fmt)
 
f_row(table t, int r, string k, string v, color vc, string sz, string tip) =>
    table.cell(t, 0, r, k, text_color=color.new(color.white, 25), text_halign=text.align_left,
         text_size=sz, bgcolor=color.new(#10151f, 8), tooltip=tip)
    table.cell(t, 1, r, v, text_color=vc, text_halign=text.align_left,
         text_size=sz, bgcolor=color.new(#10151f, 8), tooltip=tip)
 
f_alpha(int touches) =>
    math.max(55, 88 - touches * 6)
 
f_stTxt(int st, int dir, int held, int need) =>
    string a = dir == 1 ? "▲" : "▼"
    string s = ""
    if st == 1
        s := "Breakout " + a + " · awaiting confirmation (" + str.tostring(math.min(held, need)) + "/" + str.tostring(need) + ")"
    else if st == 2
        s := "TRUE breakout " + a
    else if st == 3
        s := "FALSE breakout " + a + " (return beyond boundary)"
    else if st == 4
        s := "Breakout " + a + " confirmed by retest"
    else if st == 5
        s := "Breakout " + a + " failed after confirmation"
    else if st == 6
        s := "Doubtful breakout " + a + " (low volume)"
    s
 
f_stShort(int st) =>
    string s = "⚡ breakout"
    if st == 2
        s := "✓ true"
    else if st == 3
        s := "✗ false"
    else if st == 4
        s := "✓ retest"
    else if st == 5
        s := "✗ failed"
    else if st == 6
        s := "? low volume"
    s
 
f_stCol(int st) =>
    (st == 2 or st == 4) ? #4caf50 : (st == 3 or st == 5) ? #ef5350 : #ffca28
 
f_evLabel(bool up, string txt, color c) =>
    label.new(bar_index, up ? low : high, txt, style=up ? label.style_label_up : label.style_label_down,
         color=color.new(c, 15), textcolor=color.white, size=size.small)
    0
 
f_trimLv(array<Level> arr, int maxN) =>
    while arr.size() > maxN
        Level old = arr.shift()
        if not na(old.bx)
            box.delete(old.bx)
    0
 
f_addPivot(array<Level> arr, float p, int pivBar, float atrV, float tolA, float padA, float maxW) =>
    float tol = tolA * atrV
    float pad = padA * atrV
    bool merged = false
    for lv in arr
        if not lv.broken and p >= lv.bot - tol and p <= lv.top + tol
            float nt = math.max(lv.top, p + pad)
            float nb = math.min(lv.bot, p - pad)
            if nt - nb <= maxW * atrV
                lv.top := nt
                lv.bot := nb
                lv.price := (nt + nb) / 2
                merged := true
                break
    if not merged
        Level n = Level.new()
        n.price := p
        n.top := p + pad
        n.bot := p - pad
        n.side := close > p ? 1 : -1
        n.touches := 1
        n.lastTouch := pivBar
        n.born := pivBar
        arr.push(n)
    0
 
f_tlEval(bool isHigh, int xA, float yA, int xB, float yB, float tol, int gap) =>
    float slope = (yB - yA) / (xB - xA)
    int n = 0
    int lastT = xA - gap
    bool ok = true
    for x = xA to xB
        int off = bar_index - x
        float yl = yA + slope * (x - xA)
        if isHigh
            if high[off] > yl + tol
                ok := false
                break
            if high[off] >= yl - tol and x - lastT >= gap
                n += 1
                lastT := x
        else
            if low[off] < yl - tol
                ok := false
                break
            if low[off] <= yl + tol and x - lastT >= gap
                n += 1
                lastT := x
    [ok, n, slope]
 
f_tryLine(array<TLine> tls, array<float> pY, array<int> pX, bool isHigh, float atrV, float tolA, int minT, int maxC, int maxBars, bool counter, float minMove, int gap, int maxN, color colr) =>
    int sz = pY.size()
    if sz >= 2
        int xB = pX.get(sz - 1)
        float yB = pY.get(sz - 1)
        float tol = tolA * atrV
        bool dup = false
        for t in tls
            if not t.broken and t.isHigh == isHigh
                float yl = t.y1 + t.slope * (xB - t.x1)
                if math.abs(yl - yB) <= tol
                    dup := true
                    break
        if not dup
            int bestN = 0
            int bestA = -1
            float bestSlope = 0.0
            int lowK = math.max(0, sz - 1 - maxC)
            for k = sz - 2 to lowK
                int xA = pX.get(k)
                float yA = pY.get(k)
                if xB - xA <= maxBars and math.abs(yB - yA) >= minMove * atrV
                    [ok, n, sl] = f_tlEval(isHigh, xA, yA, xB, yB, tol, gap)
                    bool dirOk = counter or (isHigh ? sl < 0 : sl > 0)
                    bool better = n > bestN
                    if not better and n == bestN and bestA >= 0
                        better := (xB - xA) > (xB - pX.get(bestA))
                    if ok and dirOk and n >= minT and better
                        bestN := n
                        bestA := k
                        bestSlope := sl
            if bestA >= 0
                int xA2 = pX.get(bestA)
                float yA2 = pY.get(bestA)
                TLine nt = TLine.new()
                nt.x1 := xA2
                nt.y1 := yA2
                nt.slope := bestSlope
                nt.isHigh := isHigh
                nt.touches := bestN
                nt.lastT := xB
                nt.ln := line.new(xA2, yA2, xB, yB, extend=extend.right, color=colr, width=2, style=line.style_solid)
                nt.lb := label.new(xB, yB, (bestSlope < 0 ? "↘ " : "↗ ") + str.tostring(bestN) + "×",
                     style=isHigh ? label.style_label_down : label.style_label_up,
                     color=color.new(colr, 70), textcolor=color.white, size=size.small)
                tls.push(nt)
                while tls.size() > maxN
                    TLine old = tls.shift()
                    line.delete(old.ln)
                    label.delete(old.lb)
    0
 
// ---------- State ----------
var array<Level> levels = array.new<Level>()
var array<TLine> tls = array.new<TLine>()
var array<Brk> brks = array.new<Brk>()
var array<float> phY = array.new<float>()
var array<int> phX = array.new<int>()
var array<float> plY = array.new<float>()
var array<int> plX = array.new<int>()
var array<string> logTxt = array.new<string>()
var array<int> logBar = array.new<int>()
 
var int tlState = 0
var int tlDir = 0
var int tlBar = na
var int tlHeld = 0
var float tlVolR = na
var string tlName = ""
 
var int lvState = 0
var int lvDir = 0
var int lvBar = na
var int lvHeld = 0
var float lvVolR = na
var string lvName = ""
 
float atr = ta.atr(atrLen)
float volMa = ta.sma(volume, 20)
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
 
bool evTlBreak = false
bool evTlTrue = false
bool evTlFalse = false
bool evLvBreak = false
bool evLvTrue = false
bool evLvFalse = false
 
// ---------- New pivots: levels and lines ----------
if barstate.isconfirmed and not na(atr)
    if not na(ph)
        int xH = bar_index - swingLen
        phY.push(ph)
        phX.push(xH)
        if phY.size() > 40
            phY.shift()
            phX.shift()
        if showLv
            f_addPivot(levels, ph, xH, atr, tolAtr, padAtr, maxWidthAtr)
            f_trimLv(levels, maxLv)
        if showTl
            f_tryLine(tls, phY, phX, true, atr, tlTolAtr, minTouchTl, maxCand, maxTlBars, allowCnt, minMoveAtr, minGap, maxTl, cTlRes)
    if not na(pl)
        int xL = bar_index - swingLen
        plY.push(pl)
        plX.push(xL)
        if plY.size() > 40
            plY.shift()
            plX.shift()
        if showLv
            f_addPivot(levels, pl, xL, atr, tolAtr, padAtr, maxWidthAtr)
            f_trimLv(levels, maxLv)
        if showTl
            f_tryLine(tls, plY, plX, false, atr, tlTolAtr, minTouchTl, maxCand, maxTlBars, allowCnt, minMoveAtr, minGap, maxTl, cTlSup)
 
// ---------- Update levels, lines and check breakouts ----------
if barstate.isconfirmed and not na(atr)
    float tolT = tolAtr * atr
    float tlTol = tlTolAtr * atr
    float buf = brkAtr * atr
    float volR = volMa > 0 ? volume / volMa : na
 
    // --- horizontal levels ---
    if showLv and levels.size() > 0
        for i = levels.size() - 1 to 0
            Level lv = levels.get(i)
            if lv.broken
                continue
            bool entered = high >= lv.bot - tolT and low <= lv.top + tolT
            bool held = lv.side == 1 ? close >= lv.bot : close <= lv.top
            if entered and held and bar_index > lv.born and bar_index - lv.lastTouch >= minGap
                lv.touches := lv.touches + 1
                lv.lastTouch := bar_index
            bool qual = lv.touches >= minTouchLv
            bool bUp = lv.side == -1 and close > lv.top + buf
            bool bDn = lv.side == 1 and close < lv.bot - buf
            color lc = lv.side == -1 ? cRes : cSup
            if qual and na(lv.bx)
                lv.bx := box.new(left=lv.born, top=lv.top, right=bar_index + 3, bottom=lv.bot,
                     border_color=color.new(lc, 30), bgcolor=color.new(lc, f_alpha(lv.touches)),
                     text="", text_color=color.white, text_size=size.small,
                     text_halign=text.align_right, text_valign=text.align_center)
            if bUp or bDn
                if qual
                    lv.broken := true
                    box.set_right(lv.bx, bar_index)
                    box.set_top(lv.bx, lv.top)
                    box.set_bottom(lv.bx, lv.bot)
                    box.set_bgcolor(lv.bx, color.new(color.gray, 88))
                    box.set_border_color(lv.bx, color.new(color.gray, 55))
                    box.set_text(lv.bx, "broken · " + str.tostring(lv.touches) + "×")
                    Brk b = Brk.new()
                    b.kind := 0
                    b.dir := bUp ? 1 : -1
                    b.ref := bUp ? lv.top : lv.bot
                    b.bar := bar_index
                    b.state := 1
                    b.held := 1
                    b.volRatio := volR
                    b.volOk := not na(volR) and volR >= volMult
                    b.name := (lv.side == -1 ? "Resistance " : "Support ") + str.tostring(lv.price, format.mintick) + " (" + str.tostring(lv.touches) + "×)"
                    brks.push(b)
                    lvState := 1
                    lvDir := b.dir
                    lvBar := bar_index
                    lvHeld := 1
                    lvVolR := volR
                    lvName := b.name
                    evLvBreak := true
                    f_evLabel(bUp, f_stShort(1), color.yellow)
                else
                    levels.remove(i)
            else if qual
                box.set_top(lv.bx, lv.top)
                box.set_bottom(lv.bx, lv.bot)
                box.set_right(lv.bx, bar_index + 3)
                box.set_bgcolor(lv.bx, color.new(lc, f_alpha(lv.touches)))
                box.set_text(lv.bx, (lv.side == -1 ? "R " : "S ") + str.tostring(lv.touches) + "×")
 
    // --- trend lines ---
    if showTl and tls.size() > 0
        for i = tls.size() - 1 to 0
            TLine t = tls.get(i)
            if t.broken
                continue
            float yl = t.y1 + t.slope * (bar_index - t.x1)
            bool tch = t.isHigh ? (high >= yl - tlTol and close <= yl) : (low <= yl + tlTol and close >= yl)
            if tch and bar_index > t.lastT and bar_index - t.lastT >= minGap
                t.touches := t.touches + 1
                t.lastT := bar_index
                label.set_text(t.lb, (t.slope < 0 ? "↘ " : "↗ ") + str.tostring(t.touches) + "×")
            bool tUp = t.isHigh and close > yl + buf
            bool tDn = (not t.isHigh) and close < yl - buf
            if tUp or tDn
                t.broken := true
                line.set_extend(t.ln, extend.none)
                line.set_x2(t.ln, bar_index)
                line.set_y2(t.ln, yl)
                line.set_color(t.ln, color.new(color.gray, 40))
                label.set_color(t.lb, color.new(color.gray, 60))
                Brk b = Brk.new()
                b.kind := 1
                b.dir := tUp ? 1 : -1
                b.ref := yl
                b.tl := t
                b.bar := bar_index
                b.state := 1
                b.held := 1
                b.volRatio := volR
                b.volOk := not na(volR) and volR >= volMult
                b.name := (t.slope < 0 ? "Falling " : "Rising ") + (t.isHigh ? "resistance line" : "support line") + " (" + str.tostring(t.touches) + "×)"
                brks.push(b)
                tlState := 1
                tlDir := b.dir
                tlBar := bar_index
                tlHeld := 1
                tlVolR := volR
                tlName := b.name
                evTlBreak := true
                f_evLabel(tUp, f_stShort(1), color.yellow)
 
    // --- breakout confirmation check ---
    if brks.size() > 0
        for i = brks.size() - 1 to 0
            Brk b = brks.get(i)
            if bar_index > b.bar
                float ref = b.ref
                if b.kind == 1
                    ref := b.tl.y1 + b.tl.slope * (bar_index - b.tl.x1)
                bool backIn = b.dir == 1 ? close < ref : close > ref
                int prevSt = b.state
                bool done = false
                if b.state == 1
                    if backIn
                        b.state := 3
                        done := true
                    else
                        b.held := b.held + 1
                        if b.held >= confBars
                            b.state := (needVol and not b.volOk) ? 6 : 2
                            b.retestLeft := retestBars
                            if b.state == 6 or not useRetest
                                done := true
                else if b.state == 2
                    bool touchedRef = b.dir == 1 ? low <= ref : high >= ref
                    if touchedRef
                        b.state := ((b.dir == 1 and close > ref) or (b.dir == -1 and close < ref)) ? 4 : 5
                        done := true
                    else
                        b.retestLeft := b.retestLeft - 1
                        if b.retestLeft <= 0
                            done := true
                bool isLatest = b.kind == 0 ? b.bar == lvBar : b.bar == tlBar
                if isLatest
                    if b.kind == 0
                        lvState := b.state
                        lvHeld := b.held
                    else
                        tlState := b.state
                        tlHeld := b.held
                if b.state != prevSt
                    f_evLabel(b.dir == 1, f_stShort(b.state), f_stCol(b.state))
                    logTxt.push(f_stTxt(b.state, b.dir, b.held, confBars) + " · " + b.name)
                    logBar.push(bar_index)
                    if logTxt.size() > 5
                        logTxt.shift()
                        logBar.shift()
                    if b.kind == 0
                        evLvTrue := evLvTrue or b.state == 2
                        evLvFalse := evLvFalse or b.state == 3 or b.state == 5
                    else
                        evTlTrue := evTlTrue or b.state == 2
                        evTlFalse := evTlFalse or b.state == 3 or b.state == 5
                if done
                    brks.remove(i)
 
// ---------- Metrics ----------
float atrPct = close > 0 ? atr / close * 100 : na
float volRatioNow = volMa > 0 ? volume / volMa : na
 
// ---------- Table ----------
tblPos = posIn == "Top right" ? position.top_right : posIn == "Bottom right" ? position.bottom_right : posIn == "Top left" ? position.top_left : position.bottom_left
tSize = szIn == "Tiny" ? size.tiny : szIn == "Small" ? size.small : size.normal
 
var table tbl = table.new(tblPos, 2, 17, bgcolor=color.new(#10151f, 8), border_width=1,
     border_color=color.new(color.gray, 75), frame_width=1, frame_color=color.new(color.gray, 60))
 
color cGreen = #4caf50
color cRed   = #ef5350
color cYel   = #ffca28
color cGray  = #b0bec5
color cWhite = color.white
 
if barstate.islast and showTbl
    // --- find nearest levels ---
    int nR = 0
    int nS = 0
    Level lvA = na
    Level lvB = na
    Level lvIn = na
    float dA = 1e12
    float dB = 1e12
    for lv in levels
        if not lv.broken and lv.touches >= minTouchLv
            if close > lv.top
                nS += 1
                float d = close - lv.top
                if d < dB
                    dB := d
                    lvB := lv
            else if close < lv.bot
                nR += 1
                float d = lv.bot - close
                if d < dA
                    dA := d
                    lvA := lv
            else
                lvIn := lv
 
    // --- find nearest trend lines ---
    int nTR = 0
    int nTS = 0
    TLine tR = na
    TLine tS = na
    float vR = na
    float vS = na
    float dR = 1e12
    float dS = 1e12
    for t in tls
        if not t.broken
            float yl = t.y1 + t.slope * (bar_index - t.x1)
            float d = math.abs(yl - close)
            if t.isHigh
                nTR += 1
                if d < dR
                    dR := d
                    tR := t
                    vR := yl
            else
                nTS += 1
                if d < dS
                    dS := d
                    tS := t
                    vS := yl
 
    // 0: header
    table.cell(tbl, 0, 0, "Auto Levels", text_color=color.white, text_halign=text.align_left, text_size=tSize, bgcolor=color.new(#1e2a3d, 0))
    table.cell(tbl, 1, 0, syminfo.ticker + " · " + timeframe.period, text_color=cYel, text_halign=text.align_left, text_size=tSize, bgcolor=color.new(#1e2a3d, 0))
 
    // 1: settings
    f_row(tbl, 1, "Min. touches", "levels ≥ " + str.tostring(minTouchLv) + " · lines ≥ " + str.tostring(minTouchTl), cWhite, tSize,
         "Only levels and lines with at least this many touches (by wicks) are shown.")
 
    // 2: levels
    f_row(tbl, 2, "Active levels", "res. " + str.tostring(nR) + " / sup. " + str.tostring(nS) + (na(lvIn) ? "" : " · price in zone"), cWhite, tSize,
         "Number of unbroken levels above and below the price.")
 
    // 3: nearest resistance
    string aTxt = "none"
    if not na(lvA)
        aTxt := str.tostring(lvA.bot, format.mintick) + " – " + str.tostring(lvA.top, format.mintick) + " · +" + f_num(dA / close * 100, "#.##") + "% · " + str.tostring(lvA.touches) + "×"
    f_row(tbl, 3, "Nearest resistance", aTxt, na(lvA) ? cGray : cRed, tSize, "Nearest zone above the price: boundaries, distance and touch count.")
 
    // 4: nearest support
    string bTxt = "none"
    if not na(lvB)
        bTxt := str.tostring(lvB.bot, format.mintick) + " – " + str.tostring(lvB.top, format.mintick) + " · −" + f_num(dB / close * 100, "#.##") + "% · " + str.tostring(lvB.touches) + "×"
    f_row(tbl, 4, "Nearest support", bTxt, na(lvB) ? cGray : cGreen, tSize, "Nearest zone below the price: boundaries, distance and touch count.")
 
    // 5: lines
    f_row(tbl, 5, "Trend lines", "res. " + str.tostring(nTR) + " / sup. " + str.tostring(nTS), cWhite, tSize,
         "Number of unbroken trend lines of each type.")
 
    // 6: resistance line
    string rTxt = "none"
    if not na(tR)
        rTxt := (tR.slope < 0 ? "↘ " : "↗ ") + str.tostring(vR, format.mintick) + " · " + f_sgn((vR / close - 1) * 100, "#.##") + "% · " + str.tostring(tR.touches) + "×"
    f_row(tbl, 6, "Resistance line", rTxt, na(tR) ? cGray : cTlRes, tSize, "Value of the nearest resistance line on the current bar.")
 
    // 7: support line
    string sTxt = "none"
    if not na(tS)
        sTxt := (tS.slope < 0 ? "↘ " : "↗ ") + str.tostring(vS, format.mintick) + " · " + f_sgn((vS / close - 1) * 100, "#.##") + "% · " + str.tostring(tS.touches) + "×"
    f_row(tbl, 7, "Support line", sTxt, na(tS) ? cGray : cTlSup, tSize, "Value of the nearest support line on the current bar.")
 
    // 8: trend signal
    string tlTxt = "No trend line breakouts yet"
    color tlCol = cGray
    if not na(tlBar)
        tlTxt := f_stTxt(tlState, tlDir, tlHeld, confBars) + "\n" + tlName + " · " + str.tostring(bar_index - tlBar) + " bars ago · volume ×" + f_num(tlVolR, "#.#")
        tlCol := f_stCol(tlState)
    f_row(tbl, 8, "SIGNAL: TREND", tlTxt, tlCol, tSize,
         "Latest trend line breakout and its status: awaiting confirmation, true, false, confirmed by retest.")
 
    // 9: level signal
    string lvTxt = "No level breakouts yet"
    color lvCol = cGray
    if not na(lvBar)
        lvTxt := f_stTxt(lvState, lvDir, lvHeld, confBars) + "\n" + lvName + " · " + str.tostring(bar_index - lvBar) + " bars ago · volume ×" + f_num(lvVolR, "#.#")
        lvCol := f_stCol(lvState)
    f_row(tbl, 9, "SIGNAL: LEVEL", lvTxt, lvCol, tSize,
         "Latest horizontal level breakout and its status.")
 
    // 10: confirmation rule
    string ruleTxt = str.tostring(confBars) + " closes beyond boundary" + (needVol ? " + volume ×" + str.tostring(volMult, "#.#") : "") + (useRetest ? " · retest " + str.tostring(retestBars) + " bars" : "")
    f_row(tbl, 10, "Confirmation rule", ruleTxt, cWhite, tSize,
         "True breakout: price held beyond the boundary. False: closed back within the confirmation window.")
 
    // 11: under review
    f_row(tbl, 11, "Under review now", str.tostring(brks.size()), brks.size() > 0 ? cYel : cGray, tSize,
         "How many breakouts are still awaiting confirmation or a retest.")
 
    // 12: ATR
    f_row(tbl, 12, "ATR", f_num(atr, format.mintick) + " · " + f_num(atrPct, "#.##") + "%", cWhite, tSize,
         "Average true range. Tolerances, breakout buffer and zone thickness are derived from it.")
 
    // 13: volume
    f_row(tbl, 13, "Volume", na(volRatioNow) ? "—" : "×" + f_num(volRatioNow, "#.##") + " of average", na(volRatioNow) ? cGray : volRatioNow >= volMult ? cGreen : cWhite, tSize,
         "Volume of the current bar relative to the 20-bar average.")
 
    // 14–16: latest events
    int lg = logTxt.size()
    for j = 0 to 2
        string evS = "—"
        color evC = cGray
        if lg - 1 - j >= 0
            string raw = logTxt.get(lg - 1 - j)
            evS := raw + " · " + str.tostring(bar_index - logBar.get(lg - 1 - j)) + " bars ago"
            evC := (str.contains(raw, "TRUE") or str.contains(raw, "retest")) ? cGreen : (str.contains(raw, "FALSE") or str.contains(raw, "failed")) ? cRed : cYel
        f_row(tbl, 14 + j, "Event −" + str.tostring(j + 1), evS, evC, tSize, "Log of the latest breakout status changes.")
 
// ---------- Alerts ----------
alertcondition(evTlBreak, "Trend line breakout", "AutoLevels: trend line breakout")
alertcondition(evTlTrue, "Trend: true breakout", "AutoLevels: true trend line breakout")
alertcondition(evTlFalse, "Trend: false breakout", "AutoLevels: false trend line breakout")
alertcondition(evLvBreak, "Level breakout", "AutoLevels: horizontal level breakout")
alertcondition(evLvTrue, "Level: true breakout", "AutoLevels: true level breakout")
alertcondition(evLvFalse, "Level: false breakout", "AutoLevels: false level breakout")

← 返回列表