ATK/DEF Life Swing MAZE Labyrinth Virtual Walls

ATTDEFS · study · 337 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATK/DEF - Life Swing MAZE: Labyrinth Virtual Walls", shorttitle="ATK/DEF - Life Swing MAZE: Labyrinth Virtual Wall", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
 
// ═══════════════════════════ Inputs ═══════════════════════════
grpInt = "Internal Structure"
internalLen   = input.int(5, "Internal Swing Length", minval=1, group=grpInt)
showInternal  = input.bool(true, "Show IH / IL", group=grpInt)
 
grpColor = "Colors"
colHigh = input.color(#8B1A3A, "IH / Bearish Color", group=grpColor)
colLow  = input.color(#2E8B57, "IL / Bullish Color", group=grpColor)
 
grpDedup = "Deduplication"
dedupPct  = input.float(0.3, "Min Distance (ATR mult, 0=Off)", minval=0.0, step=0.1, group=grpDedup)
dedupBars = input.int(3, "Min Bars Between Swings", minval=0, group=grpDedup)
 
grpBos = "BOS / CHoCH"
showBos     = input.bool(true, "Show BOS", group=grpBos)
showChoch   = input.bool(true, "Show CHoCH", group=grpBos)
sigWidth    = input.int(2, "Line Width", minval=1, maxval=5, group=grpBos)
sigOffset   = input.float(0.4, "Text Offset (ATR mult)", minval=0.0, step=0.1, group=grpBos)
sigFontSize = input.string("small", "Text Size", options=["tiny","small","normal","large"], group=grpBos)
lockChart   = input.bool(true, "Lock Chart (No Repaint)", group=grpBos)
 
grpFIFO = "FIFO Management"
keepIH      = input.int(20, "Keep Recent IH / IL", minval=1, group=grpFIFO)
keepSignal  = input.int(20, "Keep Recent BOS / CHoCH", minval=1, group=grpFIFO)
 
grpOB = "Structure OB (Latest Only)"
showOB      = input.bool(true, "Show OB", group=grpOB)
obExtend    = input.int(10, "OB Extend Right (Bars)", minval=1, group=grpOB)
obTextBull  = input.string("Structure OB+ (IL)", "Bull OB Text", group=grpOB)
obTextBear  = input.string("Structure OB- (IH)", "Bear OB Text", group=grpOB)
 
grpFVG = "FVG (Fair Value Gap)"
showFVG     = input.bool(true, "Show FVG", group=grpFVG)
fvgExtend   = input.int(10, "FVG Extend Right (Bars)", minval=1, group=grpFVG)
keepFVG     = input.int(20, "Keep Recent FVG", minval=1, group=grpFVG)
showLiq     = input.bool(true, "Show Liquidity Hunt Status", group=grpFVG)
 
// ═══════════════════════════ Utilities ═══════════════════════════
f_fsize(string s) =>
    switch s
        "tiny"   => size.tiny
        "small"  => size.small
        "normal" => size.normal
        => size.large
 
// ═══════════════════════════ FIFO Queues ═══════════════════════════
var array<label> ihQueue      = array.new<label>()
var array<line>  sigLineQueue = array.new<line>()
var array<label> sigLblQueue  = array.new<label>()
 
f_fifoLabel(array<label> q, int keep) =>
    while array.size(q) > keep
        label.delete(array.shift(q))
 
f_fifoLine(array<line> q, int keep) =>
    while array.size(q) > keep
        line.delete(array.shift(q))
 
// ═══════════════════════════ OB & FVG States ═══════════════════════════
var box   bullOBBox   = na
var label bullOBLabel = na
var box   bearOBBox   = na
var label bearOBLabel = na
 
var array<box>   fvgBoxes  = array.new<box>()
var array<label> fvgLabels = array.new<label>()
 
// ═══════════════════════════ Swing Points ═══════════════════════════
internalHighPt = ta.pivothigh(high, internalLen, internalLen)
internalLowPt  = ta.pivotlow(low,  internalLen, internalLen)
 
atr     = ta.atr(14)
minDist = atr * dedupPct
yOffset = atr * sigOffset
 
// ═══════════════════════════ Deduplication State ═══════════════════════════
var float lastHighPrice = na
var int   lastHighBar   = na
var float lastLowPrice  = na
var int   lastLowBar    = na
 
highOK = false
if not na(internalHighPt)
    priceOK = na(lastHighPrice) or math.abs(internalHighPt - lastHighPrice) >= minDist
    barOK   = na(lastHighBar)   or (bar_index - lastHighBar) >= dedupBars
    highOK  := priceOK and barOK
 
lowOK = false
if not na(internalLowPt)
    priceOK = na(lastLowPrice) or math.abs(internalLowPt - lastLowPrice) >= minDist
    barOK   = na(lastLowBar)   or (bar_index - lastLowBar) >= dedupBars
    lowOK   := priceOK and barOK
 
if highOK and lowOK
    lowOK := false
 
// ═══════════════════════════ Structure State ═══════════════════════════
var float refHigh    = na
var int   refHighBar = na
var float refLow     = na
var int   refLowBar  = na
var int   trend      = 0
var bool  hBroken    = false
var bool  lBroken    = false
 
// ═══════════════════════════ Lock Chart ═══════════════════════════
canProcess = lockChart ? barstate.isconfirmed : true
 
// ═══════════════════════════ Signal Drawing ═══════════════════════════
f_drawSignal(int refBar, float price, color col, string txt, bool isBull) =>
    startX = refBar
    endX   = bar_index
    if endX > startX
        ln = line.new(startX, price, endX, price,
             color = col, width = sigWidth,
             style = line.style_dashed, xloc = xloc.bar_index)
        array.push(sigLineQueue, ln)
        f_fifoLine(sigLineQueue, keepSignal)
        midX = math.round((startX + endX) / 2)
        lblY = isBull ? price + yOffset : price - yOffset
        lb = label.new(midX, lblY, txt,
             style = label.style_label_center,
             color = color.new(col, 100),
             textcolor = col,
             size = f_fsize(sigFontSize),
             xloc = xloc.bar_index)
        array.push(sigLblQueue, lb)
        f_fifoLabel(sigLblQueue, keepSignal)
 
// ═══════════════════════════ ③ Handle Breaks ═══════════════════════════
bullBreak = canProcess and not na(refHigh) and not hBroken and close > refHigh
bearBreak = canProcess and not na(refLow)  and not lBroken and close < refLow
 
if bullBreak
    hBroken := true
    isBos = (trend == 1)
    if (isBos and showBos) or (not isBos and showChoch)
        txt = isBos ? "BOS ↑" : "CHoCH ↑"
        f_drawSignal(refHighBar, refHigh, colLow, txt, true)
    if not isBos
        trend := 1
 
if bearBreak
    lBroken := true
    isBos = (trend == -1)
    if (isBos and showBos) or (not isBos and showChoch)
        txt = isBos ? "BOS ↓" : "CHoCH ↓"
        f_drawSignal(refLowBar, refLow, colHigh, txt, false)
    if not isBos
        trend := -1
 
// ═══════════════════════════ ① Handle IH (Structure Bear OB) ═══════════════════════════
if canProcess and highOK
    pivotBar = bar_index - internalLen
 
    if showInternal
        ihLabel = label.new(
             pivotBar, internalHighPt, "IH",
             style = label.style_label_down,
             size  = size.small,
             color = color.new(colHigh, 100),
             textcolor = colHigh)
        array.push(ihQueue, ihLabel)
        f_fifoLabel(ihQueue, keepIH)
 
    lastHighPrice := internalHighPt
    lastHighBar   := bar_index
 
    refHigh    := internalHighPt
    refHighBar := pivotBar
    hBroken    := false
 
    // Create/Replace Bear Structure OB
    if showOB
        if not na(bearOBBox)
            box.delete(bearOBBox)
        if not na(bearOBLabel)
            label.delete(bearOBLabel)
 
        topY   = internalHighPt
        botY   = low[internalLen]
        rightX = bar_index + obExtend
        leftX  = pivotBar
 
        baseBull = 45.0
        baseBear = 55.0
        
        if trend == 1
            baseBull += 10
            baseBear -= 10
        else if trend == -1
            baseBull -= 5
            baseBear += 5
 
        buPct = math.round(baseBull)
        bePct = math.round(baseBear)
 
        behaviorStr = buPct > bePct ? "(Bullish Strong)" : bePct > buPct ? "(Bearish Strong)" : "(Balanced)"
 
        txt = obTextBear + "\nBU: " + str.tostring(buPct) + "% | BE: " + str.tostring(bePct) + "%\n" + behaviorStr
 
        if not na(topY) and not na(botY) and topY > botY
            bearOBBox := box.new(leftX, topY, rightX, botY,
                 border_color = colHigh, bgcolor = color.new(colHigh, 85), border_width = 1, xloc = xloc.bar_index)
            midX = math.round((leftX + rightX) / 2)
            midY = (topY + botY) / 2
            bearOBLabel := label.new(midX, midY, txt, style = label.style_label_center,
                 color = color.new(colHigh, 100), textcolor = colHigh, size = size.tiny, xloc = xloc.bar_index)
 
// ═══════════════════════════ ② Handle IL (Structure Bull OB) ═══════════════════════════
if canProcess and lowOK
    pivotBar = bar_index - internalLen
 
    if showInternal
        ilLabel = label.new(
             pivotBar, internalLowPt, "IL",
             style = label.style_label_up,
             size  = size.small,
             color = color.new(colLow, 100),
             textcolor = colLow)
        array.push(ihQueue, ilLabel)
        f_fifoLabel(ihQueue, keepIH)
 
    lastLowPrice := internalLowPt
    lastLowBar   := bar_index
 
    refLow     := internalLowPt
    refLowBar  := pivotBar
    lBroken    := false
 
    // Create/Replace Bull Structure OB
    if showOB
        if not na(bullOBBox)
            box.delete(bullOBBox)
        if not na(bullOBLabel)
            label.delete(bullOBLabel)
 
        topY   = high[internalLen]
        botY   = internalLowPt
        rightX = bar_index + obExtend
        leftX  = pivotBar
 
        baseBull = 60.0
        baseBear = 40.0
        
        if trend == 1
            baseBull += 5
            baseBear -= 5
        else if trend == -1
            baseBull -= 10
            baseBear += 10
 
        buPct = math.round(baseBull)
        bePct = math.round(baseBear)
 
        behaviorStr = buPct > bePct ? "(Bullish Strong)" : bePct > buPct ? "(Bearish Strong)" : "(Balanced)"
 
        txt = obTextBull + "\nBU: " + str.tostring(buPct) + "% | BE: " + str.tostring(bePct) + "%\n" + behaviorStr
 
        if not na(topY) and not na(botY) and topY > botY
            bullOBBox := box.new(leftX, topY, rightX, botY,
                 border_color = colLow, bgcolor = color.new(colLow, 85), border_width = 1, xloc = xloc.bar_index)
            midX = math.round((leftX + rightX) / 2)
            midY = (topY + botY) / 2
            bullOBLabel := label.new(midX, midY, txt, style = label.style_label_center,
                 color = color.new(colLow, 100), textcolor = colLow, size = size.tiny, xloc = xloc.bar_index)
 
// ═══════════════════════════ ④ OB Lifecycle ═══════════════════════════
if canProcess and showOB
    if not na(bearOBBox) and close > box.get_top(bearOBBox)
        box.delete(bearOBBox)
        label.delete(bearOBLabel)
        bearOBBox := na
        bearOBLabel := na
 
    if not na(bullOBBox) and close < box.get_bottom(bullOBBox)
        box.delete(bullOBBox)
        label.delete(bullOBLabel)
        bullOBBox := na
        bullOBLabel := na
 
// ═══════════════════════════ ⑤ FVG Detection & Liquidity Hunt ═══════════════════════════
bullFVG = canProcess and showFVG and low > high[2] and close[1] > open[1]
bearFVG = canProcess and showFVG and high < low[2]  and close[1] < open[1]
 
if bullFVG
    topY = low
    botY = high[2]
    if topY > botY
        isTrueLiq = not na(refHigh) and high > refHigh
        
        // ★ 改为 FVG ↑
        txt = "FVG ↑"
        if showLiq
            txt := isTrueLiq ? "FVG ↑ (True Liquidity)" : "FVG ↑ (False Liquidity)"
            
        b = box.new(bar_index - 2, topY, bar_index + fvgExtend, botY,
             border_color = colLow, bgcolor = color.new(colLow, 80), border_width = 1, xloc = xloc.bar_index)
        midX = math.round((bar_index - 2 + bar_index + fvgExtend) / 2)
        midY = (topY + botY) / 2
        l = label.new(midX, midY, txt, style = label.style_label_center,
             color = color.new(colLow, 100), textcolor = colLow, size = size.tiny, xloc = xloc.bar_index)
        
        array.push(fvgBoxes, b)
        array.push(fvgLabels, l)
        
        if array.size(fvgBoxes) > keepFVG
            box.delete(array.shift(fvgBoxes))
            label.delete(array.shift(fvgLabels))
 
if bearFVG
    topY = low[2]
    botY = high
    if topY > botY
        isTrueLiq = not na(refLow) and low < refLow
        
        // ★ 改为 FVG ↓
        txt = "FVG ↓"
        if showLiq
            txt := isTrueLiq ? "FVG ↓ (True Liquidity)" : "FVG ↓ (False Liquidity)"
 
        b = box.new(bar_index - 2, topY, bar_index + fvgExtend, botY,
             border_color = colHigh, bgcolor = color.new(colHigh, 80), border_width = 1, xloc = xloc.bar_index)
        midX = math.round((bar_index - 2 + bar_index + fvgExtend) / 2)
        midY = (topY + botY) / 2
        l = label.new(midX, midY, txt, style = label.style_label_center,
             color = color.new(colHigh, 100), textcolor = colHigh, size = size.tiny, xloc = xloc.bar_index)
        
        array.push(fvgBoxes, b)
        array.push(fvgLabels, l)
        
        if array.size(fvgBoxes) > keepFVG
            box.delete(array.shift(fvgBoxes))
            label.delete(array.shift(fvgLabels))

← 返回列表