EonMetrics AMD Phases (Power of Three)

EonMetrics · study · 514 行 · 点赞 28 · TradingView 原页

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

Pine Script

//@version=6
indicator('EonMetrics AMD Phases (Power of Three)', 'AMD Phases by EonMetrics', overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
 
// ═════════════════════════════════════════════════════════════════════════════
//  ACCUMULATION
// ═════════════════════════════════════════════════════════════════════════════
grpAcc = 'Accumulation'
 
atrLen = input.int(14, 'ATR Length', minval=1, maxval=200,
  group   = grpAcc,
  tooltip = 'Length of the ATR used to scale every threshold in the script (range height, sweep depth). 14 is the common default; raising it makes all thresholds react slower to volatility changes.')
 
rangeW = input.int(20, 'Range Window (bars)', minval=5, maxval=200,
  group   = grpAcc,
  tooltip = 'How many recent bars are examined for a sideways range. The high-low span of the last W bars must stay compressed for the window to qualify as accumulation.')
 
rangeK = input.float(4.0, 'Max Range Height (x ATR)', minval=0.5, maxval=10.0, step=0.1,
  group   = grpAcc,
  tooltip = 'Compression threshold. The window counts as contained only while (highest - lowest) of the last W bars is at most this many ATRs. Smaller = tighter, rarer ranges; larger = looser, more frequent ranges.')
 
rangeM = input.int(4, 'Min Range Age (bars)', minval=1, maxval=50,
  group   = grpAcc,
  tooltip = 'The containment condition must hold for at least this many consecutive closed bars before the range qualifies as accumulation and its edges are locked. Filters out one-bar pauses.')
 
epCooldown = input.int(10, 'Episode Cooldown (bars)', minval=0, maxval=100,
  group   = grpAcc,
  tooltip = 'Minimum number of bars after an episode ends (completed or failed) before a new accumulation range may qualify. Prevents overlapping back-to-back boxes over the same congestion area.')
 
// ═════════════════════════════════════════════════════════════════════════════
//  MANIPULATION
// ═════════════════════════════════════════════════════════════════════════════
grpMan = 'Manipulation'
 
minSweep = input.float(0.25, 'Min Sweep Depth (x ATR)', minval=0.0, maxval=5.0, step=0.05,
  group   = grpMan,
  tooltip = 'How far beyond the locked range edge a wick must reach to count as a sweep, in ATR units. Prevents every one-tick poke from starting a manipulation phase. 0 accepts any pierce.')
 
reclaimWin = input.int(5, 'Reclaim Window (bars)', minval=1, maxval=20,
  group   = grpMan,
  tooltip = 'After the sweep, price must CLOSE back inside the range within this many bars for the move to count as manipulation. If it keeps trading outside longer than this, the episode is discarded as a genuine breakout.')
 
allowCloseBeyond = input.bool(true, 'Allow Closes Beyond The Edge',
  group   = grpMan,
  tooltip = 'ON (default): closes beyond the swept edge are tolerated as long as price closes back inside within the reclaim window - deeper stop-hunts also count. OFF (strict): a bar that CLOSES beyond the swept edge immediately disqualifies the episode - only pure wick sweeps count as manipulation.')
 
requirePoolSweep = input.bool(false, 'Sweep Must Take Out A Liquidity Pool',
  group   = grpMan,
  tooltip = 'OFF (default): any pierce of the locked range edge that reaches the minimum depth can start the manipulation phase. ON (stricter): the same bar must also trade through a confirmed swing high (upside sweep) or swing low (downside sweep) - the resting-liquidity levels drawn by the Liquidity Pools layer. This rejects pierces of an arbitrary box edge that did not actually collect any stops, at the cost of far fewer episodes.')
 
// ═════════════════════════════════════════════════════════════════════════════
//  DISTRIBUTION
// ═════════════════════════════════════════════════════════════════════════════
grpDist = 'Distribution'
 
distMode = input.string('Structure Break', 'Confirmation Level', options=['Structure Break', 'Opposite Edge', 'Range Midpoint'],
  group   = grpDist,
  tooltip = 'What confirms distribution after the reclaimed sweep. Structure Break (default) = a close beyond the origin of the leg that produced the sweep - the extreme of the bars immediately preceding the pierce, which is the base the sweeping push started from. Breaking it means the move that swept the edge has been fully undone. Range Midpoint = a close beyond the middle of the range. Opposite Edge = a close beyond the far side of the range (strictest, requires crossing the whole range).')
 
distWin = input.int(24, 'Confirmation Window (bars)', minval=1, maxval=100,
  group   = grpDist,
  tooltip = 'How many bars after the reclaim the distribution confirmation may arrive. If price does not confirm within this window, or takes out the manipulation extreme first, the episode is discarded.')
 
// ═════════════════════════════════════════════════════════════════════════════
//  LIQUIDITY POOLS
// ═════════════════════════════════════════════════════════════════════════════
grpPool = 'Liquidity Pools'
 
showPools = input.bool(true, 'Show Pool Lines', group=grpPool,
  tooltip = 'Draws a horizontal line at every confirmed swing high (above price) and swing low (below price) - the classic resting-liquidity levels. Each line stops extending the moment price trades through it. The levels themselves are always tracked internally, so hiding the lines does not change detection.')
 
swingLen = input.int(5, 'Swing Length (bars)', minval=1, maxval=25,
  group   = grpPool,
  tooltip = 'A swing high/low is confirmed when it is the highest/lowest point among this many bars on BOTH sides. Larger = fewer, more significant pivots. Also feeds the Structure Break confirmation of the Distribution phase.')
 
maxPools = input.int(12, 'Max Pool Lines Per Side', minval=1, maxval=50,
  group   = grpPool,
  tooltip = 'Maximum number of pool lines kept per side; the oldest are removed first.')
 
poolHiCol = input.color(#EF5350, 'Highs', inline='pc', group=grpPool)
poolLoCol = input.color(#26A69A, 'Lows', inline='pc', group=grpPool)
 
// ═════════════════════════════════════════════════════════════════════════════
//  DISPLAY
// ═════════════════════════════════════════════════════════════════════════════
grpVis = 'Display'
 
showBoxes   = input.bool(true, 'Show Phase Boxes', group=grpVis)
showLabels  = input.bool(true, 'Show A / M / D Labels', group=grpVis)
keepFailed  = input.bool(true, 'Keep Failed Episodes (dimmed)', group=grpVis,
  tooltip = 'ON: episodes that fail (real breakout, no reclaim, no confirmation) stay on the chart dimmed with a dashed border, useful for review. OFF: failed episodes are removed immediately.')
maxEpisodes = input.int(20, 'Max Episodes On Chart', minval=1, maxval=80, group=grpVis,
  tooltip = 'Maximum number of past episodes kept on the chart. The oldest drawings are removed once the limit is exceeded.')
 
accColor  = input.color(#5B9CF6, 'Accumulation', inline='c1', group=grpVis)
manColor  = input.color(#F0A44B, 'Manipulation', inline='c1', group=grpVis)
distUpCol = input.color(#26A69A, 'Distribution Up', inline='c2', group=grpVis)
distDnCol = input.color(#EF5350, 'Distribution Down', inline='c2', group=grpVis)
 
// ═════════════════════════════════════════════════════════════════════════════
//  FOLLOW-THROUGH
// ═════════════════════════════════════════════════════════════════════════════
grpRes = 'Follow-Through'
 
showRes = input.bool(true, 'Show Follow-Through Table', group=grpRes,
  tooltip = 'After each confirmed distribution, the script measures what price did next: whether it travelled the chosen distance in the confirmed direction before returning to the manipulation extreme. The table counts those outcomes on the bars loaded in this chart. It is a measurement of how the pattern behaved on this history - not a trade record, not a recommendation, and not a projection of future results.')
 
targetR = input.float(1.0, 'Risk / Reward (RR)', minval=0.25, maxval=10.0, step=0.25,
  group   = grpRes,
  tooltip = 'Choose the desired risk/reward ratio for the measured objective. Risk is defined by the pattern itself: the gap between the closing price of the confirmation bar and the manipulation extreme (the tip of the sweep). A value of 1.0 means 1R; 2.0 means 2R.')
 
showObjLine = input.bool(true, 'Draw Objective Line On Chart', group=grpRes,
  tooltip = 'After each distribution confirmation, draws a short horizontal line at the measured objective - the same level the Follow-Through table measures against, at Objective (x risk) from the confirmation close. It marks where the measurement is taken; it is not a recommendation to place an order there.')
 
objBars = input.int(8, 'Objective Line Length (bars)', minval=2, maxval=50, group=grpRes,
  tooltip = 'How many bars to the right the objective line and its TP tag extend.')
 
objCol = input.color(#FFD54F, 'Objective Line', group=grpRes)
 
resPos = input.string('Bottom Right', 'Position', options=['Top Left', 'Top Center', 'Top Right', 'Middle Left', 'Middle Right', 'Bottom Left', 'Bottom Center', 'Bottom Right'], group=grpRes)
 
// ═════════════════════════════════════════════════════════════════════════════
//  PANEL
// ═════════════════════════════════════════════════════════════════════════════
grpPan = 'Panel'
 
showPanel = input.bool(true, 'Show Live Phase Panel', group=grpPan,
  tooltip = 'A table showing which phase of the sequence is active right now, which step the AMD model requires next and at what price this script will register it, how many bars remain before the episode expires, and what would invalidate it. It reports the state of the model, not a forecast of price.')
 
panPos = input.string('Top Right', 'Position', options=['Top Left', 'Top Center', 'Top Right', 'Middle Left', 'Middle Right', 'Bottom Left', 'Bottom Center', 'Bottom Right'], group=grpPan)
panSiz = input.string('Small', 'Text Size', options=['Tiny', 'Small', 'Normal', 'Large'], group=grpPan)
panDark = input.bool(true, 'Dark Theme', group=grpPan)
 
// ═════════════════════════════════════════════════════════════════════════════
//  ALERTS
// ═════════════════════════════════════════════════════════════════════════════
grpAl = 'Alerts'
 
alertAcc  = input.bool(false, 'Accumulation Formed', group=grpAl)
alertSwp  = input.bool(false, 'Range Edge Swept (unconfirmed)', group=grpAl)
alertMan  = input.bool(true,  'Manipulation Confirmed', group=grpAl)
alertDist = input.bool(true,  'Distribution Confirmed', group=grpAl)
alertFail = input.bool(false, 'Episode Failed', group=grpAl,
  tooltip = 'Enable the alert types you want, then create ONE alert on this indicator choosing the option: Any alert() function call. Messages include symbol, timeframe and the relevant price levels.')
 
// ═════════════════════════════════════════════════════════════════════════════
//  DETECTION STATE
// ═════════════════════════════════════════════════════════════════════════════
 
float atr = ta.atr(atrLen)
float hh  = ta.highest(high, rangeW)
float ll  = ta.lowest(low, rangeW)
float ph  = ta.pivothigh(swingLen, swingLen)
float pl  = ta.pivotlow(swingLen, swingLen)
float legLo = ta.lowest(low, swingLen * 2)
float legHi = ta.highest(high, swingLen * 2)
 
var int   state     = 0
var int   rangeAge  = 0
var float accHi     = na
var float accLo     = na
var int   sweepDir  = 0
var int   sweepBar  = na
var float sweepExt  = na
var float legOrig   = na
var int   manipBar  = na
var int   lastEpEnd = na
var box   accBox    = na
var box   manBox    = na
 
 
var int cntFormed   = 0
var int cntManip    = 0
var int cntComplete = 0
var int cntFailed   = 0
 
var array<box>   epBoxes  = array.new<box>()
var array<label> epLabels = array.new<label>()
 
var array<float> trkInv = array.new<float>()
var array<float> trkObj = array.new<float>()
var array<int>   trkDir = array.new<int>()
 
var int resReach = 0
var int resInval = 0
 
var array<line> objLines = array.new<line>()
 
var array<line>  poolLines  = array.new<line>()
var array<float> poolLevels = array.new<float>()
var array<bool>  poolIsUp   = array.new<bool>()
var array<bool>  poolActive = array.new<bool>()
 
trimDrawings() =>
    while epBoxes.size() > maxEpisodes * 2
        box.delete(epBoxes.shift())
    while epLabels.size() > maxEpisodes * 3
        label.delete(epLabels.shift())
 
pushPool(line ln, float lvl, bool isUp) =>
    poolLines.push(ln)
    poolLevels.push(lvl)
    poolIsUp.push(isUp)
    poolActive.push(true)
    while poolLines.size() > maxPools * 2
        line old = poolLines.shift()
        if not na(old)
            line.delete(old)
        poolLevels.shift()
        poolIsUp.shift()
        poolActive.shift()
 
bool epFailed    = false
bool manConfirm  = false
bool distConfirm = false
bool sweepStart  = false
bool poolTapUp   = false
bool poolTapDn   = false
 
// ═════════════════════════════════════════════════════════════════════════════
//  ENGINE
// ═════════════════════════════════════════════════════════════════════════════
if barstate.isconfirmed and not na(atr) and atr > 0
    // ── Follow-through: resolve open measurements from earlier confirmations ──
    int nTrk = trkDir.size()
    if nTrk > 0
        for i = nTrk - 1 to 0
            int   td  = trkDir.get(i)
            float inv = trkInv.get(i)
            float obj = trkObj.get(i)
            bool  hitObj = td > 0 ? low <= obj : high >= obj
            bool  hitInv = td > 0 ? high >= inv : low <= inv
            if hitObj or hitInv
                if hitObj and not hitInv
                    resReach := resReach + 1
                else
                    resInval := resInval + 1
                trkDir.remove(i)
                trkInv.remove(i)
                trkObj.remove(i)
 
    // ── Liquidity pools: expire tapped levels, register fresh confirmed pivots ──
    int nPool = poolLines.size()
    if nPool > 0
        for i = 0 to nPool - 1
            if poolActive.get(i)
                float lvl  = poolLevels.get(i)
                bool  isUp = poolIsUp.get(i)
                bool  tapped = isUp ? high >= lvl : low <= lvl
                if tapped
                    poolActive.set(i, false)
                    if isUp
                        poolTapUp := true
                    else
                        poolTapDn := true
                    line ln = poolLines.get(i)
                    if not na(ln)
                        line.set_x2(ln, bar_index)
                        line.set_extend(ln, extend.none)
    if not na(ph)
        line lnH = showPools ? line.new(bar_index - swingLen, ph, bar_index, ph, extend=extend.right, color=color.new(poolHiCol, 30), width=1) : na
        pushPool(lnH, ph, true)
    if not na(pl)
        line lnL = showPools ? line.new(bar_index - swingLen, pl, bar_index, pl, extend=extend.right, color=color.new(poolLoCol, 30), width=1) : na
        pushPool(lnL, pl, false)
 
    float span      = hh - ll
    bool  contained = span <= rangeK * atr
 
    // ── 0: scan for a qualifying accumulation range ─────────────────────────
    if state == 0
        rangeAge := contained ? rangeAge + 1 : 0
        bool cooled = na(lastEpEnd) or bar_index - lastEpEnd >= epCooldown
        if rangeAge >= rangeM and cooled
            accHi := hh
            accLo := ll
            state := 1
            cntFormed := cntFormed + 1
            if showBoxes
                accBox := box.new(bar_index - rangeW + 1, accHi, bar_index, accLo, border_color=color.new(accColor, 35), border_width=1, bgcolor=color.new(accColor, 82))
                epBoxes.push(accBox)
            if showLabels
                epLabels.push(label.new(bar_index, accHi, 'A', style=label.style_label_down, color=color.new(accColor, 20), textcolor=color.white, size=size.small, tooltip='Accumulation: range compressed under ' + str.tostring(rangeK) + ' x ATR for ' + str.tostring(rangeM) + '+ bars. Edges locked.'))
            if alertAcc
                alert(str.format('AMD Phases | {0} {1} | Accumulation formed: {2} - {3}', syminfo.ticker, timeframe.period, str.tostring(accLo, format.mintick), str.tostring(accHi, format.mintick)), alert.freq_once_per_bar_close)
 
    // ── 1: accumulation active — watch for a sweep of either edge ───────────
    else if state == 1
        bool depthHi  = high > accHi + minSweep * atr
        bool depthLo  = low < accLo - minSweep * atr
        bool pierceHi = requirePoolSweep ? depthHi and poolTapUp : depthHi
        bool pierceLo = requirePoolSweep ? depthLo and poolTapDn : depthLo
        if pierceHi or pierceLo
            float upExc = high - accHi
            float dnExc = accLo - low
            sweepDir := pierceHi and pierceLo ? (upExc >= dnExc ? 1 : -1) : pierceHi ? 1 : -1
            sweepBar := bar_index
            sweepExt := sweepDir > 0 ? high : low
            legOrig := sweepDir > 0 ? legLo : legHi
            state := 2
            sweepStart := true
        else if high <= accHi and low >= accLo
            if showBoxes and not na(accBox)
                box.set_right(accBox, bar_index)
        else
            float newHi = math.max(accHi, high)
            float newLo = math.min(accLo, low)
            if newHi - newLo <= rangeK * atr
                accHi := newHi
                accLo := newLo
                if showBoxes and not na(accBox)
                    box.set_right(accBox, bar_index)
                    box.set_top(accBox, accHi)
                    box.set_bottom(accBox, accLo)
            else
                epFailed := true
 
    // ── 2: swept — waiting for the reclaim close back inside ────────────────
    if state == 2
        sweepExt := sweepDir > 0 ? math.max(sweepExt, high) : math.min(sweepExt, low)
        bool closedBeyond = sweepDir > 0 ? close > accHi : close < accLo
        bool closedInside = sweepDir > 0 ? close <= accHi : close >= accLo
        if closedInside
            manConfirm := true
        else if closedBeyond and not allowCloseBeyond
            epFailed := true
        else if bar_index - sweepBar >= reclaimWin
            epFailed := true
 
    // ── 3: manipulation confirmed — waiting for distribution confirmation ───
    if state == 3 and not manConfirm
        float mid        = (accHi + accLo) / 2.0
        float confirmLvl = distMode == 'Structure Break' ? legOrig : distMode == 'Opposite Edge' ? (sweepDir > 0 ? accLo : accHi) : mid
        bool  distHit      = sweepDir > 0 ? close < confirmLvl : close > confirmLvl
        bool  extremeTaken = sweepDir > 0 ? close > sweepExt : close < sweepExt
        if distHit
            distConfirm := true
        else if extremeTaken
            epFailed := true
        else if bar_index - manipBar >= distWin
            epFailed := true
 
// ═════════════════════════════════════════════════════════════════════════════
//  OUTCOME HANDLING
// ═════════════════════════════════════════════════════════════════════════════
if sweepStart and alertSwp
    alert(str.format('AMD Phases | {0} {1} | Range edge swept: {2} at {3} - awaiting reclaim', syminfo.ticker, timeframe.period, sweepDir > 0 ? 'highs' : 'lows', str.tostring(sweepExt, format.mintick)), alert.freq_once_per_bar_close)
 
if manConfirm
    state := 3
    manipBar := bar_index
    cntManip := cntManip + 1
    if showBoxes
        manBox := box.new(sweepBar, sweepDir > 0 ? sweepExt : accLo, bar_index, sweepDir > 0 ? accHi : sweepExt, border_color=color.new(manColor, 35), border_width=1, bgcolor=color.new(manColor, 80))
        epBoxes.push(manBox)
    if showLabels
        epLabels.push(label.new(sweepBar, sweepExt, 'M', style=sweepDir > 0 ? label.style_label_down : label.style_label_up, color=color.new(manColor, 20), textcolor=color.white, size=size.small, tooltip=(sweepDir > 0 ? 'Manipulation: swept the range highs and closed back inside.' : 'Manipulation: swept the range lows and closed back inside.')))
    if alertMan
        alert(str.format('AMD Phases | {0} {1} | Manipulation confirmed: {2} swept and reclaimed (extreme {3})', syminfo.ticker, timeframe.period, sweepDir > 0 ? 'highs' : 'lows', str.tostring(sweepExt, format.mintick)), alert.freq_once_per_bar_close)
    trimDrawings()
 
if distConfirm
    color dCol = sweepDir > 0 ? distDnCol : distUpCol
    cntComplete := cntComplete + 1
    float rDist = math.abs(close - sweepExt)
    if rDist > 0
        float objLvl = sweepDir > 0 ? close - targetR * rDist : close + targetR * rDist
        trkDir.push(sweepDir)
        trkInv.push(sweepExt)
        trkObj.push(objLvl)
        if showObjLine
            line ol = line.new(bar_index, objLvl, bar_index + objBars, objLvl, color=color.new(objCol, 0), style=line.style_solid, width=2)
            objLines.push(ol)
            if showLabels
                epLabels.push(label.new(bar_index + objBars, objLvl, str.tostring(targetR, '#.##') + 'R', style=label.style_label_left, color=color.new(objCol, 20), textcolor=#131722, size=size.tiny, tooltip='Measured objective: ' + str.tostring(targetR, '#.##') + ' R from the distribution close. This is the level the Follow-Through table measures against - not a recommendation to place an order there.'))
            while objLines.size() > maxEpisodes
                line oldOl = objLines.shift()
                if not na(oldOl)
                    line.delete(oldOl)
    if showLabels
        epLabels.push(label.new(bar_index, sweepDir > 0 ? high : low, 'D', style=sweepDir > 0 ? label.style_label_down : label.style_label_up, color=color.new(dCol, 15), textcolor=color.white, size=size.small, tooltip=(sweepDir > 0 ? 'Distribution: confirmed DOWN - closed beyond the confirmation level after the failed sweep of the highs.' : 'Distribution: confirmed UP - closed beyond the confirmation level after the failed sweep of the lows.')))
    if alertDist
        alert(str.format('AMD Phases | {0} {1} | Distribution confirmed {2} at {3}', syminfo.ticker, timeframe.period, sweepDir > 0 ? 'DOWN' : 'UP', str.tostring(close, format.mintick)), alert.freq_once_per_bar_close)
    state := 0
    rangeAge := 0
    lastEpEnd := bar_index
    accHi := na
    accLo := na
    sweepDir := 0
    sweepBar := na
    sweepExt := na
    manipBar := na
    accBox := na
    manBox := na
    trimDrawings()
 
if epFailed
    cntFailed := cntFailed + 1
    if keepFailed
        if not na(accBox)
            box.set_bgcolor(accBox, color.new(accColor, 91))
            box.set_border_color(accBox, color.new(accColor, 55))
            box.set_border_style(accBox, line.style_dashed)
        if not na(manBox)
            box.set_bgcolor(manBox, color.new(manColor, 91))
            box.set_border_color(manBox, color.new(manColor, 55))
            box.set_border_style(manBox, line.style_dashed)
    else
        if not na(accBox)
            box.delete(accBox)
        if not na(manBox)
            box.delete(manBox)
    if alertFail
        alert(str.format('AMD Phases | {0} {1} | Episode failed - the sequence did not complete', syminfo.ticker, timeframe.period), alert.freq_once_per_bar_close)
    state := 0
    rangeAge := 0
    lastEpEnd := bar_index
    accHi := na
    accLo := na
    sweepDir := 0
    sweepBar := na
    sweepExt := na
    manipBar := na
    accBox := na
    manBox := na
 
// ═════════════════════════════════════════════════════════════════════════════
//  PANEL RENDER
// ═════════════════════════════════════════════════════════════════════════════
color pnBg  = panDark ? #161A25 : #FFFFFF
color pnTx  = panDark ? #D1D4DC : #131722
color pnMut = panDark ? #787B86 : #9598A1
 
txtSz = panSiz == 'Tiny' ? size.tiny : panSiz == 'Small' ? size.small : panSiz == 'Normal' ? size.normal : size.large
 
pnPos = panPos == 'Top Left' ? position.top_left : panPos == 'Top Center' ? position.top_center : panPos == 'Top Right' ? position.top_right : panPos == 'Middle Left' ? position.middle_left : panPos == 'Middle Right' ? position.middle_right : panPos == 'Bottom Left' ? position.bottom_left : panPos == 'Bottom Center' ? position.bottom_center : position.bottom_right
 
var table P = table.new(pnPos, 2, 8, bgcolor=pnBg, border_width=1, border_color=color.new(pnMut, 82), frame_color=color.new(pnMut, 55), frame_width=1)
 
f_px(float x) => na(x) ? '-' : str.tostring(x, format.mintick)
 
f_row(int r, string lab, string val, color vc, string tip) =>
    table.cell(P, 0, r, lab, text_color=pnMut, text_size=txtSz, text_halign=text.align_left, tooltip=tip)
    table.cell(P, 1, r, val, text_color=vc, text_size=txtSz, text_halign=text.align_right, tooltip=tip)
 
if barstate.islast
    if not showPanel
        table.clear(P, 0, 0, 1, 7)
    else
        string stName = state == 0 ? 'SCANNING' : state == 1 ? 'ACCUMULATION' : state == 2 ? (sweepDir > 0 ? 'SWEEP HIGH' : 'SWEEP LOW') : 'MANIPULATION'
        string stMet  = state == 0 ? '░░░ 0/3' : state == 3 ? '██░ 2/3' : '█░░ 1/3'
        color  stCol  = state == 0 ? pnMut : state == 1 ? accColor : manColor
 
        float trigHi = na(accHi) ? na : accHi + minSweep * atr
        float trigLo = na(accLo) ? na : accLo - minSweep * atr
        float pMid   = na(accHi) or na(accLo) ? na : (accHi + accLo) / 2.0
        float cfLvl  = distMode == 'Structure Break' ? legOrig : distMode == 'Opposite Edge' ? (sweepDir > 0 ? accLo : accHi) : pMid
 
        string rgTxt = na(accHi) or na(accLo) ? '-' : f_px(accLo) + ' - ' + f_px(accHi)
 
        string nxTxt = state == 0 ? 'Compression under ' + str.tostring(rangeK) + ' x ATR' : state == 1 ? 'Sweep beyond a range edge' : state == 2 ? 'Close back inside the range' : sweepDir > 0 ? 'Close below confirmation level' : 'Close above confirmation level'
 
        string lvTxt = state == 1 ? 'H ' + f_px(trigHi) + '  L ' + f_px(trigLo) : state == 2 ? (sweepDir > 0 ? 'close <= ' + f_px(accHi) : 'close >= ' + f_px(accLo)) : state == 3 ? f_px(cfLvl) : '-'
 
        int barsLeft = state == 2 ? reclaimWin - (bar_index - sweepBar) : state == 3 ? distWin - (bar_index - manipBar) : na
        string wnTxt = state == 0 ? '-' : state == 1 ? 'open' : str.tostring(math.max(barsLeft, 0)) + ' bars left'
 
        string fbTxt = state == 0 ? '-' : state == 1 ? 'Range expands past ' + str.tostring(rangeK) + ' x ATR' : state == 2 ? (allowCloseBeyond ? 'No reclaim inside the window' : 'Any close beyond ' + f_px(sweepDir > 0 ? accHi : accLo)) : 'Close beyond ' + f_px(sweepExt) + ' or timeout'
 
        string epTxt = str.tostring(cntFormed) + ' A  ' + str.tostring(cntManip) + ' M  ' + str.tostring(cntComplete) + ' D' + (cntFormed > 0 ? '  (' + str.tostring(100.0 * cntComplete / cntFormed, '#') + '%)' : '')
 
        string tipNext = 'The next step the AMD model requires, and the price at which this script will register it. This is the condition the script is waiting for, not a forecast of price. Sequences frequently fail - the EPISODES row shows how often the full A-M-D sequence actually completed on the history loaded in this chart.'
        string tipEp   = 'Counted on the bars currently loaded in your chart. A = accumulation ranges locked, M = sweeps that closed back inside, D = sequences that reached distribution confirmation. The percentage is how many locked ranges completed the full sequence. It measures pattern completion only - it is not a trading result and says nothing about profit.'
 
        table.cell(P, 0, 0, 'AMD PHASES', text_color=pnTx, text_size=txtSz, text_halign=text.align_left, bgcolor=color.new(stCol, 86), tooltip='Live state of the Accumulation - Manipulation - Distribution sequence on this chart. Everything is evaluated on closed bars.')
        table.cell(P, 1, 0, stMet, text_color=stCol, text_size=txtSz, text_halign=text.align_right, bgcolor=color.new(stCol, 86), tooltip='How many of the three phases of the current episode are confirmed.')
 
        f_row(1, 'PHASE',    stName, stCol, 'The phase currently confirmed. SCANNING = no qualifying range yet. SWEEP HIGH / SWEEP LOW = an edge was pierced but the reclaim has not happened yet, so manipulation is not confirmed.')
        f_row(2, 'RANGE',    rgTxt,  pnTx,  'The locked edges of the current accumulation range.')
        f_row(3, 'NEXT',     nxTxt,  pnTx,  tipNext)
        f_row(4, 'LEVEL',    lvTxt,  pnTx,  tipNext)
        f_row(5, 'WINDOW',   wnTxt,  pnTx,  'Bars remaining before the current episode expires and is discarded. Accumulation itself has no time limit.')
        f_row(6, 'FAILS IF', fbTxt,  pnTx,  'What would invalidate the current episode and discard it.')
        f_row(7, 'EPISODES', epTxt,  pnTx,  tipEp)
 
// ═════════════════════════════════════════════════════════════════════════════
//  FOLLOW-THROUGH RENDER
// ═════════════════════════════════════════════════════════════════════════════
rsPos = resPos == 'Top Left' ? position.top_left : resPos == 'Top Center' ? position.top_center : resPos == 'Top Right' ? position.top_right : resPos == 'Middle Left' ? position.middle_left : resPos == 'Middle Right' ? position.middle_right : resPos == 'Bottom Left' ? position.bottom_left : resPos == 'Bottom Center' ? position.bottom_center : position.bottom_right
 
var table R = table.new(rsPos, 2, 5, bgcolor=pnBg, border_width=1, border_color=color.new(pnMut, 82), frame_color=color.new(pnMut, 55), frame_width=1)
 
f_rrow(int r, string lab, string val, color vc, string tip) =>
    table.cell(R, 0, r, lab, text_color=pnMut, text_size=txtSz, text_halign=text.align_left, tooltip=tip)
    table.cell(R, 1, r, val, text_color=vc, text_size=txtSz, text_halign=text.align_right, tooltip=tip)
 
if barstate.islast
    if not showRes
        table.clear(R, 0, 0, 1, 4)
    else
        int   nRes  = resReach + resInval
        float rate  = nRes > 0 ? 100.0 * resReach / nRes : na
        float brEven = 100.0 / (1.0 + targetR)
        color rCol   = na(rate) ? pnMut : rate >= brEven ? distUpCol : distDnCol
 
        string tipHdr = 'Measured on the bars currently loaded in this chart. After every confirmed distribution the script records the closing price of the confirmation bar, treats the distance from there to the manipulation extreme as the structural risk, and then follows price forward until it either travels the chosen objective in the confirmed direction or returns to the manipulation extreme. Whichever happens first is the outcome. It measures how the pattern behaved on this history. It is not a trade record, it includes no costs or slippage, and it does not predict future outcomes.'
        string tipTie = 'Bars where price touched both the objective and the manipulation extreme are counted as invalidated, because intrabar order cannot be determined on this data.'
 
        table.cell(R, 0, 0, 'FOLLOW-THROUGH', text_color=pnTx, text_size=txtSz, text_halign=text.align_left, bgcolor=color.new(pnMut, 86), tooltip=tipHdr)
        table.cell(R, 1, 0, str.tostring(targetR, '#.##') + ' R', text_color=pnTx, text_size=txtSz, text_halign=text.align_right, bgcolor=color.new(pnMut, 86), tooltip='The objective distance as a multiple of the structural risk defined by the pattern.')
 
        f_rrow(1, 'REACHED',     str.tostring(resReach), distUpCol, 'Confirmations where price travelled the full objective before returning to the manipulation extreme.')
        f_rrow(2, 'INVALIDATED', str.tostring(resInval), distDnCol, 'Confirmations where price returned to the manipulation extreme first.' + '\n\n' + tipTie)
        f_rrow(3, 'UNRESOLVED',  str.tostring(trkDir.size()), pnMut, 'Confirmations still running: neither the objective nor the manipulation extreme has been touched yet.')
        f_rrow(4, 'RATE',        nRes > 0 ? str.tostring(resReach) + '/' + str.tostring(nRes) + '  (' + str.tostring(rate, '#') + '%)' : '-', rCol, tipHdr + '\n\nThe colour compares this rate to the indifference line for the chosen objective, which is 100 / (1 + objective) = ' + str.tostring(brEven, '#.#') + '%. At that rate the reached and invalidated outcomes cancel out before any costs. It marks which side of that arithmetic line this history fell on. It does not mean the pattern is profitable: commissions, spread and slippage are not included anywhere in this table.')

← 返回列表