ATK/DEF Quant Directional War Command System

ATTDEFS · study · 365 行 · 点赞 15 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATK/DEF Quant Directional War Command System", overlay=true, max_labels_count=500, max_lines_count=500, max_polylines_count=100)
 
// ═══════════════════════════ 1. Module Toggles ═══════════════════════════
grp_toggle = "🔌 Module Toggles"
show_bg    = input.bool(true, "Show Rainbow Background Band", group=grp_toggle)
show_sar   = input.bool(true, "Show SAR Trend Defense Points", group=grp_toggle)
show_panel = input.bool(true, "Show Smart Radar Panel", group=grp_toggle)
show_smc   = input.bool(true, "Show SMC Structure (BOS/CHOCH)", group=grp_toggle)
show_phase = input.bool(true, "Show Divergence Risk Labels", group=grp_toggle)
show_micro = input.bool(true, "Show Micro-Structure Bid/Ask Labels", group=grp_toggle)
 
// ═══════════════════════════ 2. Global Font Size Settings ═══════════════════════════
grp_font = "🔤 Global Font Size Settings"
panel_size_str = input.string("tiny", "Panel Text Size", options=["tiny", "small", "normal", "large"], group=grp_font)
smc_size_str   = input.string("normal", "SMC Text Size", options=["tiny", "small", "normal", "large"], group=grp_font)
phase_size_str = input.string("tiny", "Divergence Label Size", options=["tiny", "small", "normal", "large"], group=grp_font)
micro_size_str = input.string("small", "Micro-Structure Label Size", options=["tiny", "small", "normal", "large"], group=grp_font)
 
get_size(string sz) =>
    sz == "tiny" ? size.tiny : sz == "small" ? size.small : sz == "normal" ? size.normal : size.large
 
string panel_sz = get_size(panel_size_str)
string smc_sz   = get_size(smc_size_str)
string phase_sz = get_size(phase_size_str)
string micro_sz = get_size(micro_size_str)
 
// ═══════════════════════════ 3. Rainbow Spectrum Settings ═══════════════════════════
grp_rainbow = "🌈 Rainbow Spectrum & Background"
poly_len    = input.int(20, "Polynomial Regression Period", minval=5, maxval=100, group=grp_rainbow)
bg_length   = input.int(80, "Background Band Lookback Length", minval=20, maxval=200, group=grp_rainbow)
 
// ═══════════════════════════ 4. SAR Trend Settings ═══════════════════════════
grp_sar = "🎯 SAR Trend Tracking Settings"
sar_start   = input.float(0.02, "SAR Step", step=0.01, group=grp_sar)
sar_max     = input.float(0.2, "SAR Max Step", step=0.05, group=grp_sar)
 
// ═══════════════════════════ 5. SMC Structure Settings ═══════════════════════════
grp_smc = "📐 SMC Structure & Divergence Settings"
pivot_len   = input.int(5, "SMC Pivot Length", minval=2, maxval=20, group=grp_smc)
lookback_div= input.int(20, "Divergence Lookback Period", minval=5, maxval=50, group=grp_smc)
 
// ═══════════════════════════ 6. Micro-Structure Settings ═══════════════════════════
grp_micro = "⚖️ Micro-Structure (RSI + ROC) Settings"
rsi_len     = input.int(14, "RSI Period", group=grp_micro)
ob_level    = input.int(75, "RSI Overbought Threshold", group=grp_micro)
os_level    = input.int(25, "RSI Oversold Threshold", group=grp_micro)
roc_len     = input.int(9, "ROC Momentum Period", group=grp_micro)
 
// ═══════════════════════════ 7. Squeeze Calculation Settings ═══════════════════════════
grp_bb = "📦 Squeeze Calculation Settings"
bb_len      = input.int(20, "Bollinger Bands Period", group=grp_bb)
bb_mult     = input.float(2.0, "Bollinger Bands Multiplier", step=0.1, group=grp_bb)
 
// ═══════════════════════════ 8. Core Algorithm ═══════════════════════════
float poly_ma = 0.0
float weight_sum = 0.0
for i = 0 to poly_len - 1
    float weight = math.pow((poly_len - i) / poly_len, 2.0) 
    poly_ma += close[i] * weight
    weight_sum += weight
poly_ma := poly_ma / weight_sum
 
float atr_val = ta.atr(14)
float atr_pct = (atr_val / close) * 100 
float raw_slope = (poly_ma - poly_ma[1]) / (atr_val + 0.00001)
float smooth_slope = ta.ema(raw_slope, 3)
float rsi_val = ta.rsi(close, rsi_len)
float roc_val = ta.roc(close, roc_len)
 
float up_vol = math.max(high - open, high - close[1]) 
float down_vol = math.max(open - low, close[1] - low)
float asym_ratio = (up_vol - down_vol) / (up_vol + down_vol + 0.00001) * 100
bool is_extreme_asym = math.abs(asym_ratio) > 60
 
float bb_basis = ta.sma(close, bb_len)
float bb_dev = bb_mult * ta.stdev(close, bb_len)
float bb_width = (bb_dev * 2) / bb_basis
float bb_width_min = ta.lowest(bb_width, 50)
bool is_squeeze = bb_width <= bb_width_min * 1.1 
 
// ═══════════════════════════ 9. 🟢 Precise Divergence & Risk Calculation ═══════════════════════════
float ph = ta.pivothigh(high, pivot_len, pivot_len)
float pl = ta.pivotlow(low, pivot_len, pivot_len)
int pivot_bar_index = bar_index - pivot_len
float hist_slope = smooth_slope[pivot_len]
float lowest_slope = ta.lowest(smooth_slope, lookback_div + pivot_len)[pivot_len]
float highest_slope = ta.highest(smooth_slope, lookback_div + pivot_len)[pivot_len]
float lowest_price = ta.lowest(low, lookback_div + pivot_len)[pivot_len]
float highest_price = ta.highest(high, lookback_div + pivot_len)[pivot_len]
 
bool bull_div = not na(pl) and (pl <= lowest_price) and (hist_slope > lowest_slope)
bool bear_div = not na(ph) and (ph >= highest_price) and (hist_slope < highest_slope)
 
float bull_risk_pct = math.min(99.0, math.abs(hist_slope - lowest_slope) * 100)
float bear_risk_pct = math.min(99.0, math.abs(hist_slope - highest_slope) * 100)
 
// ═══════════════════════════ 10. 🟢 SAR & Color Engine ═══════════════════════════
float sar_val = ta.sar(sar_start, sar_start, sar_max)
var bool is_bull_trend = true
if bull_div
    is_bull_trend := true
else if bear_div
    is_bull_trend := false
else
    is_bull_trend := close > sar_val
 
color core_color = is_bull_trend ? color.new(#00e5ff, 0) : color.new(#ff0055, 0)
if bull_div
    core_color := color.new(#00e676, 0) 
else if bear_div
    core_color := color.new(#ff0055, 0) 
 
color sar_color = show_sar ? core_color : color.new(na, 100)
plot(sar_val, "SAR Dot", color=sar_color, style=plot.style_circles, linewidth=2)
 
// ═══════════════════════════ 11. 🟢 SMC Structure Detection ═══════════════════════════
var float last_ph = na
var float last_pl = na
var bool is_uptrend = true 
 
if not na(ph)
    last_ph := ph
if not na(pl)
    last_pl := pl
 
bool bos_bull = false
bool choch_bull = false
bool bos_bear = false
bool choch_bear = false
 
if close > last_ph and close[1] <= last_ph
    if is_uptrend
        bos_bull := true 
    else
        choch_bull := true 
        is_uptrend := true
 
if close < last_pl and close[1] >= last_pl
    if not is_uptrend
        bos_bear := true 
    else
        choch_bear := true 
        is_uptrend := false
 
color long_color = color.new(#00e676, 0)
color short_color = color.new(#ff0055, 0)
 
if show_smc
    if bos_bull
        label.new(bar_index, high, "BOS", xloc=xloc.bar_index, style=label.style_none, textcolor=long_color, size=smc_sz)
    if choch_bull
        label.new(bar_index, high, "CHOCH", xloc=xloc.bar_index, style=label.style_none, textcolor=long_color, size=smc_sz)
    if bos_bear
        label.new(bar_index, low, "BOS", xloc=xloc.bar_index, style=label.style_none, textcolor=short_color, size=smc_sz)
    if choch_bear
        label.new(bar_index, low, "CHOCH", xloc=xloc.bar_index, style=label.style_none, textcolor=short_color, size=smc_sz)
 
// ═══════════════════════════ 12. 🟢 Bid/Ask (RSI+ROC) Labels ═══════════════════════════
float roc_factor = math.max(0, math.abs(roc_val) * 2) 
float final_bid = rsi_val + (roc_val > 0 ? roc_factor : 0)
float final_ask = (100 - rsi_val) + (roc_val < 0 ? roc_factor : 0)
 
float total_power = final_bid + final_ask
float bid_pct = (final_bid / total_power) * 100
float ask_pct = (final_ask / total_power) * 100
float bid_ask_diff = bid_pct - ask_pct
 
if show_micro and barstate.islast
    var label ba_label = na
    label.delete(ba_label)
    
    string ba_text = ""
    color ba_color = na
    string ba_style = ""
    float ba_y = close
    
    if is_extreme_asym and asym_ratio < -60
        ba_text := "⚠️ ASYM DOWN"
        ba_color := color.new(#b71c1c, 10)
        ba_style := label.style_label_up
        ba_y := low
    else if is_extreme_asym and asym_ratio > 60
        ba_text := "🚀 ASYM UP"
        ba_color := color.new(#00c853, 10)
        ba_style := label.style_label_down
        ba_y := high
    else if rsi_val > ob_level and bid_pct > 60
        ba_text := "🔴 EXTREME BUY"
        ba_color := color.new(#ff0055, 20)
        ba_style := label.style_label_down
        ba_y := high
    else if rsi_val < os_level and ask_pct > 60
        ba_text := "🟢 EXTREME SELL"
        ba_color := color.new(#00e676, 20)
        ba_style := label.style_label_up
        ba_y := low
    else if math.abs(bid_ask_diff) > 30
        ba_text := bid_ask_diff > 0 ? "🟢 BID HEAVY" : "🔴 ASK HEAVY"
        ba_color := bid_ask_diff > 0 ? color.new(#00e676, 20) : color.new(#ff0055, 20)
        ba_style := bid_ask_diff > 0 ? label.style_label_down : label.style_label_up
        ba_y := bid_ask_diff > 0 ? high : low
    else
        ba_text := "⚖️ BALANCED"
        ba_color := color.new(#8892b0, 20)
        ba_style := label.style_label_left
        ba_y := close
        
    ba_label := label.new(bar_index, ba_y, ba_text, xloc=xloc.bar_index, style=ba_style, color=ba_color, textcolor=color.white, size=micro_sz)
 
// ═══════════════════════════ 13. 🟢 Rainbow Glowing Background Band ═══════════════════════════
if show_bg and barstate.islast
    var array<polyline> bg_bands = array.new<polyline>()
    if array.size(bg_bands) > 0
        for i = 0 to array.size(bg_bands) - 1
            polyline.delete(array.get(bg_bands, i))
        array.clear(bg_bands)
 
    int bg_layers = 5 
    int bg_offset = 12 
 
    for i = 0 to bg_layers - 1
        array<chart.point> pts_top = array.new<chart.point>()
        array<chart.point> pts_bot = array.new<chart.point>()
        float band_width = (i + 1) * (atr_val / bg_offset)
        
        for j = 0 to bg_length
            int px = bar_index - j
            float py = na(poly_ma[j]) ? poly_ma : poly_ma[j]
            array.push(pts_top, chart.point.from_index(px, py + band_width))
            array.push(pts_bot, chart.point.from_index(px, py - band_width))
        
        array<chart.point> pts_fill = array.copy(pts_top)
        array<chart.point> r_bot = array.copy(pts_bot)
        array.reverse(r_bot)
        array.concat(pts_fill, r_bot)
        
        color band_color = color.from_gradient(i, 0, bg_layers, color.new(core_color, 85), color.new(core_color, 100))
        polyline bg_fill = polyline.new(pts_fill, curved=true, xloc=xloc.bar_index, line_color=color.new(core_color, 100), fill_color=band_color)
        array.push(bg_bands, bg_fill)
 
// ═══════════════════════════ 14. 🟢 Divergence Risk Labels (UDT fixed to avoid name conflict) ═══════════════════════════
type DivRiskRecord
    int bar_idx
    label lbl
var array<DivRiskRecord> phase_history = array.new<DivRiskRecord>()
 
if show_phase and barstate.isconfirmed
    if bull_div
        string risk_txt = "🎯 Divergence Risk\n" + str.tostring(bull_risk_pct, "#") + "%"
        label lbl_start = label.new(pivot_bar_index, pl, risk_txt, xloc=xloc.bar_index, style=label.style_label_up, color=color.new(#00e676, 20), textcolor=color.white, size=phase_sz)
        array.push(phase_history, DivRiskRecord.new(pivot_bar_index, lbl_start))
    else if bear_div
        string risk_txt = "🛑 Divergence Risk\n" + str.tostring(bear_risk_pct, "#") + "%"
        label lbl_end = label.new(pivot_bar_index, ph, risk_txt, xloc=xloc.bar_index, style=label.style_label_down, color=color.new(#ff0055, 20), textcolor=color.white, size=phase_sz)
        array.push(phase_history, DivRiskRecord.new(pivot_bar_index, lbl_end))
    
    if array.size(phase_history) > 10
        DivRiskRecord oldest = array.shift(phase_history)
        label.delete(oldest.lbl)
 
// ═══════════════════════════ 15. Core Baseline ═══════════════════════════
plot(poly_ma, color=color.new(#ffffff, 80), linewidth=1, title="Poly MA Base Line")
 
// ═══════════════════════════ 16. 🟢 Smart Momentum Diagnostics Engine ═══════════════════════════
bool is_strong = math.abs(smooth_slope) > 0.8
bool is_weak = math.abs(smooth_slope) <= 0.3
bool is_building = math.abs(smooth_slope) > math.abs(smooth_slope[1]) and math.abs(smooth_slope) > math.abs(smooth_slope[2])
bool is_fading = math.abs(smooth_slope) < math.abs(smooth_slope[1]) and math.abs(smooth_slope[1]) < math.abs(smooth_slope[2])
bool is_exhaustion = (math.abs(smooth_slope) > 1.2) and ((close < open and high - close > close - low) or (close > open and close - low > high - close))
bool is_recovery = (math.abs(smooth_slope) > math.abs(smooth_slope[1]) * 1.5) and (math.abs(smooth_slope[1]) < 0.2)
 
string diag_str = "NEUTRAL"
color diag_col = color.new(#d1d4dc, 0)
if is_extreme_asym
    diag_str := asym_ratio > 0 ? "🚀 ASYM UP" : "⚠️ ASYM DOWN"
    diag_col := asym_ratio > 0 ? color.new(#00c853, 0) : color.new(#b71c1c, 0)
else if is_squeeze
    diag_str := "🔴 SQUEEZE"
    diag_col := color.new(#ffeb3b, 0)
else if bull_div
    diag_str := "🎯 BULL DIVERGENCE"
    diag_col := color.new(#00e676, 0)
else if bear_div
    diag_str := "🎯 BEAR DIVERGENCE"
    diag_col := color.new(#ff0055, 0)
else if is_exhaustion
    diag_str := is_bull_trend ? "🟠 BULL EXHAUSTION" : "🟠 BEAR EXHAUSTION"
    diag_col := color.new(#ff9800, 0)
else if is_recovery
    diag_str := is_bull_trend ? "🌱 BULL RECOVERY" : "🌱 BEAR RECOVERY"
    diag_col := color.new(#00bcd4, 0)
else if is_building
    diag_str := is_bull_trend ? "🟢 BULL BUILDING" : "🔴 BEAR BUILDING"
    diag_col := is_bull_trend ? color.new(#00e5ff, 0) : color.new(#ff0055, 0)
else if is_fading
    diag_str := is_bull_trend ? "🟡 BULL FADING" : "🟡 BEAR FADING"
    diag_col := color.new(#fbc02d, 0)
else if is_strong
    diag_str := is_bull_trend ? "💪 STRONG BULL" : "💪 STRONG BEAR"
    diag_col := is_bull_trend ? color.new(#00e5ff, 0) : color.new(#ff0055, 0)
else if is_weak
    diag_str := "😴 WEAK MOMENTUM"
    diag_col := color.new(#8892b0, 0)
 
// ═══════════════════════════ 17. Smart Radar Panel ═══════════════════════════
if show_panel
    var table tb = table.new(position.top_right, 2, 9, bgcolor=color.new(#131722, 20), border_width=0, border_color=color.new(#2a2e39, 0))
    if barstate.islast
        table.cell(tb, 0, 0, "SMART RADAR", text_color=color.white, text_size=panel_sz)
        table.merge_cells(tb, 0, 0, 1, 0)
        
        table.cell(tb, 0, 1, "Trend State", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        string trend_str = is_bull_trend ? "🟢 BULL TREND" : "🔴 BEAR TREND"
        color trend_col = is_bull_trend ? color.new(#00e5ff, 0) : color.new(#ff0055, 0)
        table.cell(tb, 1, 1, trend_str, text_color=trend_col, text_size=panel_sz)
 
        table.cell(tb, 0, 2, "Diagnostics", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        table.cell(tb, 1, 2, diag_str, text_color=diag_col, text_size=panel_sz)
        
        table.cell(tb, 0, 3, "SAR Value", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        table.cell(tb, 1, 3, str.tostring(sar_val, format.mintick), text_color=core_color, text_size=panel_sz)
 
        table.cell(tb, 0, 4, "RSI Value", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        color rsi_col = rsi_val > ob_level ? color.new(#ff0055, 0) : (rsi_val < os_level ? color.new(#00e676, 0) : color.new(#d1d4dc, 0))
        table.cell(tb, 1, 4, str.tostring(rsi_val, "#.##"), text_color=rsi_col, text_size=panel_sz)
 
        table.cell(tb, 0, 5, "Bid | Ask", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        string ba_str = str.tostring(bid_pct, "#") + "% | " + str.tostring(ask_pct, "#") + "%"
        table.cell(tb, 1, 5, ba_str, text_color=diag_col, text_size=panel_sz)
 
        table.cell(tb, 0, 6, "Asym Volatility", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        string asym_str = str.tostring(asym_ratio, "#.#") + "%"
        color asym_col = asym_ratio > 30 ? color.new(#00c853, 0) : (asym_ratio < -30 ? color.new(#b71c1c, 0) : color.new(#8892b0, 0))
        table.cell(tb, 1, 6, asym_str, text_color=asym_col, text_size=panel_sz)
 
        table.cell(tb, 0, 7, "ATR LEVEL", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        string atr_level_str = atr_pct > 2.0 ? "🔥 EXTREME" : (atr_pct > 1.0 ? "⚠️ HIGH" : "❄️ LOW")
        color atr_level_col = atr_pct > 2.0 ? color.new(#ff0055, 0) : (atr_pct > 1.0 ? color.new(#fbc02d, 0) : color.new(#00e5ff, 0))
        string atr_display = str.tostring(atr_pct, "#.##") + "% " + atr_level_str
        table.cell(tb, 1, 7, atr_display, text_color=atr_level_col, text_size=panel_sz)
 
        table.cell(tb, 0, 8, "Action", text_color=color.new(#d1d4dc, 0), text_size=panel_sz)
        string action_str = ""
        if is_extreme_asym and asym_ratio < -60
            action_str := "⚠️ PANIC SELL"
        else if is_extreme_asym and asym_ratio > 60
            action_str := "🚀 SHORT SQUEEZE"
        else if rsi_val > ob_level and bid_pct > 60
            action_str := "⚠️ TAKE PROFIT"
        else if rsi_val < os_level and ask_pct > 60
            action_str := "🔥 BUY DIP"
        else
            action_str := is_squeeze ? "⏳ WAIT BREAKOUT" : (is_bull_trend ? (bull_div ? "🔥 LONG HEAVY" : "🟢 HOLD LONG") : (bear_div ? "🔥 SHORT HEAVY" : "🔴 HOLD SHORT"))
        table.cell(tb, 1, 8, action_str, text_color=trend_col, text_size=panel_sz)
 
// ═══════════════════════════ 18. Real-Time Alerts ═══════════════════════════
if bull_div
    alert("【LONG SIGNAL】🎯 Divergence Risk! Bullish Divergence.", alert.freq_once_per_bar_close)
if bear_div
    alert("【SHORT SIGNAL】🛑 Divergence Risk! Bearish Divergence.", alert.freq_once_per_bar_close)
if is_squeeze
    alert("【SQUEEZE ALERT】Market is in extreme squeeze.", alert.freq_once_per_bar_close)
if is_extreme_asym
    alert("【ASYM VOLATILITY】Extreme asymmetric volatility detected! Ratio: " + str.tostring(asym_ratio, "#.#") + "%", alert.freq_once_per_bar_close)

← 返回列表