本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © APCapitalTrading
//@version=5
indicator('Chandelier Exit + EMA Filtered Signals', shorttitle='CE+EMA', overlay=true)
// === INPUTS ===
var string calcGroup = 'Calculation'
length = input.int(title='ATR Period', defval=22, group=calcGroup)
mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0, group=calcGroup)
useClose = input.bool(title='Use Close Price for Extremums', defval=true, group=calcGroup)
var string visualGroup = 'Visuals'
showLabels = input.bool(title='Show Buy/Sell Labels', defval=true, group=visualGroup)
highlightState = input.bool(title='Highlight State', defval=true, group=visualGroup)
showEMAs = input.bool(title='Show EMA 13/50/200', defval=true, group=visualGroup)
var string alertGroup = 'Alerts'
awaitBarConfirmation = input.bool(title="Await Bar Confirmation", defval=true, group=alertGroup)
// === CHANDELIER CALCULATION ===
atr = mult * ta.atr(length)
longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
// === EMA FILTER ===
ema13 = ta.ema(close, 13)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)
plot(showEMAs ? ema13 : na, title="EMA 13", color=color.orange)
plot(showEMAs ? ema50 : na, title="EMA 50", color=color.blue)
plot(showEMAs ? ema200 : na, title="EMA 200", color=color.purple)
emaBullish = close > ema13 and close > ema50 and close > ema200
emaBearish = close < ema13 and close < ema50 and close < ema200
// === COLORS ===
var color longColor = color.green
var color shortColor = color.red
var color longFillColor = color.new(color.green, 90)
var color shortFillColor = color.new(color.red, 90)
var color textColor = color.new(color.white, 0)
// === SIGNALS ===
buySignal = dir == 1 and dir[1] == -1 and emaBullish
sellSignal = dir == -1 and dir[1] == 1 and emaBearish
// === PLOTS ===
longStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))
shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=textColor)
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=textColor)
plotshape(buySignal ? longStop : na, title='Buy Start', location=location.absolute, style=shape.circle, size=size.tiny, color=longColor)
plotshape(sellSignal ? shortStop : na, title='Sell Start', location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor)
midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)
longStateFillColor = highlightState and dir == 1 ? longFillColor : na
shortStateFillColor = highlightState and dir == -1 ? shortFillColor : na
fill(midPricePlot, longStopPlot, title='Long State Filling', color=longStateFillColor)
fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortStateFillColor)
// === CANDLE HIGHLIGHTING ===
barcolor(buySignal ? color.new(color.green, 0) : sellSignal ? color.new(color.red, 0) : na)
// === ALERTS ===
await = awaitBarConfirmation ? barstate.isconfirmed : true
alertcondition(dir != dir[1] and await, title='Alert: CE Direction Change', message='Chandelier Exit has changed direction!')
alertcondition(buySignal and await, title='Alert: CE Buy', message='Chandelier Exit Buy (with EMA filter)!')
alertcondition(sellSignal and await, title='Alert: CE Sell', message='Chandelier Exit Sell (with EMA filter)!')