top of page

Pine Script Library

Public·1 member

ATR Volatility Stop — Volatility Drop


A Wilder-smoothed ATR trailing stop that ratchets with the trend and flips when price closes through it. One number, one line — but it handles your stop, your trend bias, and your flip alerts in a single study.


## What it does


- Computes Wilder's ATR and projects a trailing stop at `src − mult × ATR` (long) or `src + mult × ATR` (short).

- Ratchets the long stop up and the short stop down as the trend extends — never loosens.

- Flips trend when the source closes through the active stop, then re-seeds the new-direction stop on the flip bar.

- Colors the stop green/red by trend, prints up/down triangles on each flip, and tints the chart background by regime.

- Exposes two `alertcondition` triggers: one on each LONG flip, one on each SHORT flip.


## Recommended settings


- ATR Length: **14** (Wilder's smoothing — `ta.atr` already uses Wilder)

- ATR Multiplier: **3.0** (tighter = more flips; wider = fewer, slower flips)

- Source: **close**


## Ideal timeframe


Works on any chart; sweet spots are the daily for swing position management and the 15m / 1H for intraday trailing stops.


## How to use


Use it as a regime filter *and* a stop. Only take longs while the line is green and below price; only take shorts while it's red and above price. The flip triangle is the signal to flatten, reverse, or stand down — pair it with a structure level (prior swing, VWAP, daily pivot) for the highest-quality entries. On choppy days the multiplier earns its keep: bump it to 4.0–5.0 to ride through noise; drop it to 2.0 for fast scalps.


## Pine Script v5 code


```

//@version=5

// ─────────────────────────────────────────────

// │ MarketFragments.com | DNA & Market │

// │ info@marketfragments.com │

// │ www.marketfragments.com │

// ─────────────────────────────────────────────

// Time →

// │

// █ █ █│ █

// █ █ █ │ █ █

// █ █ █ │ █ █ █ ╭─╮

// █ █ █ │ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ ╰─────╯

// ──────┴──────────────────────────────────────────────────────────────

// T1 T2 T3 T4 T5 T6

//

// ATR Volatility Stop

// Wilder's ATR trailing stop with auto trend-flip and arrows + alerts.

// Free for non-commercial use. Tweaks and forks welcome.


indicator("ATR Volatility Stop [MarketFragments]", shorttitle="ATR Vol Stop", overlay=true)


atrLength = input.int(14, "ATR Length", minval=1)

atrMult = input.float(3.0, "ATR Multiplier", minval=0.1, step=0.1)

src = input.source(close, "Source")


atr_ = ta.atr(atrLength)


longStopRaw = src - atrMult * atr_

shortStopRaw = src + atrMult * atr_


var float stop = na

var int trend = 1


prevStop = nz(stop[1], longStopRaw)

prevTrend = nz(trend[1], 1)


// Update trend state on close-through

trend := src > prevStop and prevTrend == -1 ? 1 :

src < prevStop and prevTrend == 1 ? -1 :

prevTrend


// Update stop: ratchet in trend, re-seed on flip

stop := trend == 1 and prevTrend == 1 ? math.max(longStopRaw, prevStop) :

trend == -1 and prevTrend == -1 ? math.min(shortStopRaw, prevStop) :

trend == 1 ? longStopRaw :

shortStopRaw


flipUp = trend == 1 and prevTrend == -1

flipDown = trend == -1 and prevTrend == 1


plot(stop, "ATR Stop",

color = trend == 1 ? color.new(color.lime, 0) : color.new(color.red, 0),

linewidth = 2,

style = plot.style_linebr)


plotshape(flipUp, "Flip Long", shape.triangleup, location.belowbar, color.lime, size=size.small)

plotshape(flipDown, "Flip Short", shape.triangledown, location.abovebar, color.red, size=size.small)


bgcolor(trend == 1 ? color.new(color.lime, 92) : color.new(color.red, 92))


alertcondition(flipUp, "ATR Stop flip Long", "ATR Volatility Stop flipped LONG")

alertcondition(flipDown, "ATR Stop flip Short", "ATR Volatility Stop flipped SHORT")

```


Drop your tweaks, screenshots, and questions below — that's how the library gets better.


Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page