ATK/DEF Quant ICT Timeframe Piggy Score

ATTDEFS · study · 385 行 · 点赞 16 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATK/DEF Quant ICT Timeframe Piggy Score", shorttitle="ATK/DEF Timeframe Piggy Score Chart UV", overlay=true, max_labels_count=500, max_lines_count=500, max_boxes_count=500)
 
// ═══════════════════════════ 1. Input Settings ═══════════════════════════
grp_ui = "Dashboard & UI Settings"
showPanel   = input.bool(true, "✅ Show Right Data Panel", group=grp_ui)
panelPos    = input.string("Top Right", "Panel Position", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group=grp_ui)
panelSize   = input.string("normal", "Panel Text Size", options=["tiny", "small", "normal", "large"], group=grp_ui)
 
grp_swing = "Swing Points Settings"
showSwings = input.bool(true, "Show HH/HL/LH/LL Labels", group=grp_swing)
swingLen  = input.int(5, "Swing Lookback Period", minval=1, group=grp_swing)
bullColor = input.color(color.new(color.teal, 0), "Bullish (HH/HL) Color", group=grp_swing)
bearColor = input.color(color.new(color.red, 0), "Bearish (LH/LL) Color", group=grp_swing)
labelSize = input.string("small", "Chart Text Size", options=["tiny", "small", "normal", "large"], group=grp_swing)
 
grp_tf = "Multi-TF Range Settings"
htfTf    = input.timeframe("W",   "HTF Timeframe", group=grp_tf)
mtfTf    = input.timeframe("D",   "MTF Timeframe", group=grp_tf)
ltfTf    = input.timeframe("240", "LTF Timeframe", group=grp_tf)
rangeOffset = input.int(20, "Right Candle Offset (bars)", minval=5, maxval=500, group=grp_tf)
 
grp_switch = "Chart Objects Toggle"
showRanges  = input.bool(true, "Show Multi-TF Range", group=grp_switch)
showHTF     = input.bool(true, "Show HTF", group=grp_switch)
showMTF     = input.bool(true, "Show MTF", group=grp_switch)
showLTF     = input.bool(true, "Show LTF", group=grp_switch)
enableAlerts = input.bool(true, "Enable Alerts", group=grp_switch)
showExtLines = input.bool(true, "Show Extension Lines", group=grp_switch)
 
htfColor = input.color(color.new(#F44336, 0), "HTF Color", group=grp_switch)
mtfColor = input.color(color.new(#FFEB3B, 0), "MTF Color", group=grp_switch)
ltfColor = input.color(color.new(#2196F3, 0), "LTF Color", group=grp_switch)
 
grp_st = "Supertrend Settings"
showST = input.bool(true, "Show Supertrend", group=grp_st)
stAtrPeriod = input.int(10, "ATR Period", minval=1, group=grp_st)
stFactor = input.float(3.0, "ATR Multiplier", minval=0.1, step=0.1, group=grp_st)
stColorUp = input.color(color.new(#00E676, 0), "Up Trend Color", group=grp_st)
stColorDn = input.color(color.new(#FF5252, 0), "Down Trend Color", group=grp_st)
showBreak = input.bool(true, "Show 'Break' Labels", group=grp_st)
 
// ═══════════════════════════ 2. Helper Functions ═══════════════════════════
f_adx() =>
    [diplus, diminus, adx] = ta.dmi(14, 14)
    adx
 
f_get_size(string s) =>
    string result = size.normal
    if s == "tiny"
        result := size.tiny
    else if s == "small"
        result := size.small
    else if s == "normal"
        result := size.normal
    else
        result := size.large
    result
 
// ═══════════════════════════ 3. Core Metrics Calculation ═══════════════════════════
calc_score(bool isHTF, bool isMTF, bool isLTF, string tf) =>
    [c, h, l, rsi, adx_val, atr, sma20] = request.security(syminfo.tickerid, tf, [close, high, low, ta.rsi(close, 14), f_adx(), ta.atr(14), ta.sma(close, 20)], lookahead=barmerge.lookahead_off)
    
    float score_restore = (rsi > 70 or rsi < 30) ? 100 - math.abs(50 - rsi) * 2 : 50
    float atr_pct = (atr / c) * 100
    float score_dissipate = math.min(atr_pct * 500, 100)
    float score_trend = math.min(adx_val * 2, 100)
    float dev = (c - sma20) / sma20 * 100
    float score_temp = math.min(math.abs(dev) * 200, 100)
    
    int consecutive = 0
    if close > open
        consecutive := 1
        for i = 1 to 5
            if close[i] > open[i]
                consecutive += 1
            else
                break
    else if close < open
        consecutive := 1
        for i = 1 to 5
            if close[i] < open[i]
                consecutive += 1
            else
                break
    float score_resilience = math.min(consecutive * 20, 100)
    
    float total_score = (score_restore + score_dissipate + score_trend + score_temp + score_resilience) / 5
    
    string signal = "WAIT"
    if total_score > 75
        signal := "STRONG BUY"
    else if total_score > 60
        signal := "BUY"
    else if total_score < 25
        signal := "STRONG SELL"
    else if total_score < 40
        signal := "SELL"
    else if total_score > 50 and total_score <= 60
        signal := "WEAK"
        
    [score_restore, score_dissipate, score_trend, score_temp, score_resilience, signal, total_score]
 
// Extract data
[htfRes, htfDis, htfTre, htfTem, htfResil, htfSig, htfTotal] = calc_score(true, false, false, htfTf)
[mtfRes, mtfDis, mtfTre, mtfTem, mtfResil, mtfSig, mtfTotal] = calc_score(false, true, false, mtfTf)
[ltfRes, ltfDis, ltfTre, ltfTem, ltfResil, ltfSig, ltfTotal] = calc_score(false, false, true, ltfTf)
 
// ═══════════════════════════ 4. Dashboard Table ═══════════════════════════
var string posStr = panelPos == "Top Right" ? position.top_right : panelPos == "Bottom Right" ? position.bottom_right : panelPos == "Top Left" ? position.top_left : position.bottom_left
var table panel = table.new(posStr, 8, 6, bgcolor=color.new(#1e1e1e, 10), border_width=1, border_color=color.new(color.white, 80))
 
color cHeader = color.new(#6200EA, 20) // Violet
color cHTF = color.new(#FF5252, 80) // Light Red
color cMTF = color.new(#FFCA28, 80) // Light Yellow
color cLTF = color.new(#448AFF, 80) // Light Blue
color cTxt = color.new(color.white, 0)
color cNum = color.new(#AEEA00, 0) // Neon Green Text
 
string tSize = f_get_size(panelSize)
 
if barstate.islast and showPanel
    table.cell(panel, 0, 0, "✨ TF", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 1, 0, "🔋 Restore", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 2, 0, "🔥 Energy", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 3, 0, "🚀 Trend", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 4, 0, "🌡️ Temp", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 5, 0, "🛡️ Resilience", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
    table.cell(panel, 6, 0, "🎯 Signal", bgcolor=cHeader, text_color=cTxt, text_size=tSize)
 
    // HTF Row
    table.cell(panel, 0, 1, "HTF", bgcolor=cHTF, text_color=htfColor, text_size=tSize)
    table.cell(panel, 1, 1, str.tostring(math.round(htfRes)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 2, 1, str.tostring(math.round(htfDis)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 3, 1, str.tostring(math.round(htfTre)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 4, 1, str.tostring(math.round(htfTem)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 5, 1, str.tostring(math.round(htfResil)) + "pts", text_color=cNum, text_size=tSize)
    
    color htfSigBg = str.contains(htfSig, "BUY") ? color.new(#00E676, 30) : str.contains(htfSig, "SELL") ? color.new(#FF1744, 30) : color.new(#9E9E9E, 30)
    table.cell(panel, 6, 1, htfSig, bgcolor=htfSigBg, text_color=color.white, text_size=tSize)
 
    // MTF Row
    table.cell(panel, 0, 2, "MTF", bgcolor=cMTF, text_color=mtfColor, text_size=tSize)
    table.cell(panel, 1, 2, str.tostring(math.round(mtfRes)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 2, 2, str.tostring(math.round(mtfDis)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 3, 2, str.tostring(math.round(mtfTre)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 4, 2, str.tostring(math.round(mtfTem)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 5, 2, str.tostring(math.round(mtfResil)) + "pts", text_color=cNum, text_size=tSize)
    
    color mtfSigBg = str.contains(mtfSig, "BUY") ? color.new(#00E676, 30) : str.contains(mtfSig, "SELL") ? color.new(#FF1744, 30) : color.new(#9E9E9E, 30)
    table.cell(panel, 6, 2, mtfSig, bgcolor=mtfSigBg, text_color=color.white, text_size=tSize)
 
    // LTF Row
    table.cell(panel, 0, 3, "LTF", bgcolor=cLTF, text_color=ltfColor, text_size=tSize)
    table.cell(panel, 1, 3, str.tostring(math.round(ltfRes)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 2, 3, str.tostring(math.round(ltfDis)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 3, 3, str.tostring(math.round(ltfTre)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 4, 3, str.tostring(math.round(ltfTem)) + "pts", text_color=cNum, text_size=tSize)
    table.cell(panel, 5, 3, str.tostring(math.round(ltfResil)) + "pts", text_color=cNum, text_size=tSize)
    
    color ltfSigBg = str.contains(ltfSig, "BUY") ? color.new(#00E676, 30) : str.contains(ltfSig, "SELL") ? color.new(#FF1744, 30) : color.new(#9E9E9E, 30)
    table.cell(panel, 6, 3, ltfSig, bgcolor=ltfSigBg, text_color=color.white, text_size=tSize)
 
// ═══════════════════════════ 5. Swing Point Logic ═══════════════════════════
f_size(string s) =>
    string result = s
    switch result
        "tiny" => size.tiny
        "small" => size.small
        "normal" => size.normal
        => size.large
 
f_classify(string typ, float price, float ref) =>
    string result = na
    if na(ref)
        result := typ == "H" ? "H" : "L"
    else if typ == "H"
        result := price > ref ? "HH" : "LH"
    else
        result := price < ref ? "LL" : "HL"
    result
 
f_color(string cls) =>
    color result = color.gray
    if cls == "HH" or cls == "HL"
        result := bullColor
    else if cls == "LH" or cls == "LL"
        result := bearColor
    result
 
swingHighPt = ta.pivothigh(high, swingLen, swingLen)
swingLowPt  = ta.pivotlow(low, swingLen, swingLen)
 
var float chainPrice = na
var int   chainBar   = na
var string chainType = na
var float refHigh = na
var float refLow  = na
var label curLabel = na
 
if not na(swingHighPt)
    float pPrice = swingHighPt
    int   pBar   = bar_index - swingLen
    if na(chainType)
        chainType  := "H"
        chainPrice := pPrice
        chainBar   := pBar
    else if chainType == "H"
        if pPrice > chainPrice
            if not na(curLabel)
                label.delete(curLabel)
            chainPrice := pPrice
            chainBar   := pBar
            if showSwings
                string cls = f_classify("H", pPrice, refHigh)
                color  col = f_color(cls)
                curLabel := label.new(pBar, pPrice, cls, style=label.style_label_down, size=f_size(labelSize), color=color.new(col, 15), textcolor=color.white)
    else
        refLow := chainPrice
        if showSwings
            string cls = f_classify("H", pPrice, refHigh)
            color  col = f_color(cls)
            curLabel := label.new(pBar, pPrice, cls, style=label.style_label_down, size=f_size(labelSize), color=color.new(col, 15), textcolor=color.white)
        chainType  := "H"
        chainPrice := pPrice
        chainBar   := pBar
 
if not na(swingLowPt)
    float pPrice = swingLowPt
    int   pBar   = bar_index - swingLen
    if na(chainType)
        chainType  := "L"
        chainPrice := pPrice
        chainBar   := pBar
    else if chainType == "L"
        if pPrice < chainPrice
            if not na(curLabel)
                label.delete(curLabel)
            chainPrice := pPrice
            chainBar   := pBar
            if showSwings
                string cls = f_classify("L", pPrice, refLow)
                color  col = f_color(cls)
                curLabel := label.new(pBar, pPrice, cls, style=label.style_label_up, size=f_size(labelSize), color=color.new(col, 15), textcolor=color.white)
    else
        refHigh := chainPrice
        if showSwings
            string cls = f_classify("L", pPrice, refLow)
            color  col = f_color(cls)
            curLabel := label.new(pBar, pPrice, cls, style=label.style_label_up, size=f_size(labelSize), color=color.new(col, 15), textcolor=color.white)
        chainType  := "L"
        chainPrice := pPrice
        chainBar   := pBar
 
// ═══════════════════════════ 6. Supertrend ═══════════════════════════
[stLine, stDir] = ta.supertrend(stFactor, stAtrPeriod)
 
var int   stStartBar = na
var float stStartPrice = na
var bool  isSTLong = false
var bool  isFirstBar = true
 
if isFirstBar
    isSTLong := stDir < 0
    stStartBar := bar_index
    stStartPrice := stLine
    isFirstBar := false
 
var line[] stLines = array.new_line()
 
if stDir < 0 and not isSTLong
    isSTLong := true
    stStartBar := bar_index
    stStartPrice := stLine
    if showBreak
        label.new(bar_index, stLine, "Break", style=label.style_label_up, color=color.new(stColorUp, 15), textcolor=color.white, size=size.small)
else if stDir >= 0 and isSTLong
    isSTLong := false
    stStartBar := bar_index
    stStartPrice := stLine
    if showBreak
        label.new(bar_index, stLine, "Break", style=label.style_label_down, color=color.new(stColorDn, 15), textcolor=color.white, size=size.small)
 
if showST
    var line curStLine = na
    if na(curStLine) or (stStartBar == bar_index)
        curStLine := line.new(stStartBar, stStartPrice, bar_index, stLine, color=isSTLong ? stColorUp : stColorDn, width=2, xloc=xloc.bar_index)
        array.push(stLines, curStLine)
        if array.size(stLines) > 20
            line.delete(array.shift(stLines))
    else
        line.set_xy2(curStLine, bar_index, stLine)
 
// ═══════════════════════════ 7. Multi-TF HIGH / LOW Range & Distance % ═══════════════════════════
[htfH, htfL, htfO, htfC] = request.security(syminfo.tickerid, htfTf, [high, low, open, close], lookahead=barmerge.lookahead_off)
[mtfH, mtfL, mtfO, mtfC] = request.security(syminfo.tickerid, mtfTf, [high, low, open, close], lookahead=barmerge.lookahead_off)
[ltfH, ltfL, ltfO, ltfC] = request.security(syminfo.tickerid, ltfTf, [high, low, open, close], lookahead=barmerge.lookahead_off)
 
var line[]  rangeLines  = array.new_line()
var label[] rangeLabels = array.new_label()
var box[]   rangeBoxes  = array.new_box()
 
if barstate.islast
    int rightX = bar_index + rangeOffset
    int rightX2 = rightX + 10
    int rightX3 = rightX2 + 10
    int endX = rightX3 + 50
    
    float distHTFH = (htfH - close) / close * 100
    float distHTFL = (close - htfL) / close * 100
    float distMTFH = (mtfH - close) / close * 100
    float distMTFL = (close - mtfL) / close * 100
    float distLTFH = (ltfH - close) / close * 100
    float distLTFL = (close - ltfL) / close * 100
 
    while array.size(rangeLines) > 0
        line.delete(array.shift(rangeLines))
    while array.size(rangeLabels) > 0
        label.delete(array.shift(rangeLabels))
    while array.size(rangeBoxes) > 0
        box.delete(array.shift(rangeBoxes))
 
    if showRanges
        // HTF
        if showHTF and not na(htfH)
            if showExtLines
                array.push(rangeLines, line.new(bar_index, htfH, endX, htfH, color=color.new(htfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
                array.push(rangeLines, line.new(bar_index, htfL, endX, htfL, color=color.new(htfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
            
            color cBody = htfC >= htfO ? color.new(htfColor, 30) : color.new(htfColor, 70)
            array.push(rangeLines, line.new(rightX, htfL, rightX, htfH, color=htfColor, width=1, xloc=xloc.bar_index))
            array.push(rangeBoxes, box.new(rightX - 2, math.max(htfO, htfC), rightX + 2, math.min(htfO, htfC), border_color=htfColor, bgcolor=cBody, xloc=xloc.bar_index))
            
            array.push(rangeLabels, label.new(rightX + 4, htfH, "HTF " + htfTf + "  H: " + str.tostring(htfH, format.mintick) + " (" + str.tostring(distHTFH, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=htfColor, size=size.small, xloc=xloc.bar_index))
            array.push(rangeLabels, label.new(rightX + 4, htfL, "HTF " + htfTf + "  L: " + str.tostring(htfL, format.mintick) + " (" + str.tostring(distHTFL, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=htfColor, size=size.small, xloc=xloc.bar_index))
            
            if high > htfH and htfC < htfO
                array.push(rangeLabels, label.new(rightX, htfH, "🏦 PB", style=label.style_label_down, color=color.new(htfColor, 10), textcolor=color.white, size=size.normal, xloc=xloc.bar_index))
 
        // MTF
        if showMTF and not na(mtfH)
            if showExtLines
                array.push(rangeLines, line.new(bar_index, mtfH, endX, mtfH, color=color.new(mtfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
                array.push(rangeLines, line.new(bar_index, mtfL, endX, mtfL, color=color.new(mtfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
            
            color mBody = mtfC >= mtfO ? color.new(mtfColor, 30) : color.new(mtfColor, 70)
            array.push(rangeLines, line.new(rightX2, mtfL, rightX2, mtfH, color=mtfColor, width=1, xloc=xloc.bar_index))
            array.push(rangeBoxes, box.new(rightX2 - 2, math.max(mtfO, mtfC), rightX2 + 2, math.min(mtfO, mtfC), border_color=mtfColor, bgcolor=mBody, xloc=xloc.bar_index))
            
            array.push(rangeLabels, label.new(rightX2 + 4, mtfH, "MTF " + mtfTf + "  H: " + str.tostring(mtfH, format.mintick) + " (" + str.tostring(distMTFH, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=mtfColor, size=size.small, xloc=xloc.bar_index))
            array.push(rangeLabels, label.new(rightX2 + 4, mtfL, "MTF " + mtfTf + "  L: " + str.tostring(mtfL, format.mintick) + " (" + str.tostring(distMTFL, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=mtfColor, size=size.small, xloc=xloc.bar_index))
            
            if high > mtfH and mtfC < mtfO
                array.push(rangeLabels, label.new(rightX2, mtfH, "🏦 PB", style=label.style_label_down, color=color.new(mtfColor, 10), textcolor=color.white, size=size.normal, xloc=xloc.bar_index))
 
        // LTF
        if showLTF and not na(ltfH)
            if showExtLines
                array.push(rangeLines, line.new(bar_index, ltfH, endX, ltfH, color=color.new(ltfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
                array.push(rangeLines, line.new(bar_index, ltfL, endX, ltfL, color=color.new(ltfColor, 50), style=line.style_dashed, width=1, xloc=xloc.bar_index))
            
            color lBody = ltfC >= ltfO ? color.new(ltfColor, 30) : color.new(ltfColor, 70)
            array.push(rangeLines, line.new(rightX3, ltfL, rightX3, ltfH, color=ltfColor, width=1, xloc=xloc.bar_index))
            array.push(rangeBoxes, box.new(rightX3 - 2, math.max(ltfO, ltfC), rightX3 + 2, math.min(ltfO, ltfC), border_color=ltfColor, bgcolor=lBody, xloc=xloc.bar_index))
            
            array.push(rangeLabels, label.new(rightX3 + 4, ltfH, "LTF " + ltfTf + "  H: " + str.tostring(ltfH, format.mintick) + " (" + str.tostring(distLTFH, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=ltfColor, size=size.small, xloc=xloc.bar_index))
            array.push(rangeLabels, label.new(rightX3 + 4, ltfL, "LTF " + ltfTf + "  L: " + str.tostring(ltfL, format.mintick) + " (" + str.tostring(distLTFL, "#.##") + "%)", style=label.style_label_left, color=color.new(color.white, 100), textcolor=ltfColor, size=size.small, xloc=xloc.bar_index))
            
            if high > ltfH and ltfC < ltfO
                array.push(rangeLabels, label.new(rightX3, ltfH, "🏦 PB", style=label.style_label_down, color=color.new(ltfColor, 10), textcolor=color.white, size=size.normal, xloc=xloc.bar_index))
 
// ═══════════════════════════ 8. Alert Logic ═══════════════════════════
if enableAlerts
    if high > htfH and high[1] <= htfH
        alert("⚠️ Price broke above HTF High!", alert.freq_once_per_bar)
    if low < htfL and low[1] >= htfL
        alert("⚠️ Price broke below HTF Low!", alert.freq_once_per_bar)
    if high > mtfH and high[1] <= mtfH
        alert("⚠️ Price broke above MTF High!", alert.freq_once_per_bar)
    if low < mtfL and low[1] >= mtfL
        alert("⚠️ Price broke below MTF Low!", alert.freq_once_per_bar)
    if high > ltfH and high[1] <= ltfH
        alert("⚠️ Price broke above LTF High!", alert.freq_once_per_bar)
    if low < ltfL and low[1] >= ltfL
        alert("⚠️ Price broke below LTF Low!", alert.freq_once_per_bar)

← 返回列表