本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator('ZLSMA with Chandelier Exit', overlay=true)
// ZLSMA Indicator
length_zlsma = input.int(title="ZLSMA Length", defval=50, group='ZLSMA') // Changed to 50
offset_zlsma = input.int(title="ZLSMA Offset", defval=0, group='ZLSMA')
src_zlsma = input(close, title="ZLSMA Source", group='ZLSMA')
lsma = ta.linreg(src_zlsma, length_zlsma, offset_zlsma)
lsma2 = ta.linreg(lsma, length_zlsma, offset_zlsma)
eq = lsma - lsma2
zlsma = lsma + eq
plot(zlsma, color=color.yellow, linewidth=3, title="ZLSMA") // ZLSMA with linewidth 3
// Chandelier Exit Indicator
var string calcGroup = 'Chandelier Exit Settings'
length_ce = input.int(title='ATR Period', defval=22, group=calcGroup)
mult_ce = input.float(title='ATR Multiplier', step=0.1, defval=3.0, group=calcGroup)
useClose_ce = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)
var string visualGroup = 'Chandelier Exit Visuals'
showLabels_ce = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState_ce = input.bool(title='Highlight State', defval=true, group=visualGroup)
var string alertGroup_ce = 'Chandelier Exit Alerts'
awaitBarConfirmation_ce = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup_ce)
atr_ce = mult_ce * ta.atr(length_ce)
longStop_ce = (useClose_ce ? ta.highest(close, length_ce) : ta.highest(length_ce)) - atr_ce
longStopPrev_ce = nz(longStop_ce[1], longStop_ce)
longStop_ce := close[1] > longStopPrev_ce ? math.max(longStop_ce, longStopPrev_ce) : longStop_ce
shortStop_ce = (useClose_ce ? ta.lowest(close, length_ce) : ta.lowest(length_ce)) + atr_ce
shortStopPrev_ce = nz(shortStop_ce[1], shortStop_ce)
shortStop_ce := close[1] < shortStopPrev_ce ? math.min(shortStop_ce, shortStopPrev_ce) : shortStop_ce
var int dir_ce = 1
dir_ce := close > shortStopPrev_ce ? 1 : close < longStopPrev_ce ? -1 : dir_ce
var color longColor_ce = color.green
var color shortColor_ce = color.red
var color longFillColor_ce = color.new(color.green, 90)
var color shortFillColor_ce = color.new(color.red, 90)
var color textColor_ce = color.new(color.white, 0)
longStopPlot_ce = plot(dir_ce == 1 ? longStop_ce : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor_ce, 0)) // Ensure linewidth >= 1
buySignal_ce = dir_ce == 1 and dir_ce[1] == -1
plotshape(buySignal_ce ? longStop_ce : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor_ce, 0))
plotshape(buySignal_ce and showLabels_ce ? longStop_ce : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(longColor_ce, 0), textcolor=textColor_ce)
shortStopPlot_ce = plot(dir_ce == 1 ? na : shortStop_ce, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor_ce, 0)) // Ensure linewidth >= 1
sellSignal_ce = dir_ce == -1 and dir_ce[1] == 1
plotshape(sellSignal_ce ? shortStop_ce : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor_ce, 0))
plotshape(sellSignal_ce and showLabels_ce ? shortStop_ce : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(shortColor_ce, 0), textcolor=textColor_ce)
midPricePlot_ce = plot(ohlc4, title='', style=plot.style_circles, linewidth=1, display=display.none, editable=false) // Ensure linewidth >= 1
longStateFillColor_ce = highlightState_ce ? dir_ce == 1 ? longFillColor_ce : na : na
shortStateFillColor_ce = highlightState_ce ? dir_ce == -1 ? shortFillColor_ce : na : na
fill(midPricePlot_ce, longStopPlot_ce, title='Long State Filling', color=longStateFillColor_ce)
fill(midPricePlot_ce, shortStopPlot_ce, title='Short State Filling', color=shortStateFillColor_ce)
await_ce = awaitBarConfirmation_ce ? barstate.isconfirmed : true
alertcondition(dir_ce != dir_ce[1] and await_ce, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
alertcondition(buySignal_ce and await_ce, title='Alert: CE Buy', message='Chandelier Exit Buy!')
alertcondition(sellSignal_ce and await_ce, title='Alert: CE Sell', message='Chandelier Exit Sell!')