本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator("Chandelier Exit with ZLSMA SwiftEdge", overlay=true)
// --- Inputs ---
atr_period = input.int(1, title="ATR Period", minval=1)
atr_multiplier = input.float(2.0, title="ATR Multiplier", minval=0.1)
lsma_length = input.int(50, title="ZLSMA Length", minval=1)
use_close = input.bool(true, title="Use Close Price for Extremums")
// --- Chandelier Exit Calculation ---
atr = ta.atr(atr_period)
highest_high = use_close ? ta.highest(close, atr_period) : ta.highest(high, atr_period)
lowest_low = use_close ? ta.lowest(close, atr_period) : ta.lowest(low, atr_period)
// Initialize stops
var float long_stop = na
var float short_stop = na
var int dir = 1
// Calculate stops
long_stop := highest_high - atr * atr_multiplier
short_stop := lowest_low + atr * atr_multiplier
// Adjust stops based on previous values
long_stop_prev = nz(long_stop[1], long_stop)
short_stop_prev = nz(short_stop[1], short_stop)
long_stop := close[1] > long_stop_prev ? math.max(long_stop, long_stop_prev) : long_stop
short_stop := close[1] < short_stop_prev ? math.min(short_stop, short_stop_prev) : short_stop
// Determine direction
dir := close > short_stop_prev ? 1 : close < long_stop_prev ? -1 : dir
// --- Zero Lag LSMA Calculation ---
lsma = ta.linreg(close, lsma_length, 0)
lsma2 = ta.linreg(lsma, lsma_length, 0)
zlsma = 2 * lsma - lsma2
// --- Signal Logic with ZLSMA First-Cross Filter ---
buy_signal = (dir == 1 and dir[1] == -1) and (close > zlsma and close[1] <= zlsma[1])
sell_signal = (dir == -1 and dir[1] == 1) and (close < zlsma and close[1] >= zlsma[1])
// --- Signal Strength Calculation ---
zlsma_distance = math.abs(close - zlsma) / atr
candle_size = math.abs(close - open) / atr
signal_strength = (zlsma_distance * 0.6 + candle_size * 0.4) * 100
signal_strength := math.min(100, math.max(0, signal_strength))
// --- Plot Buy/Sell Labels with Signal Strength ---
if buy_signal
label.new(bar_index, low, "Buy (" + str.tostring(math.round(signal_strength)) + "%)", color=color.new(color.green, 20), style=label.style_label_up, textcolor=color.white, size=size.normal)
if sell_signal
label.new(bar_index, high, "Sell (" + str.tostring(math.round(signal_strength)) + "%)", color=color.new(color.red, 20), style=label.style_label_down, textcolor=color.white, size=size.normal)
// --- Background Highlight for Signals ---
bgcolor(buy_signal ? color.new(color.green, 90) : sell_signal ? color.new(color.red, 90) : na)
// --- Plot ZLSMA with Dynamic Color ---
zlsma_color = close > zlsma ? color.green : color.red
plot(zlsma, title="Zero Lag LSMA", color=zlsma_color, linewidth=3)
// --- Plot Trend Direction Arrows ---
plotshape(buy_signal, title="Bullish Arrow", location=location.belowbar, style=shape.triangleup, size=size.tiny, color=color.green)
plotshape(sell_signal, title="Bearish Arrow", location=location.abovebar, style=shape.triangledown, size=size.tiny, color=color.red)