Chandelier Exit + ZLSMA strategy in Pine Script
A Chandelier Exit turn opens a trade only when the price also crosses a zero-lag least squares average (ZLSMA).
Chandelier Exit · ZLSMA · ATR · long and short · by simwai · Mozilla Public License 2.0
What is its backtest worth?
A parameter sweep finds the best setting of the Chandelier Exit + ZLSMA strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.
Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.
The rules
- Works on the average of the previous bar (open, high, low, close).
- Enters long when the Chandelier Exit turns up and that price crosses above the ZLSMA; short on the opposite turn and cross.
- Closes the long when the price crosses back below the ZLSMA, the short when it crosses back above.
What to watch for
- Two signals on the same bar are rare, so the trade count can be low – check it before reading the profit.
- The ATR period is 1 by default: the stop distance then follows a single bar’s range.
How PineTester judges a backtest, with its sources: how we judge results.
The Pine Script code
The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.
//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © simwai
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 100% of equity per entry,
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
strategy('Chandelier Exit ZLSMA Strategy', shorttitle='CE_ZLSMA', overlay = true, calc_on_every_tick = false, process_orders_on_close = true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
// -- Colors --
color maximumYellowRed = color.rgb(255, 203, 98) // yellow
color rajah = color.rgb(242, 166, 84) // orange
color magicMint = color.rgb(171, 237, 198)
color languidLavender = color.rgb(232, 215, 255)
color maximumBluePurple = color.rgb(181, 161, 226)
color skyBlue = color.rgb(144, 226, 244)
color lightGray = color.rgb(214, 214, 214)
color quickSilver = color.rgb(163, 163, 163)
color mediumAquamarine = color.rgb(104, 223, 153)
color carrotOrange = color.rgb(239, 146, 46)
// -- Inputs --
length = input(title='ATR Period', defval=1)
mult = input.float(title='ATR Multiplier', step=0.1, defval=2)
showLabels = input(title='Show Buy/Sell Labels ?', tooltip='Created by Chandelier Exit (CE)', defval=false)
isSignalLabelEnabled = input(title='Show Signal Labels ?', defval=true)
useClose = input(title='Use Close Price for Extrema ?', defval=true)
zcolorchange = input(title='Enable Rising/Decreasing Highlightning', defval=false)
zlsmaLength = input(title='ZLSMA Length', defval=50)
offset = input(title='Offset', defval=0)
// -- CE - Credits to @everget --
float haClose = float(1) / 4 * (open[1] + high[1] + low[1] + close[1])
atr = mult * ta.atr(length)[1]
longStop = (useClose ? ta.highest(haClose, length) : ta.highest(haClose, length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := haClose > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = (useClose ? ta.lowest(haClose, length) : ta.lowest(haClose, length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := haClose < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := haClose > shortStopPrev ? 1 : haClose < longStopPrev ? -1 : dir
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=mediumAquamarine, textcolor=color.white)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=carrotOrange, textcolor=color.white)
changeCond = dir != dir[1]
// -- ZLSMA - Credits to @netweaver2011 --
lsma = ta.linreg(haClose, zlsmaLength, offset)
lsma2 = ta.linreg(lsma, zlsmaLength, offset)
eq = lsma - lsma2
zlsma = lsma + eq
zColor = zcolorchange ? zlsma > zlsma[1] ? magicMint : rajah : languidLavender
plot(zlsma, title='ZLSMA', linewidth=2, color=zColor)
// -- Signals --
var string isTradeOpen = ''
var string signalCache = ''
bool enterLong = buySignal and ta.crossover(haClose, zlsma)
bool exitLong = ta.crossunder(haClose, zlsma)
bool enterShort = sellSignal and ta.crossunder(haClose, zlsma)
bool exitShort = ta.crossover(haClose, zlsma)
if (signalCache == 'long entry')
signalCache := ''
enterLong := true
else if (signalCache == 'short entry')
signalCache := ''
enterShort := true
if (isTradeOpen == '')
if (exitShort and (not enterLong))
exitShort := false
if (exitLong and (not enterShort))
exitLong := false
if (enterLong and exitShort)
isTradeOpen := 'long'
exitShort := false
else if (enterShort and exitLong)
isTradeOpen := 'short'
exitLong := false
else if (enterLong)
isTradeOpen := 'long'
else if (enterShort)
isTradeOpen := 'short'
else if (isTradeOpen == 'long')
if (exitShort)
exitShort := false
if (enterLong)
enterLong := false
if (enterShort and exitLong)
enterShort := false
signalCache := 'short entry'
if (exitLong)
isTradeOpen := ''
else if (isTradeOpen == 'short')
if (exitLong)
exitLong := false
if (enterShort)
enterShort := false
if (enterLong and exitShort)
enterLong := false
signalCache := 'long entry'
if (exitShort)
isTradeOpen := ''
plotshape((isSignalLabelEnabled and enterLong) ? zlsma : na, title='LONG', text='L', style=shape.labelup, color=mediumAquamarine, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and enterShort) ? zlsma : na, title='SHORT', text='S', style=shape.labeldown, color=carrotOrange, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitLong) ? zlsma : na, title='LONG EXIT', style=shape.circle, color=magicMint, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitShort) ? zlsma : na, title='SHORT EXIT', style=shape.circle, color=rajah, size=size.tiny, location=location.absolute)
barcolor(color=isTradeOpen == 'long' ? mediumAquamarine : isTradeOpen == 'short' ? carrotOrange : na)
// -- Long Exits --
if (exitLong and strategy.position_size > 0)
strategy.close('long', comment='EXIT_LONG')
// -- Short Exits --
if (exitShort and strategy.position_size < 0)
strategy.close('short', comment='EXIT_SHORT')
// -- Long Entries --
if (enterLong)
strategy.entry('long', strategy.long, comment='ENTER_LONG')
// -- Short Entries --
if (enterShort)
strategy.entry('short', strategy.short, comment='ENTER_SHORT')
Related strategies
- PresentTrend – An ATR band follows the price and switches sides with the Money Flow Index; a turn of the band opens the trade.
- Donchian Channel MACD Trend – A breakout of the Donchian channel, confirmed by the MACD, with a stop a multiple of the ATR away.
- Crunchster's Turtle and Trend System – A Turtle-style channel breakout with a trailing channel exit and a position size set by recent volatility.
- Hull Kaufman SuperTrend Cloud – A cloud of three lines – Kaufman adaptive average, its Hull average and a Supertrend – that the price has to break.
Open-source script by simwai, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.