ATK/DEF Life Swing Maze Labyrinth Wall Structure

ATTDEFS · study · 459 行 · 点赞 24 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATK/DEF — Life Swing Maze: Labyrinth Wall Structure", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
 
// ── Custom Types ──────────────────────────────────────
type SwingLabel
    label lbl
    int   bar
    float price
 
// ═══════════════════════════ Inputs ═══════════════════════════
 
// 1. Main Structure
grp_show = "Main Structure Display"
showHHLL  = input.bool(true, "Show Unbroken HH/HL/LH/LL", group=grp_show)
showBOS   = input.bool(true, "Show BOS / CHoCH", group=grp_show)
showPDH   = input.bool(true, "Show PDH (Previous Day High)", group=grp_show)
showPDL   = input.bool(true, "Show PDL (Previous Day Low)", group=grp_show)
 
grp_basic = "Basic Settings"
leftBars  = input.int(3, "Left Bars (Pivot)", minval=1, group=grp_basic)
rightBars = input.int(3, "Right Bars (Confirm Delay)", minval=1, group=grp_basic)
lineWidth = input.int(1, "Structure Line Width", minval=1, group=grp_basic)
textOffset= input.float(0.3, "Text Offset (ATR Multiple)", step=0.1, group=grp_basic)
pdLineLen = input.int(20, "PDH/PDL Line Length (Bars)", minval=1, group=grp_basic)
atrLen    = input.int(14, "ATR Length", minval=1, group=grp_basic)
 
grp_dedup = "Deduplication"
dedupBar  = input.int(20, "Time Tolerance (Bars)", group=grp_dedup)
dedupTol  = input.float(2.0, "Price Tolerance (±Price)", step=0.5, group=grp_dedup)
 
grp_style = "Style"
bullColor = input.color(color.new(color.teal, 0), "Bullish Color (HH/HL)", group=grp_style)
bearColor = input.color(color.new(color.red, 0), "Bearish Color (LH/LL)", group=grp_style)
pdhColor  = input.color(color.new(color.yellow, 0), "PDH Color", group=grp_style)
pdlColor  = input.color(color.new(color.orange, 0), "PDL Color", group=grp_style)
 
// 2. FVG Dual Flow
grpFVG = "FVG Dual Flow"
showFVG        = input.bool(true, "Show FVG (Box + Label + Flow)", group=grpFVG)
fvgUpColor     = input.color(color.new(color.teal, 0), "FVG ↑ Color", group=grpFVG)
fvgDnColor     = input.color(color.new(color.red, 0),  "FVG ↓ Color", group=grpFVG)
fvgAlpha       = input.int(80, "FVG Fill Transparency", minval=0, maxval=100, group=grpFVG)
fvgBorder      = input.bool(true, "FVG Border", group=grpFVG)
fvgBoxWidth    = input.int(10, "FVG Width (Bars)", minval=1, maxval=50, group=grpFVG)
fvgMinSizeATR  = input.float(0.0, "FVG Min Size (ATR)", minval=0.0, step=0.1, group=grpFVG)
fvgLabelSize   = input.string("tiny", "FVG Label Size", options=["tiny","small","normal","large"], group=grpFVG)
fvgMaxKeep     = input.int(50, "FVG Max Kept", minval=1, maxval=500, group=grpFVG)
atrLenFVG      = input.int(14, "ATR Length (FVG)", minval=1, group=grpFVG)
 
grpLiq = "FVG Liquidity"
showFvgLiq   = input.bool(true, "Show Liquidity on FVG Label", group=grpLiq)
liqThreshold = input.float(1.2, "Dominance Threshold (x)", minval=1.0, step=0.1, group=grpLiq)
fvgMaxAge    = input.int(500, "FVG Max Age (Bars, 0=unlimited)", minval=0, maxval=5000, group=grpLiq)
 
// ═══════════════════════════ Helper Functions ═══════════════════════════
f_size(string s) =>
    switch s
        "tiny" => size.tiny
        "small" => size.small
        "normal" => size.normal
        "large" => size.large
 
// ═══════════════════════════ Calculations ═══════════════════════════
ph = ta.pivothigh(high, leftBars, rightBars)
pl = ta.pivotlow(low, leftBars, rightBars)
atrVal = ta.atr(atrLen)
offset = atrVal * textOffset
 
atrFVG   = ta.atr(atrLenFVG)
fvgMinSize = atrFVG * fvgMinSizeATR
 
// ═══════════════════════════ Main Structure Logic ═══════════════════════════
var float lastPH = na
var float lastPL = na
var int   lastPHBar = na
var int   lastPLBar = na
var bool  isUptrend = true 
 
var array<SwingLabel> swingLabels = array.new<SwingLabel>()
var array<line>  bosLines  = array.new<line>() 
var array<label> bosLabels = array.new<label>()
 
var float lastTagPriceH = na
var int   lastTagBarH   = na
var float lastTagPriceL = na
var int   lastTagBarL   = na
var float lastLinePrice = na
var int   lastLineBar   = na
 
var array<float> brokenHighs = array.new<float>()
var array<float> brokenLows  = array.new<float>()
 
// FIFO Cleanup for Main Structure
if array.size(swingLabels) > 50
    SwingLabel old = array.shift(swingLabels)
    label.delete(old.lbl)
if array.size(bosLines) > 50
    line.delete(array.shift(bosLines))
    label.delete(array.shift(bosLabels))
if array.size(brokenHighs) > 50
    array.shift(brokenHighs)
if array.size(brokenLows) > 50
    array.shift(brokenLows)
 
isDup(float p, float lastP, int lastB) =>
    not na(lastP) and math.abs(p - lastP) <= dedupTol and (bar_index - lastB) <= dedupBar
 
isBroken(array<float> arr, float p) =>
    result = false
    if array.size(arr) > 0
        for i = 0 to array.size(arr) - 1
            if math.abs(array.get(arr, i) - p) <= 0.01 
                result := true
                break
    result
 
// BOS / CHoCH Detection
if ta.crossover(close, lastPH) and not na(lastPH)
    isBOS = isUptrend
    isCHoCH = not isUptrend
    array.push(brokenHighs, lastPH)
    if showBOS and not isDup(lastPH, lastLinePrice, lastLineBar)
        string txt = isBOS ? "BOS" : "CHoCH"
        l = line.new(lastPHBar, lastPH, bar_index, lastPH, color=bullColor, style=line.style_dashed, width=lineWidth, extend=extend.none)
        int midBar = math.round((lastPHBar + bar_index) / 2)
        lb = label.new(midBar, lastPH + offset, txt, style=label.style_none, textcolor=bullColor, size=size.small)
        array.push(bosLines, l)
        array.push(bosLabels, lb)
        lastLinePrice := lastPH
        lastLineBar := bar_index
    isUptrend := true 
 
if ta.crossunder(close, lastPL) and not na(lastPL)
    isBOS = not isUptrend
    isCHoCH = isUptrend
    array.push(brokenLows, lastPL)
    if showBOS and not isDup(lastPL, lastLinePrice, lastLineBar)
        string txt = isBOS ? "BOS" : "CHoCH"
        l = line.new(lastPLBar, lastPL, bar_index, lastPL, color=bearColor, style=line.style_dashed, width=lineWidth, extend=extend.none)
        int midBar = math.round((lastPLBar + bar_index) / 2)
        lb = label.new(midBar, lastPL - offset, txt, style=label.style_none, textcolor=bearColor, size=size.small)
        array.push(bosLines, l)
        array.push(bosLabels, lb)
        lastLinePrice := lastPL
        lastLineBar := bar_index
    isUptrend := false 
 
// HH/HL/LH/LL Labels
if not na(ph)
    if not na(lastPH)
        tag = ph > lastPH ? "HH" : "LH"
        col = ph > lastPH ? bullColor : bearColor
        bool wasBroken = isBroken(brokenHighs, ph)
        if showHHLL and not wasBroken and not isDup(ph, lastTagPriceH, lastTagBarH)
            lbl = label.new(bar_index - rightBars, ph + offset, tag, style=label.style_none, textcolor=col, size=size.small)
            array.push(swingLabels, SwingLabel.new(lbl, bar_index - rightBars, ph))
            lastTagPriceH := ph
            lastTagBarH := bar_index
    lastPH := ph
    lastPHBar := bar_index - rightBars
 
if not na(pl)
    if not na(lastPL)
        tag = pl < lastPL ? "LL" : "HL"
        col = pl < lastPL ? bearColor : bullColor
        bool wasBroken = isBroken(brokenLows, pl)
        if showHHLL and not wasBroken and not isDup(pl, lastTagPriceL, lastTagBarL)
            lbl = label.new(bar_index - rightBars, pl - offset, tag, style=label.style_none, textcolor=col, size=size.small)
            array.push(swingLabels, SwingLabel.new(lbl, bar_index - rightBars, pl))
            lastTagPriceL := pl
            lastTagBarL := bar_index
    lastPL := pl
    lastPLBar := bar_index - rightBars
 
// PDH/PDL
[dOpen, dHigh, dLow, dClose] = request.security(syminfo.tickerid, "D", [open, high, low, close], lookahead=barmerge.lookahead_on)
float pdh = dHigh[1]
float pdl = dLow[1]
var line pdhLine = na, var line pdlLine = na
var label pdhLabel = na, var label pdlLabel = na
if timeframe.change("D")
    line.delete(pdhLine), line.delete(pdlLine)
    label.delete(pdhLabel), label.delete(pdlLabel)
    int startBar = bar_index
    if showPDH and not na(pdh)
        pdhLine := line.new(startBar, pdh, startBar + pdLineLen, pdh, color=pdhColor, style=line.style_dotted, width=2, extend=extend.none)
        pdhLabel := label.new(startBar + pdLineLen, pdh, "PDH", style=label.style_label_left, textcolor=pdhColor, color=color.new(color.white, 100), size=size.small)
    if showPDL and not na(pdl)
        pdlLine := line.new(startBar, pdl, startBar + pdLineLen, pdl, color=pdlColor, style=line.style_dotted, width=2, extend=extend.none)
        pdlLabel := label.new(startBar + pdLineLen, pdl, "PDL", style=label.style_label_left, textcolor=pdlColor, color=color.new(color.white, 100), size=size.small)
 
// ═══════════════════════════ FVG Dual Flow ═══════════════════════════
fvgBullColor = color.new(fvgUpColor, fvgAlpha)
fvgBearColor = color.new(fvgDnColor, fvgAlpha)
 
float curBullVol = close > open ? volume : 0.0
float curBearVol = close < open ? volume : 0.0
 
bullFvgTxt(int st) =>
    switch st
        1 => "FVG ↑ Coalesce ➡"
        2 => "FVG ↑ Form ⬆"
        3 => "FVG ↑ Boundary ⬆"
        4 => "FVG ↑ Run ⬆"
        5 => "FVG ↑ Filter ⬇"
        6 => "FVG ↑ Converge ⬇"
        7 => "FVG ↑ Transform ⬆"
        8 => "FVG ↑ Conceal ⬆"
        9 => "FVG ↑ Rebirth ⬇"
        => "FVG ↑ Coalesce ➡"
 
bearFvgTxt(int st) =>
    switch st
        1 => "FVG ↓ Coalesce ➡"
        2 => "FVG ↓ Form ⬇"
        3 => "FVG ↓ Boundary ⬇"
        4 => "FVG ↓ Run ⬇"
        5 => "FVG ↓ Filter ⬆"
        6 => "FVG ↓ Converge ⬆"
        7 => "FVG ↓ Transform ⬇"
        8 => "FVG ↓ Conceal ⬇"
        9 => "FVG ↓ Rebirth ⬆"
        => "FVG ↓ Coalesce ➡"
 
liqText(float bullVol, float bearVol) =>
    float total = bullVol + bearVol
    if total <= 0
        "\n[L: —]"
    else
        float bullPct = bullVol / total * 100
        float bearPct = bearVol / total * 100
        string state = "Balanced"
        if bullVol > bearVol * liqThreshold
            state := "Bull Dominant"
        else if bearVol > bullVol * liqThreshold
            state := "Bear Dominant"
        "\n[L: " + state + " | Bull " + str.tostring(bullPct, "#") + "% | Bear " + str.tostring(bearPct, "#") + "%]"
 
var array<box>   bullFvgBoxes  = array.new<box>()
var array<box>   bearFvgBoxes  = array.new<box>()
var array<label> bullFvgLabels = array.new<label>()
var array<label> bearFvgLabels = array.new<label>()
 
var array<int>   bullFvgStage  = array.new<int>()
var array<float> bullFvgTop    = array.new<float>()
var array<float> bullFvgBot    = array.new<float>()
var array<int>   bullFvgBar    = array.new<int>()
 
var array<int>   bearFvgStage  = array.new<int>()
var array<float> bearFvgTop    = array.new<float>()
var array<float> bearFvgBot    = array.new<float>()
var array<int>   bearFvgBar    = array.new<int>()
 
var array<float> bullFvgBullVol = array.new<float>()
var array<float> bullFvgBearVol = array.new<float>()
var array<float> bearFvgBullVol = array.new<float>()
var array<float> bearFvgBearVol = array.new<float>()
 
// FVG Detection
if showFVG and low > high[2]
    float gapTop = low
    float gapBot = high[2]
    if gapTop - gapBot >= fvgMinSize
        box b = box.new(bar_index, gapTop, bar_index + fvgBoxWidth, gapBot, border_color = fvgBorder ? color.new(fvgUpColor, 50) : na, border_width = fvgBorder ? 1 : 0, bgcolor = fvgBullColor, xloc = xloc.bar_index)
        array.push(bullFvgBoxes, b)
        int midX = bar_index + math.round(fvgBoxWidth / 2)
        float midY = (gapTop + gapBot) / 2
        lbl = label.new(midX, midY, bullFvgTxt(1), style=label.style_none, textcolor=fvgUpColor, size=f_size(fvgLabelSize), xloc=xloc.bar_index)
        array.push(bullFvgLabels, lbl)
        array.push(bullFvgStage, 1)
        array.push(bullFvgTop, gapTop)
        array.push(bullFvgBot, gapBot)
        array.push(bullFvgBar, bar_index)
        array.push(bullFvgBullVol, curBullVol)
        array.push(bullFvgBearVol, curBearVol)
 
if showFVG and high < low[2]
    float gapTop = low[2]
    float gapBot = high
    if gapTop - gapBot >= fvgMinSize
        box b = box.new(bar_index, gapTop, bar_index + fvgBoxWidth, gapBot, border_color = fvgBorder ? color.new(fvgDnColor, 50) : na, border_width = fvgBorder ? 1 : 0, bgcolor = fvgBearColor, xloc = xloc.bar_index)
        array.push(bearFvgBoxes, b)
        int midX = bar_index + math.round(fvgBoxWidth / 2)
        float midY = (gapTop + gapBot) / 2
        lbl = label.new(midX, midY, bearFvgTxt(1), style=label.style_none, textcolor=fvgDnColor, size=f_size(fvgLabelSize), xloc=xloc.bar_index)
        array.push(bearFvgLabels, lbl)
        array.push(bearFvgStage, 1)
        array.push(bearFvgTop, gapTop)
        array.push(bearFvgBot, gapBot)
        array.push(bearFvgBar, bar_index)
        array.push(bearFvgBullVol, curBullVol)
        array.push(bearFvgBearVol, curBearVol)
 
// FVG Incremental Update & Stage Advance
if showFVG and array.size(bullFvgStage) > 0
    int i = 0
    while i < array.size(bullFvgStage)
        int stage = array.get(bullFvgStage, i)
        float gTop = array.get(bullFvgTop, i)
        float gBot = array.get(bullFvgBot, i)
        int gBar = array.get(bullFvgBar, i)
        label lb = array.get(bullFvgLabels, i)
        if bar_index > gBar
            array.set(bullFvgBullVol, i, array.get(bullFvgBullVol, i) + curBullVol)
            array.set(bullFvgBearVol, i, array.get(bullFvgBearVol, i) + curBearVol)
            if stage == 1
                if low <= gTop
                    array.set(bullFvgStage, i, 2)
            else if stage == 2
                if close > gTop
                    array.set(bullFvgStage, i, 3)
            else if stage == 3
                if close < gTop
                    array.set(bullFvgStage, i, 4)
            else if stage == 4
                if low <= gBot
                    array.set(bullFvgStage, i, 5)
            else if stage == 5
                if close > gBot
                    array.set(bullFvgStage, i, 6)
            else if stage == 6
                if close > gTop
                    array.set(bullFvgStage, i, 7)
            else if stage == 7
                if close < gTop
                    array.set(bullFvgStage, i, 8)
            else if stage == 8
                if close < gBot
                    array.set(bullFvgStage, i, 9)
            else if stage == 9
                array.set(bullFvgStage, i, 1)
            int newStage = array.get(bullFvgStage, i)
            string baseTxt = bullFvgTxt(newStage)
            if showFvgLiq
                float bv = array.get(bullFvgBullVol, i)
                float sv = array.get(bullFvgBearVol, i)
                baseTxt := baseTxt + liqText(bv, sv)
            label.set_text(lb, baseTxt)
        i += 1
 
if showFVG and array.size(bearFvgStage) > 0
    int i = 0
    while i < array.size(bearFvgStage)
        int stage = array.get(bearFvgStage, i)
        float gTop = array.get(bearFvgTop, i)
        float gBot = array.get(bearFvgBot, i)
        int gBar = array.get(bearFvgBar, i)
        label lb = array.get(bearFvgLabels, i)
        if bar_index > gBar
            array.set(bearFvgBullVol, i, array.get(bearFvgBullVol, i) + curBullVol)
            array.set(bearFvgBearVol, i, array.get(bearFvgBearVol, i) + curBearVol)
            if stage == 1
                if high >= gBot
                    array.set(bearFvgStage, i, 2)
            else if stage == 2
                if close < gBot
                    array.set(bearFvgStage, i, 3)
            else if stage == 3
                if close > gBot
                    array.set(bearFvgStage, i, 4)
            else if stage == 4
                if high >= gTop
                    array.set(bearFvgStage, i, 5)
            else if stage == 5
                if close < gTop
                    array.set(bearFvgStage, i, 6)
            else if stage == 6
                if close < gBot
                    array.set(bearFvgStage, i, 7)
            else if stage == 7
                if close > gBot
                    array.set(bearFvgStage, i, 8)
            else if stage == 8
                if close > gTop
                    array.set(bearFvgStage, i, 9)
            else if stage == 9
                array.set(bearFvgStage, i, 1)
            int newStage = array.get(bearFvgStage, i)
            string baseTxt = bearFvgTxt(newStage)
            if showFvgLiq
                float bv = array.get(bearFvgBullVol, i)
                float sv = array.get(bearFvgBearVol, i)
                baseTxt := baseTxt + liqText(bv, sv)
            label.set_text(lb, baseTxt)
        i += 1
 
// FVG FIFO Cleanup
if showFVG
    if array.size(bullFvgBoxes) > 0
        int i = 0
        while i < array.size(bullFvgBoxes)
            box b = array.get(bullFvgBoxes, i)
            int gBar = array.get(bullFvgBar, i)
            bool expired = fvgMaxAge > 0 and (bar_index - gBar) > fvgMaxAge
            if not na(b)
                if close < box.get_bottom(b) or expired
                    box.delete(b)
                    array.remove(bullFvgBoxes, i)
                    if i < array.size(bullFvgLabels)
                        label.delete(array.get(bullFvgLabels, i))
                        array.remove(bullFvgLabels, i)
                    if i < array.size(bullFvgStage)
                        array.remove(bullFvgStage, i)
                        array.remove(bullFvgTop, i)
                        array.remove(bullFvgBot, i)
                        array.remove(bullFvgBar, i)
                        array.remove(bullFvgBullVol, i)
                        array.remove(bullFvgBearVol, i)
                else
                    i += 1
            else
                i += 1
 
    if array.size(bearFvgBoxes) > 0
        int i = 0
        while i < array.size(bearFvgBoxes)
            box b = array.get(bearFvgBoxes, i)
            int gBar = array.get(bearFvgBar, i)
            bool expired = fvgMaxAge > 0 and (bar_index - gBar) > fvgMaxAge
            if not na(b)
                if close > box.get_top(b) or expired
                    box.delete(b)
                    array.remove(bearFvgBoxes, i)
                    if i < array.size(bearFvgLabels)
                        label.delete(array.get(bearFvgLabels, i))
                        array.remove(bearFvgLabels, i)
                    if i < array.size(bearFvgStage)
                        array.remove(bearFvgStage, i)
                        array.remove(bearFvgTop, i)
                        array.remove(bearFvgBot, i)
                        array.remove(bearFvgBar, i)
                        array.remove(bearFvgBullVol, i)
                        array.remove(bearFvgBearVol, i)
                else
                    i += 1
            else
                i += 1
 
    while array.size(bullFvgBoxes) > fvgMaxKeep
        box.delete(array.shift(bullFvgBoxes))
        if array.size(bullFvgLabels) > 0
            label.delete(array.shift(bullFvgLabels))
            array.shift(bullFvgStage)
            array.shift(bullFvgTop)
            array.shift(bullFvgBot)
            array.shift(bullFvgBar)
            array.shift(bullFvgBullVol)
            array.shift(bullFvgBearVol)
 
    while array.size(bearFvgBoxes) > fvgMaxKeep
        box.delete(array.shift(bearFvgBoxes))
        if array.size(bearFvgLabels) > 0
            label.delete(array.shift(bearFvgLabels))
            array.shift(bearFvgStage)
            array.shift(bearFvgTop)
            array.shift(bearFvgBot)
            array.shift(bearFvgBar)
            array.shift(bearFvgBullVol)
            array.shift(bearFvgBearVol)

← 返回列表