Code Library / Trailing Stop
Pine Script Chandelier Exit
Chandelier Exit indicator for TradingView using ATR to set trailing stops.
Language: Pine Script · Category: Trailing Stop · Tags: chandelier exit, Pine Script, trailing stop, ATR, TradingView
Source code
//@version=5
indicator("Chandelier Exit", overlay=true)
length = input.int(22, "ATR Period")
mult = input.float(3.0, "ATR Multiplier")
atr = ta.atr(length)
longStop = ta.highest(high, length) - atr * mult
shortStop = ta.lowest(low, length) + atr * mult
var float lsPrev = na
var float ssPrev = na
lsPrev := nz(longStop[1], longStop)
ssPrev := nz(shortStop[1], shortStop)
longStop := close[1] > lsPrev ? math.max(longStop, lsPrev) : longStop
shortStop := close[1] < ssPrev ? math.min(shortStop, ssPrev) : shortStop
var int dir = 1
dir := close > ssPrev ? 1 : close < lsPrev ? -1 : dir
plot(dir==1?longStop:na,"Long",color.green,2,plot.style_linebr)
plot(dir==-1?shortStop:na,"Short",color.red,2,plot.style_linebr)How to use this snippet
This is a building block, not a finished trading system. Paste it into your own Pine Script project, compile it, and test it on a demo account with your broker's real spreads before relying on it. You can convert it to MQL4 or MQL5 with the Pine Script converter.