ATR Trailing Stop — a stop a fixed number of ATRs from the close that
only ever moves in the direction of the trade, and flips side when the
close crosses it. Kernel K6 over atrValues.
d = multiplier × ATR(period) (Wilder's ATR)
close > prev and prevClose > prev -> stop =max(prev, close − d) long, ratchet close < prev and prevClose < prev -> stop =min(prev, close + d) short, ratchet close > prev -> stop = close − d flip long otherwise -> stop = close + d flip short
Appends two columns:
${prefix} — the stop level (ats at the default prefix).
${prefix}Trend — +1 while the stop is below price, −1 above.
The two-column shape and the sign convention are parabolicSar's,
and for the same reason: the side is what a consumer draws, and on a flip
bar the stop can print exactly at the close (when ATR = 0), so
stop < close is not a safe derivation of it.
Which trailing stop — the anchor is the close, and the alternative is named
Two families circulate under names this close together that picking one has
to be explicit:
This study: the band is measured from the close
(close ± multiplier × ATR), ratcheted, with the four-case flip above.
This is the form published as "ATR Trailing Stop" — Sylvain Vervoort's,
and the one TradingView's widely-copied ATR Trailing Stop / "UT Bot"
scripts implement.
The Chandelier Exit (Chuck LeBeau): the band is measured from the
rolling extreme instead (highestHigh(period) − multiplier × ATR
while long, lowestLow(period) + multiplier × ATR while short). That is
a different study, not a variant of this one, and it is a composition
away from what the package already ships — donchian supplies both
extremes and atr the width. It is not implemented here, and this
study deliberately does not grow an anchor: 'close' | 'extreme'
knob: the two differ in more than one place (the extreme form also
needs its own warm-up, max(period, period), and its flip is
conventionally tested on the close against the other side's band), so
a knob would be two studies wearing one name.
high and low are read only by the ATR — the stop itself is a
close-to-close construction.
Warm-up and gaps
Length-preserving; both columns start on the ATR's first bar, which is bar
period on gap-free input. As with superTrend, the ATR is the gap
rule in practice: an interior hole leaves Wilder's recursion NaN to the
end, so the study reads undefined from there. The K6 reset
([PND-SFOLD]) is what governs a gap in close alone.
Edges
In the units of the price: scaling every bar by k scales the stop
by k; adding c shifts it by c. The trend column is unchanged by
either.
A flat stretch gives ATR = 0 and a stop sitting exactly on the
close. The fourth case is written otherwise rather than close < prev
precisely so that close === prev resolves — it flips (or stays) short,
which is the same tie-break the published form uses. No division, so no
zero-denominator case.
multiplier is a width, not a bar count, so it is validated as a
positive finite number rather than by assertPeriod.
ATR Trailing Stop — a stop a fixed number of ATRs from the close that only ever moves in the direction of the trade, and flips side when the close crosses it. Kernel K6 over atrValues.
Appends two columns:
${prefix}— the stop level (atsat the default prefix).${prefix}Trend—+1while the stop is below price,−1above.The two-column shape and the sign convention are parabolicSar's, and for the same reason: the side is what a consumer draws, and on a flip bar the stop can print exactly at the close (when
ATR = 0), sostop < closeis not a safe derivation of it.Which trailing stop — the anchor is the close, and the alternative is named
Two families circulate under names this close together that picking one has to be explicit:
close ± multiplier × ATR), ratcheted, with the four-case flip above. This is the form published as "ATR Trailing Stop" — Sylvain Vervoort's, and the one TradingView's widely-copiedATR Trailing Stop/ "UT Bot" scripts implement.highestHigh(period) − multiplier × ATRwhile long,lowestLow(period) + multiplier × ATRwhile short). That is a different study, not a variant of this one, and it is a composition away from what the package already ships — donchian supplies both extremes and atr the width. It is not implemented here, and this study deliberately does not grow ananchor: 'close' | 'extreme'knob: the two differ in more than one place (the extreme form also needs its own warm-up,max(period, period), and its flip is conventionally tested on the close against the other side's band), so a knob would be two studies wearing one name.highandloware read only by the ATR — the stop itself is a close-to-close construction.Warm-up and gaps
Length-preserving; both columns start on the ATR's first bar, which is bar
periodon gap-free input. As with superTrend, the ATR is the gap rule in practice: an interior hole leaves Wilder's recursionNaNto the end, so the study readsundefinedfrom there. The K6 reset ([PND-SFOLD]) is what governs a gap inclosealone.Edges
kscales the stop byk; addingcshifts it byc. The trend column is unchanged by either.ATR = 0and a stop sitting exactly on the close. The fourth case is writtenotherwiserather thanclose < prevprecisely so thatclose === prevresolves — it flips (or stays) short, which is the same tie-break the published form uses. No division, so no zero-denominator case.multiplieris a width, not a bar count, so it is validated as a positive finite number rather than byassertPeriod.