ATK/DEF LTF Liquidity Action With Breaks

ATTDEFS · study · 363 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("ATK/DEF LTF Liquidity Action With Breaks", shorttitle="LTF Liquidity Action With Breaks ", overlay=true, max_labels_count=500, max_boxes_count=500)
 
// ═══════════════════════════ 1. UDT Definitions ═══════════════════════════
type MTFData
    float  ratio       
    float  atk         
    float  def         
    bool   is_bull     
    string status      
    color  col         
    string arrow       
    color  arrow_col   
 
type SignalState
    int  last_signal  
    int  last_bar     
 
type LTFState
    int    age
    float  j_line
    bool   is_golden_cross
    bool   is_dead_cross
    string trend_status
 
// ═══════════════════════════ 2. Input Parameters ═══════════════════════════
grp_calc = "Gravity Core Algorithm"
gravityLen = input.int(21, "Gravity Period", minval=1, group=grp_calc)
baseMult = input.float(1.0, "Base Band Multiplier", step=0.1, group=grp_calc)
rangeMult = input.float(0.5, "Range Width/Depth Multiplier", step=0.1, group=grp_calc)
 
grp_mtf = "MTF Period Settings"
tfD = input.timeframe("D", "Daily Zone", group=grp_mtf)
tf4H = input.timeframe("240", "4H Zone", group=grp_mtf)
tf1H = input.timeframe("60", "1H Zone", group=grp_mtf)
lookback = input.int(50, "Radar Scan Period", group=grp_mtf)
smoothLen = input.int(5, "Signal Smooth Period", group=grp_mtf)
 
grp_range = "LTF Range Sniper Settings"
rangeLen = input.int(20, "Range Lookback Length", minval=5, group=grp_range)
sensitivity = input.float(0.15, "Sniper Trigger Sensitivity (%)", step=0.05, group=grp_range)
 
grp_ltf = "LTF Age & KDJ Settings"
ltf1 = input.timeframe("15", "LTF 1 (e.g., 15m)", group=grp_ltf)
ltf2 = input.timeframe("60", "LTF 2 (e.g., 1H)", group=grp_ltf)
ltf3 = input.timeframe("240", "LTF 3 (e.g., 4H)", group=grp_ltf)
kdjLen = input.int(9, "KDJ Period", group=grp_ltf)
 
grp_smc = "SMC & Liquidity Filters"
useSqueeze = input.bool(true, "Use Squeeze Filter", group=grp_smc)
useIFVG = input.bool(true, "Use IFVG Filter", group=grp_smc)
 
grp_visual = "Visual Elements (On/Off)"
showBands = input.bool(true, "Show Dynamic Bands", group=grp_visual)
showFill = input.bool(false, "Show Warzone Fill", group=grp_visual)
showSMC = input.bool(true, "Show IFVG & Squeeze Markers", group=grp_visual)
showZones = input.bool(true, "Show Active Liquidity Pool (MTF)", group=grp_visual)
 
grp_panels = "Dashboard Panels (On/Off)"
show_range_info = input.bool(true, "Show Flow Radar (Bottom Left)", group=grp_panels)
 
// ═══════════════════════════ 3. Helper Functions ═══════════════════════════
f_get_status_str(float r, bool up) =>
    string s = "【Neutral】"
    if r > 60 and up
        s := "【Suppression】"
    else if r > 55
        s := "【Advantage】"
    else if r < 40 and not up
        s := "【Defense Collapse】"
    else if r < 45
        s := "【Enemy Advance】"
    s
 
f_get_color(float r) => r > 50 ? color.new(color.teal, 0) : color.new(color.red, 0)
 
f_calc_mtf(string tf) =>
    [o, h, l, c, v] = request.security(syminfo.tickerid, tf, [open, high, low, close, volume], lookahead=barmerge.lookahead_off)
    body = math.abs(c - o)
    upper_wick = h - math.max(c, o)
    lower_wick = math.min(c, o) - l
    
    safe_v = na(v) or v == 0 ? 1.0 : v 
    bull_vol = (c > o) ? safe_v : (c < o ? safe_v * 0.5 : safe_v * 0.8)
    bear_vol = (c < o) ? safe_v : (c > o ? safe_v * 0.5 : safe_v * 0.8)
    
    atk_raw = (body * 0.6 + lower_wick * 0.4) * bull_vol
    def_raw = (body * 0.6 + upper_wick * 0.4) * bear_vol
    
    atk_smooth = ta.sma(atk_raw, lookback)
    def_smooth = ta.sma(def_raw, lookback)
    
    total = atk_smooth + def_smooth
    ratio = total == 0 ? 50.0 : (atk_smooth / total) * 100
    
    ma = ta.sma(c, smoothLen)
    is_bull = c > ma
    
    bool is_ratio_up = ratio > nz(ratio[1])
    string arrow = is_ratio_up ? "▲" : "▼" 
    color arrow_col = is_ratio_up ? color.new(color.teal, 0) : color.new(color.red, 0)
    
    MTFData.new(ratio, atk_smooth, def_smooth, is_bull, f_get_status_str(ratio, is_bull), f_get_color(ratio), arrow, arrow_col)
 
f_calc_ltf(string tf) =>
    [h, l, c] = request.security(syminfo.tickerid, tf, [high, low, close], lookahead=barmerge.lookahead_off)
    
    rHigh = ta.highest(h, rangeLen)
    rLow  = ta.lowest(l, rangeLen)
    is_consolidating = c <= rHigh and c >= rLow
    age = ta.barssince(not is_consolidating)
    
    rsv = ta.stoch(c, h, l, kdjLen)
    k = ta.sma(rsv, 3)
    d = ta.sma(k, 3)
    j = 3 * k - 2 * d
    
    is_golden_cross = ta.crossover(k, d) and j < 30
    is_dead_cross = ta.crossunder(k, d) and j > 70
    
    string trend_status = "⚖️ Neutral"
    if is_golden_cross
        trend_status := "🚀 Start"
    else if is_dead_cross
        trend_status := "🏁 End"
    else if j > 80
        trend_status := "🔥 Overbought"
    else if j < 20
        trend_status := "🧊 Oversold"
        
    LTFState.new(age, j, is_golden_cross, is_dead_cross, trend_status)
 
f_fill_ltf_row(table tb, int row, string tf_name, LTFState data) =>
    table.cell(tb, 0, row, tf_name, text_color=color.white, text_size=size.small)
    ageColor = data.age > 20 ? color.yellow : color.white
    table.cell(tb, 1, row, str.tostring(data.age) + " bars", text_color=ageColor, text_size=size.small)
    jColor = data.j_line > 80 ? color.red : data.j_line < 20 ? color.teal : color.gray
    table.cell(tb, 2, row, str.tostring(data.j_line, "#.#"), text_color=jColor, text_size=size.small)
    tColor = data.is_golden_cross ? color.teal : data.is_dead_cross ? color.red : color.white
    table.cell(tb, 3, row, data.trend_status, text_color=tColor, text_size=size.small)
 
// ═══════════════════════════ 4. Data Logic ═══════════════════════════
d_data = f_calc_mtf(tfD)
h4_data = f_calc_mtf(tf4H)
h1_data = f_calc_mtf(tf1H)
 
ltf1_data = f_calc_ltf(ltf1)
ltf2_data = f_calc_ltf(ltf2)
ltf3_data = f_calc_ltf(ltf3)
 
var SignalState sig_state = SignalState.new(0, 0)
 
typical_price = (high + low + close) / 3
base_gravity = ta.vwma(typical_price, gravityLen)
atr_val = ta.atr(gravityLen)
 
rangeHigh = ta.highest(high, rangeLen)
rangeLow  = ta.lowest(low, rangeLen)
rangeWidth = rangeHigh - rangeLow
 
bool mtf_bull_crush = d_data.ratio > 50 and h4_data.ratio > 50 and h1_data.ratio > 50
bool mtf_bear_crush = d_data.ratio < 50 and h4_data.ratio < 50 and h1_data.ratio < 50
 
float topStretch = 1.0
float botStretch = 1.0
if mtf_bull_crush
    topStretch := 0.8
    botStretch := 1.5
else if mtf_bear_crush
    topStretch := 1.5
    botStretch := 0.8
 
bandWidthBase = math.max(atr_val * baseMult, rangeWidth * rangeMult)
upper_gravity = base_gravity + (bandWidthBase * topStretch)
lower_gravity = base_gravity - (bandWidthBase * botStretch)
 
p1 = plot(showBands ? upper_gravity : na, title="Dynamic Upper Band", color=color.new(color.red, 50), style=plot.style_circles)
p2 = plot(showBands ? lower_gravity : na, title="Dynamic Lower Band", color=color.new(color.teal, 50), style=plot.style_circles)
fill(p1, p2, color=showFill ? color.new(color.gray, 90) : na, title="Gravity Warzone")
 
// ═══════════════════════════ 5. SMC Filter: IFVG & Squeeze Engine ═══════════════════════════
bb_basis = ta.sma(close, 20)
bb_dev = 2.0 * ta.stdev(close, 20)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
 
kc_basis = ta.ema(close, 20)
kc_range = ta.atr(20)
kc_upper = kc_basis + (kc_range * 1.5)
kc_lower = kc_basis - (kc_range * 1.5)
 
is_squeeze = (bb_upper < kc_upper) and (bb_lower > kc_lower)
fvg_bull = low > high[2]
fvg_bear = high < low[2]
ifvg_bull_confirm = fvg_bull and close > high[1]
ifvg_bear_confirm = fvg_bear and close < low[1]
 
sweep_top = high > upper_gravity and close < upper_gravity
sweep_bot = low < lower_gravity and close > lower_gravity
 
strong_bull = mtf_bull_crush or (ifvg_bull_confirm and not is_squeeze)
strong_bear = mtf_bear_crush or (ifvg_bear_confirm and not is_squeeze)
 
plotshape(showSMC and is_squeeze, title="Squeeze", style=shape.circle, location=location.abovebar, color=color.new(color.yellow, 50), size=size.tiny)
plotshape(showSMC and ifvg_bull_confirm, title="IFVG Bull", style=shape.diamond, location=location.belowbar, color=color.new(color.teal, 20), size=size.tiny)
plotshape(showSMC and ifvg_bear_confirm, title="IFVG Bear", style=shape.diamond, location=location.abovebar, color=color.new(color.red, 20), size=size.tiny)
 
// ═══════════════════════════ 5.1 活跃流动性池 (Active Liquidity Pool) ═══════════════════════════
[ph, pl] = request.security(syminfo.tickerid, tf1H, [ta.pivothigh(high, 5, 5), ta.pivotlow(low, 5, 5)], lookahead=barmerge.lookahead_off)
var float active_high = na
var float active_low = na
var int active_start_bar = na
 
if not na(ph)
    active_high := ph
    active_start_bar := bar_index
if not na(pl)
    active_low := pl
    active_start_bar := bar_index
 
var box active_pool = na
if showZones and not na(active_high) and not na(active_low) and not na(active_start_bar)
    if na(active_pool)
        active_pool := box.new(active_start_bar, active_high, bar_index, active_low, border_color=color.new(color.blue, 30), border_style=line.style_dashed, bgcolor=color.new(color.blue, 90), text="Active Pool (1H)", text_color=color.blue, text_size=size.tiny, text_halign=text.align_right)
    else
        box.set_top(active_pool, active_high)
        box.set_bottom(active_pool, active_low)
        box.set_right(active_pool, bar_index)
 
if showZones and not na(active_pool)
    if close > active_high or close < active_low
        box.set_bgcolor(active_pool, color.new(color.gray, 95))
        box.set_border_color(active_pool, color.new(color.gray, 80))
        box.set_text(active_pool, "Pool Cleared")
        active_pool := na 
 
// ═══════════════════════════ 5.2 波动状态引擎 (Volatility Regime) ═══════════════════════════
adxLen = 14
[diplus, diminus, adx] = ta.dmi(adxLen, adxLen)
 
string vol_type = "- (Range)"
color vol_color = color.gray
 
if adx > 25
    if mtf_bull_crush or close > base_gravity
        vol_type := "↑ (Trend Up)"
        vol_color := color.teal
    else if mtf_bear_crush or close < base_gravity
        vol_type := "↓ (Trend Down)"
        vol_color := color.red
 
// ═══════════════════════════ 5.3 CDSR 风险引擎 (Crowding + Double-sided + Systemic) ═══════════════════════════
// 修复:先用 var 声明变量,再用 := 进行累加
var int up_streak = 0
var int down_streak = 0
up_streak := close > open ? up_streak[1] + 1 : 0
down_streak := close < open ? down_streak[1] + 1 : 0
 
vol_z = (volume - ta.sma(volume, 20)) / math.max(ta.stdev(volume, 20), syminfo.mintick)
max_streak = math.max(up_streak, down_streak)
crowding_risk = math.min(100, (math.max(0, vol_z) * 20) + (max_streak * 10))
 
body = math.abs(close - open)
upper_wick = high - math.max(close, open)
lower_wick = math.min(close, open) - low
total_range = high - low
double_sided_ratio = (upper_wick + lower_wick) / math.max(total_range, syminfo.mintick)
double_sided_risk = math.min(100, double_sided_ratio * 120)
 
rsi_val = ta.rsi(close, 14)
dist_to_grav = math.abs(close - base_gravity) / math.max(atr_val, syminfo.mintick)
systemic_risk = math.min(100, (dist_to_grav * 15) + (math.abs(rsi_val - 50) * 1.5))
 
cdsr = (crowding_risk + double_sided_risk + systemic_risk) / 3
 
string cdsr_status = "🟢 LOW RISK"
color cdsr_color = color.green
if cdsr > 75
    cdsr_status := "🚨 CRITICAL"
    cdsr_color := color.red
else if cdsr > 50
    cdsr_status := "🟡 ELEVATED"
    cdsr_color := color.orange
 
// ═══════════════════════════ 6. Tactical Fire Logic (Silent) ═══════════════════════════
bull_rejection = close > open and (math.min(close, open) - low) > math.abs(close - open) * 1.5
bear_rejection = close < open and (high - math.max(close, open)) > math.abs(close - open) * 1.5
 
buy_cond = (low <= lower_gravity or low < base_gravity) and bull_rejection and close > low
raw_buy = ta.crossover(close, base_gravity) or (buy_cond and close > open)
 
sell_cond = (high >= upper_gravity or high > base_gravity) and bear_rejection and close < high
raw_sell = ta.crossunder(close, base_gravity) or (sell_cond and close < open)
 
bool final_buy = false
bool final_sell = false
 
if raw_buy and (sig_state.last_signal != 1 or (bar_index - sig_state.last_bar) > 5)
    final_buy := true
    sig_state.last_signal := 1
    sig_state.last_bar := bar_index
    
if raw_sell and (sig_state.last_signal != -1 or (bar_index - sig_state.last_bar) > 5)
    final_sell := true
    sig_state.last_signal := -1
    sig_state.last_bar := bar_index
 
// ═══════════════════════════ 7. LTF Range Sniper Logic (Silent) ═══════════════════════════
pricePosition = rangeWidth == 0 ? 50 : ((close - rangeLow) / rangeWidth) * 100
triggerUpLevel = 100 - (sensitivity * 100) 
triggerDownLevel = sensitivity * 100       
 
base_upFire = high >= (rangeLow + rangeWidth * (triggerUpLevel / 100)) and close < open and pricePosition > triggerUpLevel
base_downFire = low <= (rangeLow + rangeWidth * (triggerDownLevel / 100)) and close > open and pricePosition < triggerDownLevel
 
validUpFire = base_upFire and not mtf_bull_crush
validDownFire = base_downFire and not mtf_bear_crush
 
// ═══════════════════════════ 8. LTF Range Radar Panel (Bottom Left) ═══════════════════════════
var table rangePanel = table.new(position.bottom_left, 4, 9, bgcolor=color.new(color.black, 80), frame_color=color.new(color.gray, 30), frame_width=1, border_width=1)
 
if show_range_info and barstate.islast
    table.cell(rangePanel, 0, 0, "LIQUIDITY FLOW & LTF MATRIX", text_color=color.gray, text_size=size.small)
    table.merge_cells(rangePanel, 0, 0, 3, 0)
    
    posColor = pricePosition > triggerUpLevel ? color.red : pricePosition < triggerDownLevel ? color.teal : color.gray
    table.cell(rangePanel, 0, 1, "Pos %:", text_color=color.white, text_size=size.small)
    table.cell(rangePanel, 1, 1, str.tostring(pricePosition, "#.#") + "%", text_color=posColor, text_size=size.small)
    
    string poolStatus = na(active_high) or na(active_low) ? "Scanning..." : (close > active_high or close < active_low) ? "🔴 CLEARED" : "🔵 ACTIVE POOL"
    table.cell(rangePanel, 2, 1, "Liquidity:", text_color=color.white, text_size=size.small)
    table.cell(rangePanel, 3, 1, poolStatus, text_color=poolStatus == "🔵 ACTIVE POOL" ? color.blue : color.gray, text_size=size.small)
    
    table.cell(rangePanel, 0, 2, "Volatility:", text_color=color.white, text_size=size.small)
    table.cell(rangePanel, 1, 2, vol_type, text_color=vol_color, text_size=size.small)
    
    string dirStatus = strong_bull ? "🚀 STRONG BULL" : strong_bear ? "🔻 STRONG BEAR" : "⚖️ RANGING"
    table.cell(rangePanel, 2, 2, "MTF Flow:", text_color=color.white, text_size=size.small)
    table.cell(rangePanel, 3, 2, dirStatus, text_color=strong_bull ? color.teal : strong_bear ? color.red : color.gray, text_size=size.small)
 
    table.cell(rangePanel, 0, 3, "Risk (CDSR):", text_color=color.white, text_size=size.small)
    table.cell(rangePanel, 1, 3, str.tostring(cdsr, "#.#") + " " + cdsr_status, text_color=cdsr_color, text_size=size.small)
    
    table.cell(rangePanel, 0, 4, "", bgcolor=color.gray, height=1)
    table.merge_cells(rangePanel, 0, 4, 3, 4)
 
    table.cell(rangePanel, 0, 5, "LTF Timeframe", text_color=color.gray, text_size=size.small)
    table.cell(rangePanel, 1, 5, "Range Age", text_color=color.gray, text_size=size.small)
    table.cell(rangePanel, 2, 5, "KDJ (J Line)", text_color=color.gray, text_size=size.small)
    table.cell(rangePanel, 3, 5, "Trend Phase", text_color=color.gray, text_size=size.small)
 
    f_fill_ltf_row(rangePanel, 6, ltf1, ltf1_data)
    f_fill_ltf_row(rangePanel, 7, ltf2, ltf2_data)
    f_fill_ltf_row(rangePanel, 8, ltf3, ltf3_data)
 
// ═══════════════════════════ 9. Tactical Alerts ═══════════════════════════
alertcondition(final_buy, title="ATK - Long Attack Buy Signal", message="【🚨 KINGDOM ALERT】Long attack initiated! ATK buy signal triggered on Gravity Band.")
alertcondition(final_sell, title="DEF - Short Bombing Sell Signal", message="【🚨 KINGDOM ALERT】Short bombing initiated! DEF sell signal triggered on Gravity Band.")
 
if final_buy
    alert("ATK FIRE! Current Air Superiority: " + str.tostring(d_data.ratio, "#.#") + "%", alert.freq_once_per_bar_close)
if final_sell
    alert("DEF BOMBING! Current Air Superiority: " + str.tostring(d_data.ratio, "#.#") + "%", alert.freq_once_per_bar_close)

← 返回列表