本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blackcat1402
//@version=6
indicator(title='[blackcat] L2 Volatility Shield', shorttitle='L2 VS', overlay=false, max_labels_count=500)
// ============================================================================
// COLOR SEMANTICS SYSTEM (Visualization Standard)
// ============================================================================
color C_LONG = color.new(color.green, 0)
color C_SHORT = color.new(color.red, 0)
color C_EXIT = color.new(color.orange, 0)
color C_BGLONG = color.new(color.green, 95)
color C_BGSHORT = color.new(color.red, 95)
// ============================================================================
// INPUT PARAMETERS (Grouped)
// ============================================================================
// --- Volatility Settings ---
grpVol = "Volatility Settings"
volatility_period = input.int(14, "Volatility Period", minval=1, group=grpVol)
volatility_multiplier = input.float(2.0, "Volatility Multiplier", minval=0.5, step=0.25, group=grpVol)
// --- ALMA Settings ---
grpALMA = "ALMA Settings"
alma_len = input.int(21, "ALMA Length", minval=1, group=grpALMA)
alma_offset = input.float(0.85, "ALMA Offset", minval=0.0, maxval=1.0, step=0.05, group=grpALMA)
alma_sigma = input.int(6, "ALMA Sigma", minval=1, group=grpALMA)
smooth_len = input.int(89, "Smooth Period", minval=1, group=grpALMA)
// --- PSAR Settings ---
grpPSAR = "PSAR Settings"
psar_start = input.float(0.02, "PSAR Start", minval=0.001, step=0.005, group=grpPSAR)
psar_step = input.float(0.02, "PSAR Step", minval=0.001, step=0.005, group=grpPSAR)
psar_max = input.float(0.2, "PSAR Max", minval=0.01, step=0.01, group=grpPSAR)
// --- RSI Settings (for gradient color) ---
grpRSI = "RSI Gradient"
rsi_len = input.int(21, "RSI Length", minval=1, group=grpRSI)
// --- Signal Settings ---
grpSignal = "Signal Settings"
useSignals = input.bool(true, "Enable Trading Signals", group=grpSignal)
cooldownBars = input.int(3, "Cooldown Bars", minval=0, group=grpSignal)
useExclusivity = input.bool(true, "Same-bar Exclusivity", group=grpSignal)
// --- Risk Management (TP/SL) ---
grpRisk = "Risk Management"
useTPSL = input.bool(true, "Show TP/SL Levels", group=grpRisk)
atrLen = input.int(14, "ATR Length", minval=1, group=grpRisk)
slMult = input.float(2.0, "Stop Loss (xATR)", minval=0.5, step=0.25, group=grpRisk)
tpMult = input.float(3.0, "Take Profit (xATR)", minval=0.5, step=0.25, group=grpRisk)
// --- Visualization ---
grpVisual = "Visualization"
showBgcolor = input.bool(true, "Show Background Color", group=grpVisual)
showDashboard = input.bool(true, "Show Dashboard", group=grpVisual)
showLabels = input.bool(true, "Show Signal Labels", group=grpVisual)
// ============================================================================
// FUNCTION: Create Gradient Color Array (Preserved from V1)
// ============================================================================
create_gradient_color_array() =>
var gradient_colors = array.new_color(na)
if barstate.isfirst
array.push(gradient_colors, color.rgb(32, 7, 218))
array.push(gradient_colors, color.new(#2d0aff, 0))
array.push(gradient_colors, color.new(#0038ff, 0))
array.push(gradient_colors, color.new(#0050ff, 0))
array.push(gradient_colors, color.new(#0062ff, 0))
array.push(gradient_colors, color.new(#0071ff, 0))
array.push(gradient_colors, color.new(#007eff, 0))
array.push(gradient_colors, color.new(#0089ff, 0))
array.push(gradient_colors, color.new(#0093ff, 0))
array.push(gradient_colors, color.new(#009dff, 0))
array.push(gradient_colors, color.new(#00a5ff, 0))
array.push(gradient_colors, color.new(#00adff, 0))
array.push(gradient_colors, color.new(#00b5ff, 0))
array.push(gradient_colors, color.new(#00bcff, 0))
array.push(gradient_colors, color.new(#00c2ff, 0))
array.push(gradient_colors, color.new(#00c9ff, 0))
array.push(gradient_colors, color.new(#00cfee, 0))
array.push(gradient_colors, color.new(#00d6d9, 0))
array.push(gradient_colors, color.new(#00dcc4, 0))
array.push(gradient_colors, color.new(#00e2ae, 0))
array.push(gradient_colors, color.new(#00e898, 0))
array.push(gradient_colors, color.new(#00ed82, 0))
array.push(gradient_colors, color.new(#00f26b, 0))
array.push(gradient_colors, color.new(#00f753, 0))
array.push(gradient_colors, color.new(#00fb38, 0))
array.push(gradient_colors, color.new(#0ffe09, 0))
array.push(gradient_colors, color.new(#1bf20a, 0))
array.push(gradient_colors, color.new(#33f203, 0))
array.push(gradient_colors, color.new(#43f300, 0))
array.push(gradient_colors, color.new(#50f300, 0))
array.push(gradient_colors, color.new(#5bf400, 0))
array.push(gradient_colors, color.new(#64f400, 0))
array.push(gradient_colors, color.new(#6df500, 0))
array.push(gradient_colors, color.new(#76f500, 0))
array.push(gradient_colors, color.new(#7df500, 0))
array.push(gradient_colors, color.new(#85f600, 0))
array.push(gradient_colors, color.new(#8cf600, 0))
array.push(gradient_colors, color.new(#93f700, 0))
array.push(gradient_colors, color.new(#99f700, 0))
array.push(gradient_colors, color.new(#9ff700, 0))
array.push(gradient_colors, color.new(#a5f800, 0))
array.push(gradient_colors, color.new(#abf800, 0))
array.push(gradient_colors, color.new(#b1f800, 0))
array.push(gradient_colors, color.new(#b7f800, 0))
array.push(gradient_colors, color.new(#bcf900, 0))
array.push(gradient_colors, color.new(#c2f900, 0))
array.push(gradient_colors, color.new(#c7f900, 0))
array.push(gradient_colors, color.new(#ccf900, 0))
array.push(gradient_colors, color.new(#d1fa00, 0))
array.push(gradient_colors, color.new(#d6fa02, 0))
array.push(gradient_colors, color.new(#dbfa09, 0))
array.push(gradient_colors, color.new(#f6ff0d, 0))
array.push(gradient_colors, color.new(#f9f700, 0))
array.push(gradient_colors, color.new(#fbf000, 0))
array.push(gradient_colors, color.new(#fde800, 0))
array.push(gradient_colors, color.new(#ffe000, 0))
array.push(gradient_colors, color.new(#ffd900, 0))
array.push(gradient_colors, color.new(#ffd100, 0))
array.push(gradient_colors, color.new(#ffc900, 0))
array.push(gradient_colors, color.new(#ffc100, 0))
array.push(gradient_colors, color.new(#ffb900, 0))
array.push(gradient_colors, color.new(#ffb100, 0))
array.push(gradient_colors, color.new(#ffa800, 0))
array.push(gradient_colors, color.new(#ffa000, 0))
array.push(gradient_colors, color.new(#ff9800, 0))
array.push(gradient_colors, color.new(#ff8f00, 0))
array.push(gradient_colors, color.new(#ff8600, 0))
array.push(gradient_colors, color.new(#ff7d00, 0))
array.push(gradient_colors, color.new(#ff7400, 0))
array.push(gradient_colors, color.new(#ff6a00, 0))
array.push(gradient_colors, color.new(#ff6000, 0))
array.push(gradient_colors, color.new(#ff5600, 0))
array.push(gradient_colors, color.new(#ff4a00, 0))
array.push(gradient_colors, color.new(#ff3e00, 0))
array.push(gradient_colors, color.new(#fc2f00, 0))
array.push(gradient_colors, color.new(#fa1a09, 0))
array.push(gradient_colors, color.new(#ff1e0d, 0))
array.push(gradient_colors, color.new(#ff071b, 0))
array.push(gradient_colors, color.new(#ff0025, 0))
array.push(gradient_colors, color.new(#ff002f, 0))
array.push(gradient_colors, color.new(#ff0038, 0))
array.push(gradient_colors, color.new(#ff0041, 0))
array.push(gradient_colors, color.new(#ff0049, 0))
array.push(gradient_colors, color.new(#ff0052, 0))
array.push(gradient_colors, color.new(#ff005b, 0))
array.push(gradient_colors, color.new(#ff0064, 0))
array.push(gradient_colors, color.new(#ff006e, 0))
array.push(gradient_colors, color.new(#ff0077, 0))
array.push(gradient_colors, color.new(#ff0081, 0))
array.push(gradient_colors, color.new(#ff008a, 0))
array.push(gradient_colors, color.new(#ff0094, 0))
array.push(gradient_colors, color.new(#ff009e, 0))
array.push(gradient_colors, color.new(#ff00a8, 0))
array.push(gradient_colors, color.new(#ff00b2, 0))
array.push(gradient_colors, color.new(#ff00bd, 0))
array.push(gradient_colors, color.new(#ff00c7, 0))
array.push(gradient_colors, color.new(#ff00d1, 0))
array.push(gradient_colors, color.new(#ff00dc, 0))
array.push(gradient_colors, color.new(#ff00e6, 0))
array.push(gradient_colors, color.new(#ff00f0, 0))
array.push(gradient_colors, color.new(#f709fa, 0))
gradient_colors
// ============================================================================
// CORE CALCULATIONS (Preserved logic from V1, parameterized)
// ============================================================================
// Smoothed average using ALMA
calculate_smoothed_average_value(close_price) =>
ta.alma(close_price, alma_len, alma_offset, alma_sigma)
// Gradient color based on RSI
calculate_gradient_color(close_price) =>
rsi_value = math.round(ta.rsi(calculate_smoothed_average_value(close_price), rsi_len))
gradient_colors = create_gradient_color_array()
rsi_clamped = math.max(0, math.min(rsi_value, array.size(gradient_colors) - 1))
array.get(gradient_colors, rsi_clamped)
// Parabolic SAR
calculate_parabolic_sar() =>
ta.sar(psar_start, psar_step, psar_max)
// True Range with na protection
calculate_true_range() =>
math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
// Volatility Stop
calculate_volatility_stop(parabolic_sar, true_range) =>
ta.alma(parabolic_sar + volatility_multiplier * true_range * volatility_period, 3, alma_offset, alma_sigma)
// Smooth the Volatility Stop
smooth_volatility_stop(volatility_stop) =>
ta.alma(volatility_stop, smooth_len, alma_offset, alma_sigma)
// ============================================================================
// MAIN CALCULATIONS
// ============================================================================
parabolic_sar = calculate_parabolic_sar()
true_range = calculate_true_range()
volatility_stop = calculate_volatility_stop(parabolic_sar, true_range)
smoothed_volatility_stop = smooth_volatility_stop(volatility_stop)
gradient_color = calculate_gradient_color(close)
atrVal = ta.atr(atrLen)
// ============================================================================
// S1: SIGNAL STATE MACHINE + S2: EDGE DETECTION (W1+W2+W4)
// ============================================================================
var int posState = 0 // -1 short / 0 flat / +1 long
var int lastSignalBar = na
// S7: ATR-BASED TAKE PROFIT / STOP LOSS state (declared early for exit logic)
var float entryPrice = na
var int entryDir = 0
cooldownOK = na(lastSignalBar) or bar_index - lastSignalBar > cooldownBars
// Crossover detection for signal generation
vs_above_smooth = volatility_stop > smoothed_volatility_stop
vs_below_smooth = volatility_stop < smoothed_volatility_stop
crossUp = ta.crossover(volatility_stop, smoothed_volatility_stop)
crossDown = ta.crossunder(volatility_stop, smoothed_volatility_stop)
// Stop-loss hits evaluated against the currently open position
longSLHit = useTPSL and not na(entryPrice) and entryDir == 1 and low <= entryPrice - slMult * atrVal
shortSLHit = useTPSL and not na(entryPrice) and entryDir == -1 and high >= entryPrice + slMult * atrVal
// Raw signal conditions (W1: Bidirectional signals)
rawLongEntry = crossUp and posState <= 0 and cooldownOK and useSignals
rawShortEntry = crossDown and posState >= 0 and cooldownOK and useSignals
rawLongExit = (crossDown or longSLHit) and posState == 1
rawShortExit = (crossUp or shortSLHit) and posState == -1
// Same-bar exclusivity (W4: Signal governance)
longExit = useExclusivity ? rawLongExit and not rawLongEntry and not rawShortEntry : rawLongExit
shortExit = useExclusivity ? rawShortExit and not rawLongEntry and not rawShortEntry : rawShortExit
longEntry = useExclusivity ? rawLongEntry and not longExit and not shortExit : rawLongEntry
shortEntry = useExclusivity ? rawShortEntry and not longExit and not shortExit : rawShortEntry
// State transition (S1)
if longEntry
posState := 1
lastSignalBar := bar_index
else if shortEntry
posState := -1
lastSignalBar := bar_index
else if longExit or shortExit
posState := 0
lastSignalBar := bar_index
// Confirmed edges for alerts (S2)
longEntryConfirmed = barstate.isconfirmed and longEntry
shortEntryConfirmed = barstate.isconfirmed and shortEntry
longExitConfirmed = barstate.isconfirmed and longExit
shortExitConfirmed = barstate.isconfirmed and shortExit
// ============================================================================
// S7: ATR-BASED TAKE PROFIT / STOP LOSS (W3) - position state update
// ============================================================================
if longEntry
entryPrice := close
entryDir := 1
if shortEntry
entryPrice := close
entryDir := -1
if longExit or shortExit
entryPrice := na
entryDir := 0
float slPrice = useTPSL and not na(entryPrice) ? entryPrice - entryDir * slMult * atrVal : na
float tpPrice = useTPSL and not na(entryPrice) ? entryPrice + entryDir * tpMult * atrVal : na
// ============================================================================
// PLOTS (Preserved from V1 + New TP/SL lines)
// ============================================================================
plot(volatility_stop, "Volatility Stop", color=volatility_stop > smoothed_volatility_stop ? color.new(gradient_color, 0) : color.gray, linewidth=2)
plot(smoothed_volatility_stop, "Smoothed VS", color=color.gray, linewidth=2, style=plot.style_stepline)
plot(slPrice, "Stop Loss", color=color.red, style=plot.style_linebr, display=display.data_window)
plot(tpPrice, "Take Profit", color=color.teal, style=plot.style_linebr, display=display.data_window)
plot(posState, "Position State", display=display.data_window)
// ============================================================================
// S5: VISUALIZATION SYSTEM (W5)
// ============================================================================
// Background color for position state
bgcolor(showBgcolor ? (posState == 1 ? C_BGLONG : posState == -1 ? C_BGSHORT : na) : na)
// Signal labels (S5: label.new system)
if showLabels
if longEntry
label.new(bar_index, low, "E", style=label.style_label_up, color=C_LONG, textcolor=color.white, size=size.small)
if shortEntry
label.new(bar_index, high, "S", style=label.style_label_down, color=C_SHORT, textcolor=color.white, size=size.small)
if longExit
label.new(bar_index, high, "XL", style=label.style_xcross, color=C_EXIT, textcolor=color.white, size=size.tiny)
if shortExit
label.new(bar_index, low, "XS", style=label.style_xcross, color=C_EXIT, textcolor=color.white, size=size.tiny)
// Dashboard table (S5: Information panel)
var table dash = table.new(position.top_right, 2, 5, bgcolor=color.new(color.black, 60), border_width=1, frame_color=color.gray)
if showDashboard and barstate.islast
table.cell(dash, 0, 0, "Position", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 0, posState == 1 ? "LONG" : posState == -1 ? "SHORT" : "FLAT", text_color=posState == 1 ? color.green : posState == -1 ? color.red : color.gray, text_size=size.tiny)
table.cell(dash, 0, 1, "VS Trend", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 1, volatility_stop > smoothed_volatility_stop ? "BULLISH" : "BEARISH", text_color=volatility_stop > smoothed_volatility_stop ? color.green : color.red, text_size=size.tiny)
table.cell(dash, 0, 2, "ATR", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 2, str.tostring(atrVal, format.mintick), text_color=color.white, text_size=size.tiny)
table.cell(dash, 0, 3, "Entry Price", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 3, not na(entryPrice) ? str.tostring(entryPrice, format.mintick) : "N/A", text_color=color.white, text_size=size.tiny)
table.cell(dash, 0, 4, "SL / TP", text_color=color.white, text_size=size.tiny)
table.cell(dash, 1, 4, not na(slPrice) ? str.tostring(slPrice, format.mintick) + " / " + str.tostring(tpPrice, format.mintick) : "N/A", text_color=color.white, text_size=size.tiny)
// ============================================================================
// S6: ALERT SYSTEM (W6)
// ============================================================================
if longEntryConfirmed
alert("L2 VS Long Entry @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if shortEntryConfirmed
alert("L2 VS Short Entry @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if longExitConfirmed
alert("L2 VS Long Exit @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
if shortExitConfirmed
alert("L2 VS Short Exit @ " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
alertcondition(longEntry, "Long Entry", "Volatility Stop crossed above Smoothed VS - Bullish entry signal")
alertcondition(shortEntry, "Short Entry", "Volatility Stop crossed below Smoothed VS - Bearish entry signal")
alertcondition(longExit, "Long Exit", "Long position exited - bearish crossover or SL hit")
alertcondition(shortExit, "Short Exit", "Short position exited - bullish crossover or SL hit")