CTZ Elliott Wave Pro (based on LuxAlgo)

CryptoTradingZone · study · 669 行 · 点赞 191 · TradingView 原页

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

Pine Script

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// Original script: "Elliott Wave [LuxAlgo]" © LuxAlgo — full credit to LuxAlgo for the original concept and structure.
// Modified version (2026) by CTZ, shared under the same CC BY-NC-SA 4.0 licence.
//
// Changes from the original:
//  Fixes
//   - low source input tested i_hi instead of i_lo
//   - waves were evaluated on pivots that never entered the zigzag (false / misplaced (5) labels)
//   - ta.crossover/crossunder called conditionally (inconsistent history)
//   - wrong "Bullish" text on bearish corrective invalidation alert
//   - zero-seeded zigzag produced junk waves on early bars
//   - unbounded break labels consumed the label budget and deleted old wave labels
//   - unbounded history loop on the fib break check
//  Upgrades
//   - ATR minimum-swing filter
//   - Classic / Strict rule sets (hard rules + guideline ratios)
//   - Fib confluence score per impulse, ratio tooltips on the (5) label
//   - Per-degree notation: (1) / 1 / i / [1]
//   - Live projections: wave 3, wave 4 zones, wave 5 targets, B and C targets, invalidation levels
//   - ABC classification (Zigzag / Flat) with C/A ratio
//   - Impulse invalidation on full retrace, trend-resumption signals on B break
//   - Status table, alerts with ticker / timeframe / degree / price
//   - (5) confirmation: faded until price retraces a set fraction of the impulse, then full.
//     While unconfirmed, a new higher high / lower low snaps (5) to it
//   - Smart label stacking: labels only offset when another degree already labels the same pivot
//   - Status table defaults to bottom right, with an edge-gap input so it clears TradingView's events icon / OHLC legend
//   - Pine Script v6
 
//@version=6
indicator('Elliott Wave Pro (based on LuxAlgo)', shorttitle='EW Pro', overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=100, max_bars_back=5000)
 
//------------------------------------------------------------------------------
// Settings
//------------------------------------------------------------------------------
G_SRC    = 'Source & filter'
i_hi     = input.string('high', 'High / Low source', options=['high', 'close', 'max open/close'], group=G_SRC, inline='src')
i_lo     = input.string('low' , ''                 , options=['low' , 'close', 'min open/close'], group=G_SRC, inline='src')
i_atrL   = input.int   (14    , 'ATR length'       , minval=1, group=G_SRC)
i_minSw  = input.float (0.5   , 'Min swing (× ATR)', minval=0, step=0.1, group=G_SRC, tooltip='A new zigzag leg must move at least this many ATRs. 0 = off (original behaviour).')
i_showZZ = input.bool  (false , 'Show zigzag'      , group=G_SRC)
 
G_DEG = 'Wave degrees  [on · length · colour · notation]'
en1  = input.bool  (true         , ''            , group=G_DEG, inline='1')
len1 = input.int   (4            , 'Minor       ', minval=1, group=G_DEG, inline='1')
col1 = input.color (color.red    , ''            , group=G_DEG, inline='1')
nt1  = input.string('Roman i'    , ''            , options=['Paren (1)', 'Plain 1', 'Roman i', 'Bracket [1]'], group=G_DEG, inline='1')
en2  = input.bool  (true         , ''            , group=G_DEG, inline='2')
len2 = input.int   (8            , 'Intermediate', minval=1, group=G_DEG, inline='2')
col2 = input.color (color.blue   , ''            , group=G_DEG, inline='2')
nt2  = input.string('Plain 1'    , ''            , options=['Paren (1)', 'Plain 1', 'Roman i', 'Bracket [1]'], group=G_DEG, inline='2')
en3  = input.bool  (true         , ''            , group=G_DEG, inline='3')
len3 = input.int   (16           , 'Primary     ', minval=1, group=G_DEG, inline='3')
col3 = input.color (color.orange , ''            , group=G_DEG, inline='3')
nt3  = input.string('Paren (1)'  , ''            , options=['Paren (1)', 'Plain 1', 'Roman i', 'Bracket [1]'], group=G_DEG, inline='3')
 
G_RUL    = 'Wave rules'
i_rules  = input.string('Strict', 'Rule set', options=['Classic', 'Strict'], group=G_RUL, tooltip='Classic = the hard Elliott rules only. Strict = hard rules + the guideline ratios below.')
i_w2min  = input.float (0.236, 'Wave 2 retrace of W1   min', minval=0, step=0.01, group=G_RUL, inline='w2')
i_w2max  = input.float (0.886, 'max'                       , minval=0, step=0.01, group=G_RUL, inline='w2')
i_w3min  = input.float (1.0  , 'Wave 3 min (× W1)'         , minval=0, step=0.1 , group=G_RUL)
i_w4max  = input.float (0.5  , 'Wave 4 max retrace of W3'  , minval=0, step=0.01, group=G_RUL)
i_abcMax = input.float (0.886, 'ABC max retrace of impulse', minval=0, maxval=1, step=0.01, group=G_RUL)
i_tol    = input.float (0.1  , 'Fib match tolerance'       , minval=0.01, maxval=0.5, step=0.01, group=G_RUL, tooltip='Used for the fib confluence score. 0.1 = within 10% of the target ratio.')
 
G_CNF     = 'Wave (5) confirmation'
i_confRet = input.float(0.236, 'Confirm after retrace of impulse', minval=0.01, maxval=1, step=0.01, group=G_CNF, tooltip='(5) stays faded until price pulls back this fraction of the whole 0→5 move. Until confirmed, a new higher high (lower low if bearish) snaps (5) to it.')
i_fadeT   = input.int  (65   , 'Unconfirmed transparency', minval=0, maxval=95, group=G_CNF)
 
G_FIB     = 'Levels & projections'
i_fibShow = input.bool (true , 'Retracements after completed (5)', group=G_FIB)
i_f1      = input.float(0.382, 'Level 1', minval=0, maxval=1, step=0.001, group=G_FIB, inline='f1')
i_f2      = input.float(0.5  , 'Level 2', minval=0, maxval=1, step=0.001, group=G_FIB, inline='f1')
i_f3      = input.float(0.618, 'Level 3', minval=0, maxval=1, step=0.001, group=G_FIB, inline='f2')
i_f4      = input.float(0.786, 'Level 4', minval=0, maxval=1, step=0.001, group=G_FIB, inline='f2')
i_proj    = input.bool (true , 'Live projections (W3 / W4 / W5 / B / C)', group=G_FIB)
i_projLen = input.int  (15   , 'Projection length (bars)', minval=1, group=G_FIB)
 
G_DSP   = 'Display'
i_keep  = input.int   (15         , 'Completed waves kept per degree', minval=1, maxval=20, group=G_DSP)
i_sig   = input.bool  (true       , 'Trend-resumption signals', group=G_DSP)
i_table = input.bool  (true       , 'Status table', group=G_DSP, inline='tb')
i_tpos  = input.string('Bottom right', '', options=['Top right', 'Bottom right', 'Top left', 'Bottom left'], group=G_DSP, inline='tb')
i_tgap  = input.float (5          , 'Table edge gap (% of chart)', minval=0, maxval=25, step=1, group=G_DSP, tooltip='Space between the table and the chart edge so it clears the TradingView events icon (bottom) or the OHLC legend (top).')
i_stack = input.bool  (true       , 'Stack labels across degrees', group=G_DSP, inline='lb', tooltip='When two degrees label the same pivot, the later label is offset so they do not overlap. Labels on their own pivot always sit right on the swing.')
i_lsize = input.string('Small'    , 'Size', options=['Tiny', 'Small', 'Normal'], group=G_DSP, inline='lb')
 
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
type ZZ
    int[]   d
    int[]   x
    float[] y
    line[]  l
 
type Ewave
    int     dir
    int[]   px
    float[] py
    line[]  ln
    label[] lb
    bool    on      = true
    float   ext
    float   score
    bool    hasABC  = false
    bool    abcDead = false
    bool    resumed = false
    int     xA
    int     xB
    int     xC
    float   yB
    string  abcTxt  = ''
    bool    conf    = false
    color   col
    line[]  cln
    label[] clb
    box     bx
 
//------------------------------------------------------------------------------
// Globals
//------------------------------------------------------------------------------
hi  = i_hi == 'high' ? high : i_hi == 'close' ? close : math.max(open, close)
lo  = i_lo == 'low'  ? low  : i_lo == 'close' ? close : math.min(open, close)
atr = ta.atr(i_atrL)
 
var string[] ROM = array.from('i', 'ii', 'iii', 'iv', 'v', 'a', 'b', 'c')
var string[] NUM = array.from('1', '2', '3', '4', '5', 'A', 'B', 'C')
 
// labels already placed per pivot (key = bar * 2 + side), shared across all degrees
var map<int, int> OCC = map.new<int, int>()
 
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
f_base(string st, int i) =>
    string b = st == 'Roman i' ? ROM.get(i) : NUM.get(i)
    st == 'Paren (1)' ? '(' + b + ')' : st == 'Bracket [1]' ? '[' + b + ']' : b
 
f_near(float r, float a, float b) =>
    math.abs(r - a) <= a * i_tol or math.abs(r - b) <= b * i_tol
 
f_tip(float r2, float r3, float r4, float r5, float score) =>
    str.format('W2 = {0,number,#.###} × W1\nW3 = {1,number,#.###} × W1\nW4 = {2,number,#.###} × W3\nW5 = {3,number,#.###} × W1\nFib confluence: {4,number,#}%', r2, r3, r4, r5, score)
 
f_alert(string deg, string msg, float price) =>
    alert(syminfo.ticker + ' ' + timeframe.period + ' · ' + deg + ' · ' + msg + ' @ ' + str.tostring(price, format.mintick), alert.freq_once_per_bar_close)
 
f_label(int x, float y, bool isHigh, string txt, color c, string tip, string lsz) =>
    int    key  = x * 2 + (isHigh ? 1 : 0)
    int    used = nz(OCC.get(key))
    OCC.put(key, used + 1)
    string pad  = not i_stack or used == 0 ? '' : used == 1 ? '\n' : '\n\n'
    label.new(x, y, isHigh ? txt + pad : pad + txt, color=color(na), textcolor=c, style=isHigh ? label.style_label_down : label.style_label_up, size=lsz, tooltip=tip)
 
f_proj(line[] pl, label[] pb, int x1, int x2, float y, color c, string txt, string st) =>
    pl.push(line.new(x1, y, x2, y, color=c, style=st, width=1))
    pb.push(label.new(x2, y, txt + '  ' + str.tostring(y, format.mintick), color=color(na), textcolor=c, style=label.style_label_left, size=size.small))
    true
 
// Adds a pivot to the zigzag. Returns true only if the zigzag actually changed.
method add(ZZ z, int d, int x, float y, float minMove, color col) =>
    bool changed = false
    int  sz      = z.d.size()
    if sz == 0
        z.d.unshift(d)
        z.x.unshift(x)
        z.y.unshift(y)
        changed := true
    else
        int   d0 = z.d.get(0)
        float y0 = z.y.get(0)
        if d0 == d
            if d * y > d * y0
                z.x.set(0, x)
                z.y.set(0, y)
                changed := true
                if i_showZZ and z.l.size() > 0
                    z.l.get(0).set_xy2(x, y)
        else if math.abs(y - y0) >= minMove
            int x0 = z.x.get(0)
            z.d.unshift(d)
            z.x.unshift(x)
            z.y.unshift(y)
            if z.d.size() > 12
                z.d.pop()
                z.x.pop()
                z.y.pop()
            if i_showZZ
                z.l.unshift(line.new(x0, y0, x, y, color=color.new(col, 50)))
                if z.l.size() > 12
                    z.l.pop().delete()
            changed := true
    changed
 
method fade(Ewave w) =>
    for l in w.ln
        l.set_style(line.style_dotted)
    for b in w.lb
        b.set_textcolor(color.gray)
    w.on := false
 
method dashABC(Ewave w) =>
    for l in w.cln
        l.set_style(line.style_dashed)
    for b in w.clb
        b.set_textcolor(color.gray)
    if not na(w.bx)
        w.bx.set_bgcolor(color(na))
        w.bx.set_text('ABC invalid')
    w.abcDead := true
 
method kill(Ewave w) =>
    for l in w.ln
        l.delete()
    for b in w.lb
        b.delete()
    for l in w.cln
        l.delete()
    for b in w.clb
        b.delete()
    if not na(w.bx)
        w.bx.delete()
    true
 
// Evaluates the last 6 zigzag points as a 1-2-3-4-5 impulse (normalised so bullish logic covers both directions)
f_motive(ZZ z) =>
    int   s  = z.d.get(0)
    float v0 = s * z.y.get(5)
    float v1 = s * z.y.get(4)
    float v2 = s * z.y.get(3)
    float v3 = s * z.y.get(2)
    float v4 = s * z.y.get(1)
    float v5 = s * z.y.get(0)
    float w1 = v1 - v0
    float w2 = v1 - v2
    float w3 = v3 - v2
    float w4 = v3 - v4
    float w5 = v5 - v4
    // hard rules: W2 < 100% of W1, W3 not shortest, W4 no overlap with W1, W3 beyond W1, W5 beyond W3
    bool  hard  = w1 > 0 and w2 > 0 and w3 > 0 and w4 > 0 and w5 > 0 and v2 > v0 and v3 > v1 and v4 > v1 and v5 > v3 and not (w3 < w1 and w3 < w5)
    float r2    = w2 / w1
    float r3    = w3 / w1
    float r4    = w4 / w3
    float r5    = w5 / w1
    bool  guide = r2 >= i_w2min and r2 <= i_w2max and r3 >= i_w3min and r4 <= i_w4max
    bool  ok    = hard and (i_rules == 'Classic' or guide)
    float score = (f_near(r2, 0.5, 0.618) ? 25 : 0) + (f_near(r3, 1.618, 2.618) ? 25 : 0) + (f_near(r4, 0.236, 0.382) ? 25 : 0) + (f_near(r5, 0.618, 1.0) ? 25 : 0)
    [ok, s, score, r2, r3, r4, r5]
 
// Recomputes ratios / score from a wave's stored points (used when (5) snaps)
f_ratios(float[] py, int s) =>
    float w1 = s * (py.get(1) - py.get(0))
    float w2 = s * (py.get(1) - py.get(2))
    float w3 = s * (py.get(3) - py.get(2))
    float w4 = s * (py.get(3) - py.get(4))
    float w5 = s * (py.get(5) - py.get(4))
    float r2 = w2 / w1
    float r3 = w3 / w1
    float r4 = w4 / w3
    float r5 = w5 / w1
    float score = (f_near(r2, 0.5, 0.618) ? 25 : 0) + (f_near(r3, 1.618, 2.618) ? 25 : 0) + (f_near(r4, 0.236, 0.382) ? 25 : 0) + (f_near(r5, 0.618, 1.0) ? 25 : 0)
    [r2, r3, r4, r5, score]
 
// Faded + dashed wave-5 leg and (5) label while unconfirmed, full once confirmed
method style5(Ewave w) =>
    color c = w.conf ? w.col : color.new(w.col, i_fadeT)
    w.ln.get(4).set_color(c)
    w.ln.get(4).set_style(w.conf ? line.style_solid : line.style_dashed)
    w.lb.get(4).set_textcolor(c)
    true
 
// Runs only when the zigzag changed
f_eval(ZZ z, Ewave[] waves, color col, string nt, string deg, int wd, string lsz) =>
    int  sz      = z.d.size()
    bool snapped = false
    //--------------------------------------------------------------- snap unconfirmed-or-no-ABC (5) to a new extreme
    if sz >= 3 and waves.size() > 0
        Ewave w = waves.get(0)
        int   s = w.dir
        if w.on and not w.hasABC and z.d.get(0) == s and z.x.get(0) > w.px.get(5) and s * z.y.get(0) > s * w.py.get(5)
            bool found = false
            bool holds = true
            for i = 1 to sz - 1
                if z.x.get(i) == w.px.get(5)
                    found := true
                    break
                if z.x.get(i) < w.px.get(5)
                    break
                // pullbacks since the old (5) must stay beyond wave 4, otherwise it's a new structure
                if z.d.get(i) == -s and s * z.y.get(i) <= s * w.py.get(4)
                    holds := false
            if found and holds
                int   x = z.x.get(0)
                float y = z.y.get(0)
                w.px.set(5, x)
                w.py.set(5, y)
                w.ln.get(4).set_xy2(x, y)
                w.lb.get(4).set_xy(x, y)
                [q2, q3, q4, q5, qs] = f_ratios(w.py, s)
                w.lb.get(4).set_tooltip(f_tip(q2, q3, q4, q5, qs))
                w.score := qs
                w.ext   := s == 1 ? math.min(low, low[1]) : math.max(high, high[1])
                w.conf  := false
                w.style5()
                snapped := true
                f_alert(deg, '(5) snapped to new ' + (s == 1 ? 'high' : 'low') + ' (unconfirmed)', y)
    //--------------------------------------------------------------- impulse
    if sz >= 6 and not snapped
        [ok, s, score, r2, r3, r4, r5] = f_motive(z)
        bool same = false
        if waves.size() > 0
            Ewave prev = waves.get(0)
            same := prev.px.get(0) == z.x.get(5) and prev.px.get(1) == z.x.get(4) and prev.px.get(2) == z.x.get(3) and prev.px.get(3) == z.x.get(2) and prev.px.get(4) == z.x.get(1)
        if ok
            string tip = f_tip(r2, r3, r4, r5, score)
            if same
                // wave 5 extending
                Ewave last = waves.get(0)
                int   x    = z.x.get(0)
                float y    = z.y.get(0)
                last.px.set(5, x)
                last.py.set(5, y)
                last.ln.get(4).set_xy2(x, y)
                last.lb.get(4).set_xy(x, y)
                last.lb.get(4).set_tooltip(tip)
                last.score := score
                last.ext   := s == 1 ? math.min(low, low[1]) : math.max(high, high[1])
                last.conf  := false
                last.style5()
            else
                Ewave w = Ewave.new(dir=s, px=array.new<int>(), py=array.new<float>(), ln=array.new<line>(), lb=array.new<label>(), cln=array.new<line>(), clb=array.new<label>(), score=score, col=col, ext=s == 1 ? math.min(low, low[1]) : math.max(high, high[1]))
                for i = 5 to 0
                    w.px.push(z.x.get(i))
                    w.py.push(z.y.get(i))
                for i = 1 to 5
                    bool isHigh = (i % 2 == 1) == (s == 1)
                    w.ln.push(line.new(w.px.get(i - 1), w.py.get(i - 1), w.px.get(i), w.py.get(i), color=col, width=wd))
                    w.lb.push(f_label(w.px.get(i), w.py.get(i), isHigh, f_base(nt, i - 1), col, i == 5 ? tip : '', lsz))
                w.style5()
                waves.unshift(w)
                if waves.size() > i_keep
                    waves.pop().kill()
                f_alert(deg, 'New ' + (s == 1 ? 'bullish' : 'bearish') + ' impulse 1-5 (unconfirmed) · fib ' + str.tostring(score, '#') + '%', z.y.get(0))
    //--------------------------------------------------------------- ABC
    if sz >= 4 and waves.size() > 0
        Ewave w = waves.get(0)
        int   s = w.dir
        if z.x.get(3) == w.px.get(5) and z.d.get(0) == -s
            float top     = s * w.py.get(5)
            float org     = s * w.py.get(0)
            float vA      = s * z.y.get(2)
            float vB      = s * z.y.get(1)
            float vC      = s * z.y.get(0)
            float lim     = top - (top - org) * i_abcMax
            bool  valid   = w.on and vA < top and vB > vA and vB < top and vC < vB and vA > lim and vC > lim
            bool  sameABC = w.hasABC and w.xA == z.x.get(2) and w.xB == z.x.get(1)
            if valid
                float  aLen = top - vA
                float  rB   = (vB - vA) / aLen
                float  rC   = (vB - vC) / aLen
                string txt  = (rB >= 0.9 ? 'Flat' : 'Zigzag') + ' · C = ' + str.tostring(rC, '#.##') + '×A'
                int    xA   = z.x.get(2)
                int    xB   = z.x.get(1)
                int    xC   = z.x.get(0)
                float  yA   = z.y.get(2)
                float  yB   = z.y.get(1)
                float  yC   = z.y.get(0)
                int    wdt  = math.max(xC - w.px.get(5), 1)
                if sameABC
                    // C extending
                    w.cln.get(2).set_xy2(xC, yC)
                    w.clb.get(2).set_xy(xC, yC)
                    w.clb.get(2).set_tooltip(txt)
                    w.bx.set_lefttop(xC, math.max(yA, yC))
                    w.bx.set_rightbottom(xC + wdt, math.min(yA, yC))
                    w.bx.set_text(txt)
                    w.xC     := xC
                    w.abcTxt := txt
                else
                    if w.hasABC
                        for l in w.cln
                            l.delete()
                        for b in w.clb
                            b.delete()
                        w.cln.clear()
                        w.clb.clear()
                        if not na(w.bx)
                            w.bx.delete()
                    w.cln.push(line.new(w.px.get(5), w.py.get(5), xA, yA, color=col, width=wd))
                    w.cln.push(line.new(xA, yA, xB, yB, color=col, width=wd))
                    w.cln.push(line.new(xB, yB, xC, yC, color=col, width=wd))
                    w.clb.push(f_label(xA, yA, s == -1, f_base(nt, 5), col, '' , lsz))
                    w.clb.push(f_label(xB, yB, s ==  1, f_base(nt, 6), col, '' , lsz))
                    w.clb.push(f_label(xC, yC, s == -1, f_base(nt, 7), col, txt, lsz))
                    w.bx      := box.new(xC, math.max(yA, yC), xC + wdt, math.min(yA, yC), border_color=color.new(col, 60), bgcolor=color.new(col, 92), text=txt, text_color=col, text_size=size.small, text_halign=text.align_left, text_valign=text.align_top)
                    w.hasABC  := true
                    w.abcDead := false
                    w.resumed := false
                    w.xA      := xA
                    w.xB      := xB
                    w.xC      := xC
                    w.yB      := yB
                    w.abcTxt  := txt
                    f_alert(deg, 'ABC after ' + (s == 1 ? 'bullish' : 'bearish') + ' impulse (' + txt + ')', yC)
            else if sameABC and not w.abcDead
                w.dashABC()
                f_alert(deg, 'ABC invalidated', z.y.get(0))
    true
 
//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
draw(bool enabled, simple int len, color col, int n, string nt, string deg) =>
    var ZZ         z     = ZZ.new(array.new<int>(), array.new<int>(), array.new<float>(), array.new<line>())
    var Ewave[]    waves = array.new<Ewave>()
    var line[]     pln   = array.new<line>()
    var label[]    plb   = array.new<label>()
    var linefill[] pfl   = array.new<linefill>()
    var string     state = 'Waiting for pivots'
    var string     info  = '—'
    var float      inval = na
    bool   bullSig = false
    bool   bearSig = false
    float  ph      = ta.pivothigh(hi, len, 1)
    float  pl      = ta.pivotlow (lo, len, 1)
    string lsz     = i_lsize == 'Tiny' ? size.tiny : i_lsize == 'Normal' ? size.normal : size.small
    int    wd      = n + 1
    float  minMove = nz(atr) * i_minSw
    if enabled
        //----------------------------------------------------------- pivots
        if not na(ph)
            if z.add(1, bar_index - 1, ph, minMove, col)
                f_eval(z, waves, col, nt, deg, wd, lsz)
        if not na(pl)
            if z.add(-1, bar_index - 1, pl, minMove, col)
                f_eval(z, waves, col, nt, deg, wd, lsz)
        //----------------------------------------------------------- per-bar tracking
        if waves.size() > 0
            Ewave w = waves.get(0)
            if w.on
                w.ext := w.dir == 1 ? math.min(nz(w.ext, low), low) : math.max(nz(w.ext, high), high)
                bool broke = w.dir == 1 ? w.ext < w.py.get(0) : w.ext > w.py.get(0)
                if broke
                    w.fade()
                    f_alert(deg, 'Impulse invalidated (fully retraced)', close)
                else if not w.conf
                    float d   = w.py.get(5) - w.py.get(0)
                    float lvl = w.py.get(5) - d * i_confRet
                    bool  hit = w.dir == 1 ? w.ext <= lvl : w.ext >= lvl
                    if hit or w.hasABC
                        w.conf := true
                        w.style5()
                        f_alert(deg, '(5) confirmed', close)
            if w.hasABC and not w.abcDead and not w.resumed and bar_index > w.xC
                if w.dir == 1 ? close > w.yB : close < w.yB
                    w.resumed := true
                    if w.dir == 1
                        bullSig := true
                    else
                        bearSig := true
                    f_alert(deg, 'Trend resumption: ' + (w.dir == 1 ? 'bullish' : 'bearish') + ' (B broken)', close)
        //----------------------------------------------------------- last bar: state + projections
        if barstate.islast
            for l in pln
                l.delete()
            for b in plb
                b.delete()
            for f in pfl
                linefill.delete(f)
            pln.clear()
            plb.clear()
            pfl.clear()
            state := 'Scanning'
            info  := '—'
            inval := na
            int  sz   = z.d.size()
            int  xr   = bar_index + i_projLen
            bool done = false
            if sz >= 6
                // ----- states tied to the latest completed impulse
                if waves.size() > 0
                    Ewave w = waves.get(0)
                    if w.on
                        int   s  = w.dir
                        float p0 = w.py.get(0)
                        float p5 = w.py.get(5)
                        int   x5 = w.px.get(5)
                        if z.x.get(0) == x5
                            done  := true
                            float cl = p5 - (p5 - p0) * i_confRet
                            state := w.conf ? 'Impulse (5) confirmed' : 'Impulse (5) unconfirmed'
                            info  := 'Fib ' + str.tostring(w.score, '#') + '%' + (w.conf ? '' : ' · confirms ' + (s == 1 ? '< ' : '> ') + str.tostring(cl, format.mintick))
                            inval := p0
                            if i_fibShow
                                float  d    = p5 - p0
                                bool   deep = s == 1 ? w.ext < p5 - d * i_f4 : w.ext > p5 - d * i_f4
                                color  fc   = deep ? color.red : w.conf ? col : color.new(col, i_fadeT)
                                string fs   = deep ? line.style_dotted : line.style_solid
                                f_proj(pln, plb, x5, xr, p5 - d * i_f1, fc, str.tostring(i_f1), fs)
                                f_proj(pln, plb, x5, xr, p5 - d * i_f2, fc, str.tostring(i_f2), fs)
                                f_proj(pln, plb, x5, xr, p5 - d * i_f3, fc, str.tostring(i_f3), fs)
                                f_proj(pln, plb, x5, xr, p5 - d * i_f4, fc, str.tostring(i_f4), fs)
                                pfl.push(linefill.new(pln.get(1), pln.get(2), color.new(fc, 88)))
                        else if z.x.get(1) == x5
                            done := true
                            float yA   = z.y.get(0)
                            float aLen = math.abs(p5 - yA)
                            state := (w.conf ? '' : '(5)? · ') + 'Wave B forming'
                            info  := 'A = ' + str.tostring(aLen / math.abs(p5 - p0), '#.###') + ' of impulse'
                            inval := p5
                            if i_proj
                                int xa = z.x.get(0)
                                f_proj(pln, plb, xa, xr, yA + s * aLen * 0.5  , col      , 'B 0.5'           , line.style_dashed)
                                f_proj(pln, plb, xa, xr, yA + s * aLen * 0.618, col      , 'B 0.618'         , line.style_dashed)
                                f_proj(pln, plb, xa, xr, yA + s * aLen * 0.786, col      , 'B 0.786'         , line.style_dashed)
                                f_proj(pln, plb, xa, xr, p5                   , color.red, 'B limit (5 end)' , line.style_dotted)
                        else if z.x.get(2) == x5
                            done := true
                            float yA   = z.y.get(1)
                            float yB   = z.y.get(0)
                            float aLen = math.abs(p5 - yA)
                            float lim  = p5 - (p5 - p0) * i_abcMax
                            state := (w.conf ? '' : '(5)? · ') + 'Wave C forming'
                            info  := 'B = ' + str.tostring(math.abs(yB - yA) / aLen, '#.###') + ' of A'
                            inval := lim
                            if i_proj
                                int xb = z.x.get(0)
                                f_proj(pln, plb, xb, xr, yB - s * aLen * 0.618, col      , 'C 0.618×A', line.style_dashed)
                                f_proj(pln, plb, xb, xr, yB - s * aLen        , col      , 'C = A'    , line.style_dashed)
                                f_proj(pln, plb, xb, xr, yB - s * aLen * 1.618, col      , 'C 1.618×A', line.style_dashed)
                                f_proj(pln, plb, xb, xr, lim                  , color.red, 'ABC limit', line.style_dotted)
                        else if z.x.get(3) == x5 and w.hasABC and not w.abcDead
                            if w.xC == z.x.get(0)
                                done  := true
                                state := w.resumed ? 'Trend resumed' : 'ABC complete?'
                                info  := w.abcTxt
                                inval := z.y.get(0)
                                if i_proj
                                    f_proj(pln, plb, z.x.get(0), xr, w.yB      , col      , 'Confirm (break B)', line.style_dashed)
                                    f_proj(pln, plb, z.x.get(0), xr, z.y.get(0), color.red, 'C extreme'        , line.style_dotted)
                // ----- 0-1-2-3-4 → wave 5 in progress
                if not done
                    int   sd = -z.d.get(0)
                    float v0 = sd * z.y.get(4)
                    float v1 = sd * z.y.get(3)
                    float v2 = sd * z.y.get(2)
                    float v3 = sd * z.y.get(1)
                    float v4 = sd * z.y.get(0)
                    float w1 = v1 - v0
                    float w2 = v1 - v2
                    float w3 = v3 - v2
                    float w4 = v3 - v4
                    bool  ok = w1 > 0 and w2 > 0 and w3 > 0 and w4 > 0 and v2 > v0 and v3 > v1 and v4 > v1
                    if ok and i_rules == 'Strict'
                        ok := w2 / w1 >= i_w2min and w2 / w1 <= i_w2max and w3 / w1 >= i_w3min and w4 / w3 <= i_w4max
                    if ok
                        done := true
                        float y4 = z.y.get(0)
                        int   x4 = z.x.get(0)
                        state := 'Wave 5 in progress'
                        info  := 'W3 = ' + str.tostring(w3 / w1, '#.##') + '×W1'
                        inval := z.y.get(3)
                        if i_proj
                            f_proj(pln, plb, x4, xr, y4 + sd * w1 * 0.618       , col, 'W5 0.618×W1'    , line.style_dashed)
                            f_proj(pln, plb, x4, xr, y4 + sd * w1               , col, 'W5 = W1'        , line.style_dashed)
                            f_proj(pln, plb, x4, xr, y4 + sd * (v3 - v0) * 0.618, col, 'W5 0.618×(0→3)' , line.style_dashed)
                            if w3 < w1
                                f_proj(pln, plb, x4, xr, y4 + sd * w3, color.orange, 'W5 max (W3 not shortest)', line.style_dotted)
                            f_proj(pln, plb, x4, xr, inval, color.red, 'Invalidation (W1 overlap)', line.style_dotted)
                // ----- 0-1-2-3 → wave 4 in progress
                if not done
                    int   sd = z.d.get(0)
                    float v0 = sd * z.y.get(3)
                    float v1 = sd * z.y.get(2)
                    float v2 = sd * z.y.get(1)
                    float v3 = sd * z.y.get(0)
                    float w1 = v1 - v0
                    float w2 = v1 - v2
                    float w3 = v3 - v2
                    bool  ok = w1 > 0 and w2 > 0 and w3 > 0 and v2 > v0 and v3 > v1
                    if ok and i_rules == 'Strict'
                        ok := w2 / w1 >= i_w2min and w2 / w1 <= i_w2max and w3 / w1 >= i_w3min
                    if ok
                        done := true
                        float y3 = z.y.get(0)
                        int   x3 = z.x.get(0)
                        state := 'Wave 4 in progress'
                        info  := 'W3 = ' + str.tostring(w3 / w1, '#.##') + '×W1'
                        inval := z.y.get(2)
                        if i_proj
                            f_proj(pln, plb, x3, xr, y3 - sd * w3 * 0.236, col, 'W4 0.236', line.style_dashed)
                            f_proj(pln, plb, x3, xr, y3 - sd * w3 * 0.382, col, 'W4 0.382', line.style_dashed)
                            f_proj(pln, plb, x3, xr, y3 - sd * w3 * 0.5  , col, 'W4 0.5'  , line.style_dashed)
                            f_proj(pln, plb, x3, xr, inval, color.red, 'Invalidation (W1 overlap)', line.style_dotted)
                // ----- 0-1-2 → wave 3 setup
                if not done
                    int   sd = -z.d.get(0)
                    float v0 = sd * z.y.get(2)
                    float v1 = sd * z.y.get(1)
                    float v2 = sd * z.y.get(0)
                    float w1 = v1 - v0
                    float w2 = v1 - v2
                    bool  ok = w1 > 0 and w2 > 0 and v2 > v0
                    if ok and i_rules == 'Strict'
                        ok := w2 / w1 >= i_w2min and w2 / w1 <= i_w2max
                    if ok
                        done := true
                        float y2 = z.y.get(0)
                        int   x2 = z.x.get(0)
                        state := 'Wave 3 setup'
                        info  := 'W2 = ' + str.tostring(w2 / w1, '#.###') + '×W1'
                        inval := z.y.get(2)
                        if i_proj
                            f_proj(pln, plb, x2, xr, y2 + sd * w1        , col, 'W3 = W1'    , line.style_dashed)
                            f_proj(pln, plb, x2, xr, y2 + sd * w1 * 1.618, col, 'W3 1.618×W1', line.style_dashed)
                            f_proj(pln, plb, x2, xr, y2 + sd * w1 * 2.618, col, 'W3 2.618×W1', line.style_dashed)
                            f_proj(pln, plb, x2, xr, inval, color.red, 'Invalidation (W1 start)', line.style_dotted)
    [bullSig, bearSig, state, info, inval]
 
//------------------------------------------------------------------------------
// Run
//------------------------------------------------------------------------------
[bu1, be1, st1, in1, iv1] = draw(en1, len1, col1, 0, nt1, 'Minor')
[bu2, be2, st2, in2, iv2] = draw(en2, len2, col2, 1, nt2, 'Intermediate')
[bu3, be3, st3, in3, iv3] = draw(en3, len3, col3, 2, nt3, 'Primary')
 
plotshape(i_sig and bu1, 'Minor bull resume'       , shape.triangleup  , location.belowbar, col1, size=size.tiny )
plotshape(i_sig and be1, 'Minor bear resume'       , shape.triangledown, location.abovebar, col1, size=size.tiny )
plotshape(i_sig and bu2, 'Intermediate bull resume', shape.triangleup  , location.belowbar, col2, size=size.small)
plotshape(i_sig and be2, 'Intermediate bear resume', shape.triangledown, location.abovebar, col2, size=size.small)
plotshape(i_sig and bu3, 'Primary bull resume'     , shape.triangleup  , location.belowbar, col3, size=size.normal)
plotshape(i_sig and be3, 'Primary bear resume'     , shape.triangledown, location.abovebar, col3, size=size.normal)
 
//------------------------------------------------------------------------------
// Status table
//------------------------------------------------------------------------------
f_pos(string p) =>
    p == 'Top right' ? position.top_right : p == 'Bottom right' ? position.bottom_right : p == 'Top left' ? position.top_left : position.bottom_left
 
f_cell(table tb, int c, int r, string t, color tc, color bg, string ha) =>
    table.cell(tb, c, r, t, text_color=tc, bgcolor=bg, text_size=size.small, text_halign=ha)
    true
 
f_row(table tb, int r, bool en, string deg, color c, string st, string inf, float iv) =>
    if en
        color bg = color.new(chart.bg_color, 10)
        f_cell(tb, 0, r, deg, c             , bg, text.align_left)
        f_cell(tb, 1, r, st , chart.fg_color, bg, text.align_left)
        f_cell(tb, 2, r, inf, chart.fg_color, bg, text.align_left)
        f_cell(tb, 3, r, na(iv) ? '—' : str.tostring(iv, format.mintick), color.red, bg, text.align_center)
    true
 
// transparent table; each cell carries its own background so the spacer row stays invisible
var table tb = table.new(f_pos(i_tpos), 4, 5, bgcolor=color(na), frame_width=0, border_width=0)
 
if barstate.islast and i_table
    bool  top = i_tpos == 'Top right' or i_tpos == 'Top left'
    int   o   = top ? 1 : 0
    color hb  = color.new(chart.fg_color, 85)
    // spacer on the anchored edge
    table.cell(tb, 0, top ? 0 : 4, '', height=i_tgap, bgcolor=color(na))
    f_cell(tb, 0, o, 'Degree'      , chart.fg_color, hb, text.align_center)
    f_cell(tb, 1, o, 'State'       , chart.fg_color, hb, text.align_center)
    f_cell(tb, 2, o, 'Detail'      , chart.fg_color, hb, text.align_center)
    f_cell(tb, 3, o, 'Invalidation', chart.fg_color, hb, text.align_center)
    f_row(tb, o + 1, en1, 'Minor ('        + str.tostring(len1) + ')', col1, st1, in1, iv1)
    f_row(tb, o + 2, en2, 'Intermediate (' + str.tostring(len2) + ')', col2, st2, in2, iv2)
    f_row(tb, o + 3, en3, 'Primary ('      + str.tostring(len3) + ')', col3, st3, in3, iv3)
//------------------------------------------------------------------------------

← 返回列表