本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ATK/DEF Quant War Castle: Kingdom Command System", shorttitle="ATK/DEF Castle Command", overlay=true, max_labels_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 RegimeData
int code
string state_name
string description
string action
color action_col
color box_color
color text_color
// ═══════════════════════════ 2. Input Parameters ═══════════════════════════
grp_calc = "Gravity Core Algorithm"
gravityLen = input.int(21, "Gravity Period", minval=1, group=grp_calc)
gravityMult = input.float(1.5, "Gravity Stretch 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)
tf15m = input.timeframe("15", "15m 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_visual = "Visual Elements (On/Off)"
showGravityLine = input.bool(true, "Show Gravity Center Line", group=grp_visual)
showBands = input.bool(true, "Show Upper & Lower Bands", group=grp_visual)
showFill = input.bool(true, "Show Gravity Warzone Fill", group=grp_visual)
showBg = input.bool(true, "Show Battlefield Background Color", group=grp_visual)
showSignals = input.bool(true, "Show ATK/DEF Fire Signals", group=grp_visual)
grp_panels = "Dashboard Panels (On/Off)"
show_table = input.bool(true, "Show MTF Firepower Panel (Top Right)", group=grp_panels)
show_metrics = input.bool(true, "Show Tactical Regime Gauge (Bottom Right)", group=grp_panels)
grp_ui_size = "Panel Size & Style"
tableTextSizeStr = input.string("Small", "Table Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grp_ui_size)
gaugeTextSizeStr = input.string("Large", "Gauge Action Size", options=["Small", "Normal", "Large", "Huge"], group=grp_ui_size)
f_get_text_size(string s) =>
switch s
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.huge
tableSize = f_get_text_size(tableTextSizeStr)
gaugeSize = f_get_text_size(gaugeTextSizeStr)
// ═══════════════════════════ 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_fill_row(table tb, int row, string tf_name, MTFData data) =>
table.cell(tb, 0, row, tf_name, text_color=color.white, text_size=tableSize)
table.cell(tb, 1, row, data.status + " " + data.arrow, text_color=data.col, text_size=tableSize)
table.cell(tb, 2, row, str.tostring(data.ratio, "#.#") + "%", text_color=color.white, text_size=tableSize)
table.cell(tb, 3, row, "[☢ " + str.tostring(data.atk, "#.#") + "]", text_color=color.teal, text_size=tableSize)
table.cell(tb, 4, row, "[⛨ " + str.tostring(data.def, "#.#") + "]", text_color=color.red, text_size=tableSize)
// ═══════════════════════════ 4. Data & Gravity Warzone ═══════════════════════════
d_data = f_calc_mtf(tfD)
h4_data = f_calc_mtf(tf4H)
h1_data = f_calc_mtf(tf1H)
m15_data = f_calc_mtf(tf15m)
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)
upper_gravity = base_gravity + (atr_val * gravityMult)
lower_gravity = base_gravity - (atr_val * gravityMult)
// 🌟 应用视觉开关
plot(showGravityLine ? base_gravity : na, title="Gravity Center", color=color.new(color.yellow, 0), linewidth=2)
p1 = plot(showBands ? upper_gravity : na, title="Upper Band", color=color.new(color.red, 50), style=plot.style_circles)
p2 = plot(showBands ? lower_gravity : na, title="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. Tactical Fire Signals ═══════════════════════════
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
plotshape(showSignals and final_buy, title="Long Attack", style=shape.triangleup, location=location.belowbar, color=color.new(color.teal, 0), size=size.small, text="ATK", textcolor=color.white)
plotshape(showSignals and final_sell, title="Short Bombing", style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small, text="DEF", textcolor=color.white)
// ═══════════════════════════ 6. Capital Decay (CDR) & Capital Commitment (CCR) ═══════════════════════════
vol_ratio = ta.atr(5) / math.max(ta.atr(20), syminfo.mintick)
decay_base = 100 * (1 - math.min(vol_ratio, 1.0))
in_gravity_mid = math.abs(close - base_gravity) < (atr_val * 0.5)
final_decay = in_gravity_mid ? math.min(decay_base * 1.5, 100) : decay_base
vol_ma = ta.sma(volume, 20)
vol_std = ta.stdev(volume, 20)
is_volume_valid = nz(vol_std) > 0 and nz(vol_ma) > 0
vol_zscore = is_volume_valid ? (volume - vol_ma) / vol_std : (math.abs(close - open) / (high - low + syminfo.mintick)) * 2
body_ratio = math.abs(close - open) / math.max(high - low, syminfo.mintick)
ccr_raw = is_volume_valid ? ((vol_zscore + 2) / 5) * 100 * body_ratio : (body_ratio * 100)
final_ccr = math.max(0, math.min(ccr_raw, 100))
cdr_color = final_decay > 70 ? color.new(color.red, 0) : final_decay > 40 ? color.new(color.orange, 0) : color.new(color.green, 0)
ccr_color = final_ccr > 70 ? color.new(color.teal, 0) : final_ccr > 40 ? color.new(color.blue, 0) : color.new(color.gray, 0)
cdr_arrow = final_decay > nz(final_decay[1]) ? "▲" : "▼"
cdr_arrow_color = final_decay > nz(final_decay[1]) ? color.new(color.red, 0) : color.new(color.green, 0)
ccr_arrow = final_ccr > nz(final_ccr[1]) ? "▲" : "▼"
ccr_arrow_color = final_ccr > nz(final_ccr[1]) ? color.new(color.green, 0) : color.new(color.red, 0)
color bgCol = na
if showBg
if final_buy
bgCol := color.new(color.teal, 85)
else if final_sell
bgCol := color.new(color.red, 85)
else if close > upper_gravity
bgCol := color.new(color.red, 92)
else if close < lower_gravity
bgCol := color.new(color.teal, 92)
bgcolor(bgCol, title="Battlefield Situation")
// ═══════════════════════════ 7. Regime Engine (Castle Kingdom Directives) ═══════════════════════════
bool mtf_aligned_bull = d_data.ratio > 50 and h4_data.ratio > 50 and h1_data.ratio > 50
bool mtf_aligned_bear = d_data.ratio < 50 and h4_data.ratio < 50 and h1_data.ratio < 50
var RegimeData regime = RegimeData.new(0, "Analyzing", "Scouting the realms...", "Awaiting King's Orders", color.gray, color.gray, color.white)
var int last_regime_code = 0
if mtf_aligned_bull and final_ccr > 60 and final_decay < 40
regime.code := 1
regime.state_name := "👑 KING'S CRUSADE"
regime.description := "All zones aligned, massive capital commitment."
regime.action := "⚔️ FULL ATTACK (LONG)"
regime.action_col := color.new(color.teal, 0)
regime.box_color := color.new(color.teal, 10)
regime.text_color := color.white
else if mtf_aligned_bear and final_ccr > 60 and final_decay < 40
regime.code := 2
regime.state_name := "🏰 CASTLE SIEGE"
regime.description := "Enemy overwhelming force, total collapse."
regime.action := "🛡️ DEFEND (SHORT)"
regime.action_col := color.new(color.red, 0)
regime.box_color := color.new(color.red, 10)
regime.text_color := color.white
else if final_decay > 65 and not mtf_aligned_bull and not mtf_aligned_bear
regime.code := 3
regime.state_name := "💤 STALE MATE"
regime.description := "Choppy no-man's land, severe capital decay."
regime.action := "🍵 HOLD THE GATE (WAIT)"
regime.action_col := color.new(color.orange, 0)
regime.box_color := color.new(color.orange, 20)
regime.text_color := color.white
else if final_decay > 65 and (mtf_aligned_bull or mtf_aligned_bear)
regime.code := 4
regime.state_name := "🩸 BLEEDING OUT"
regime.description := "Army is exhausted, supply lines are broken."
regime.action := "🏳️ RETREAT (TAKE PROFIT)"
regime.action_col := color.new(color.yellow, 0)
regime.box_color := color.new(color.yellow, 20)
regime.text_color := color.black
else if (close > upper_gravity or close < lower_gravity) and final_decay > 50
regime.code := 5
regime.state_name := "🏹 AMBUSH"
regime.description := "Enemy overextended into our territory."
regime.action := "🎯 COUNTER-STRIKE (REV)"
regime.action_col := color.new(color.purple, 0)
regime.box_color := color.new(color.purple, 20)
regime.text_color := color.white
else
regime.code := 0
regime.state_name := "🛡️ STALEMATE"
regime.description := "Borders are stable. Patrol and observe."
regime.action := "🍵 PATROL (WAIT)"
regime.action_col := color.new(color.gray, 0)
regime.box_color := color.new(color.gray, 50)
regime.text_color := color.white
bool regime_changed = regime.code != last_regime_code
last_regime_code := regime.code
// ═══════════════════════════ 8. Draw MTF Firepower Panel (Top Right) ═══════════════════════════
var table tb = table.new(position.top_right, 6, 7, bgcolor=color.new(color.black, 70), frame_color=color.new(color.gray, 30), frame_width=2, border_width=1)
if show_table and barstate.islast
table.cell(tb, 0, 0, "Zone", text_color=color.gray, text_size=tableSize)
table.cell(tb, 1, 0, "Status", text_color=color.gray, text_size=tableSize)
table.cell(tb, 2, 0, "Air Superiority", text_color=color.gray, text_size=tableSize)
table.cell(tb, 3, 0, "Long (ATK)", text_color=color.teal, text_size=tableSize)
table.cell(tb, 4, 0, "Short (DEF)", text_color=color.red, text_size=tableSize)
table.cell(tb, 5, 0, "Trend", text_color=color.gray, text_size=tableSize)
f_fill_row(tb, 1, "Daily Zone", d_data)
f_fill_row(tb, 2, "4H Zone", h4_data)
f_fill_row(tb, 3, "1H Zone", h1_data)
f_fill_row(tb, 4, "15m Zone", m15_data)
if show_metrics
table.cell(tb, 0, 5, "Logistics (CDR)", text_color=color.white, text_size=tableSize)
table.merge_cells(tb, 1, 5, 4, 5)
table.cell(tb, 1, 5, str.tostring(final_decay, "#.#") + "%", text_color=cdr_color, text_size=tableSize)
table.cell(tb, 5, 5, cdr_arrow, text_color=cdr_arrow_color, text_size=tableSize)
table.cell(tb, 0, 6, "Commitment (CCR)", text_color=color.white, text_size=tableSize)
table.merge_cells(tb, 1, 6, 4, 6)
table.cell(tb, 1, 6, str.tostring(final_ccr, "#.#") + "%", text_color=ccr_color, text_size=tableSize)
table.cell(tb, 5, 6, ccr_arrow, text_color=ccr_arrow_color, text_size=tableSize)
// ═══════════════════════════ 9. Draw Tactical Gauge (Bottom Right) ═══════════════════════════
var table gauge = table.new(position.bottom_right, 4, 7, bgcolor=color.new(color.black, 70), border_width=2, border_color=color.gray)
if show_metrics and barstate.islast
table.cell(gauge, 0, 0, "== KINGDOM COMMAND ==", text_color=color.gray, text_size=size.small)
table.merge_cells(gauge, 0, 0, 3, 0)
table.cell(gauge, 0, 1, regime.state_name, text_color=regime.text_color, bgcolor=regime.box_color, text_size=gaugeSize)
table.merge_cells(gauge, 0, 1, 3, 1)
table.cell(gauge, 0, 2, regime.action, text_color=regime.action_col, text_size=gaugeSize)
table.merge_cells(gauge, 0, 2, 3, 2)
table.cell(gauge, 0, 3, regime.description, text_color=color.white, text_size=size.small)
table.merge_cells(gauge, 0, 3, 3, 3)
table.cell(gauge, 0, 4, "", bgcolor=color.gray, height=1)
table.merge_cells(gauge, 0, 4, 3, 4)
table.cell(gauge, 0, 5, "Logistics (CDR)", text_color=color.gray, text_size=size.small)
table.merge_cells(gauge, 0, 5, 1, 5)
table.cell(gauge, 2, 5, str.tostring(final_decay, "#.#") + "%", text_color=cdr_color, text_size=size.normal)
table.cell(gauge, 3, 5, cdr_arrow, text_color=cdr_arrow_color, text_size=size.normal)
table.cell(gauge, 0, 6, "Commitment (CCR)", text_color=color.gray, text_size=size.small)
table.merge_cells(gauge, 0, 6, 1, 6)
table.cell(gauge, 2, 6, str.tostring(final_ccr, "#.#") + "%", text_color=ccr_color, text_size=size.normal)
table.cell(gauge, 3, 6, ccr_arrow, text_color=ccr_arrow_color, text_size=size.normal)
// ═══════════════════════════ 10. 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.")
alertcondition(regime_changed, title="Regime Shift - High Command Update", message="【⚔️ BATTLE REPORT】High Command regime status has changed. Check chart immediately!")
if final_buy
alert("ATK FIRE! Current Air Superiority: " + str.tostring(d_data.ratio, "#.#") + "% | Logistics Decay: " + str.tostring(final_decay, "#.#") + "%", alert.freq_once_per_bar_close)
if final_sell
alert("DEF BOMBING! Current Air Superiority: " + str.tostring(d_data.ratio, "#.#") + "% | Logistics Decay: " + str.tostring(final_decay, "#.#") + "%", alert.freq_once_per_bar_close)
if regime_changed and barstate.isconfirmed
alert("【REGIME SHIFT】Status: " + regime.state_name + " | Directive: " + regime.action + " | Advice: " + regime.description, alert.freq_once_per_bar_close)