Indicator · Pine Script v5 · On the chart

Volatility Stop

by ivansoft· code credited to TradingView

An indicator on the price chart that plots a trailing stop line offset from a chosen source by a multiple of the Average True Range.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code161
Settings11inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

161 lines of Pine Script v5 from edu_std/volatility_stop.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

volatility_stop.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingView
//@version=5

// Volatility Stop
// v3, 2022.05.13
// This code was written using the recommendations from the Pine Script™ User Manual's Style Guide:
//   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html


indicator("Volatility Stop", "V-Stop", true)

import PineCoders/Time/1 as pcTimeLib



// ———————————————————— Constants {


// —————————— Base colors
var color  GREEN1     = #00FF00FF
var color  DEEPSKY    = #00C0FFFF
var color  RED1       = #FF0000FF
var color  DEEPPINK   = #FF0080FF
var color  CLEARBLACK = #00000000

// —————————— Strings
var string VSRC_TT    = "Source to offset the stop from."
var string VLEN_TT    = "The length (bars back) to use in the ATR calculation."
var string VATR_TT    = "A multiplier to allow adjustment of the stop as a function of the ATR value. Example: 1 is 100% of the calculated ATR value."

var string REV_TT     = "Triggers an alert on changes of the trend's direction. If a delay is used, the alert will be delayed until the close has remained crossed over the stop for the number of minutes defined in the 'Delay' setting."
var string UP_TT      = "Triggers an alert on a cross up of a short stop. If a delay is used, the alert will be delayed until the close has remained crossed over the stop for the number of minutes defined in the 'Delay' setting."
var string DN_TT      = "Triggers an alert on a cross down of a long stop. If a delay is used, the alert will be delayed until the close has remained crossed over the stop for the number of minutes defined in the 'Delay' setting."
var string SEC_TT     = "Minutes for which price must breach the line before an alert triggers. Use '0.5' for 30 seconds."

var string C01        = "Lime/Red"
var string C02        = "Aqua/Pink"
var string S01        = "Line"
var string S02        = "Circles"
var string S03        = "Diamonds"
var string S04        = "Arrows"

var string A1         = alert.freq_once_per_bar
var string A2         = alert.freq_once_per_bar_close
// }



// ———————————————————— Inputs {


var string GRP1       = "════════    Stop Calculations    ════════"
float      srcInput   = input.source(close,  "Source",                   group = GRP1, tooltip = VSRC_TT)
int        lenInput   = input.int(20,        "ATR Length",               group = GRP1, tooltip = VLEN_TT, minval = 2)
float      atrInput   = input.float(2.0,     "ATR Factor",               group = GRP1, tooltip = VATR_TT, minval = 0.25, step = 0.25)

var string GRP2       = "════════════ Line Style ═══════════"
string     colorInput = input.string(C01,    "Color Scheme",             group = GRP2, options = [C01, C02])
string     styleInput = input.string(S01,    "Indicator style",          group = GRP2, options = [S01, S02, S03, S04])
int        widthInput = input.int(2,         "Line Width",               group = GRP2, options = [0, 1, 2, 3])

var string GRP3       = "═════════ Alert Conditions ═════════"
bool       revInput   = input.bool(false,    "Reversal Alert",           group = GRP3, tooltip = REV_TT)
bool       upInput    = input.bool(false,    "Breach of downtrend stop", group = GRP3, tooltip = UP_TT)
bool       dnInput    = input.bool(false,    "Breach of uptrend stop",   group = GRP3, tooltip = DN_TT)
float      delayInput = input.float(0.0,     "Delay in minutes",         group = GRP3, tooltip = SEC_TT, minval = 0.0, step = 0.5)
string     freqInput  = input.string(A1,     "Alert Frequency",          group = GRP3, options = [A1, A2])
// }



// ———————————————————— Calculations {


// @function        Calculates a value from the ATR, which is offset from price action to use as a stop loss or trend detection. 
// @param source    (series int/float) The source to calculate the stop value from.  
// @param atrLength (simple int) The length over which to calcualte the RMA of the true range. (number of bars back).
// @param atrFactor (series int/float) A multiplier to allow adjustment of the stop as a function of the ATR value. Example: 1 is 100% of the calculated ATR value. 
// @returns         ([float, bool]) A tuple of the volatility stop value and the trend direction as a bool. 
vStop(series float source, simple int atrLength, series float atrFactor) =>
    float src = nz(source, close)
    var bool  trendUp = true
    var float max = src
    var float min = src
    var float stop = 0.0
    float atrM  = nz(ta.atr(atrLength) * atrFactor, ta.tr)
    max  := math.max(max, src)
    min  := math.min(min, src)
    stop := nz(trendUp ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src)
    trendUp := src - stop >= 0.0
    if trendUp != nz(trendUp[1], true)
        max  := src
        min  := src
        stop := trendUp ? max - atrM : min + atrM
    [stop, trendUp]

var bool defaultScheme = colorInput == C01
var bool showCircles   = styleInput == S02
var bool showDiamonds  = styleInput == S03
var bool showArrows    = styleInput == S04

// Calculate Volatility Stop 
[stop, trendUp] = vStop(srcInput, lenInput, atrInput)

// Get conditions for colors, plotting, and alerts 
bool trendReversal   = trendUp != trendUp[1]
bool trendChangeToUp = trendUp and not trendUp[1]
bool trendChangeToDn = not trendUp and trendUp[1]

// Get duration of each condition
float secondsUp = pcTimeLib.secondsSince(trendChangeToUp, false)
float secondsDn = pcTimeLib.secondsSince(trendChangeToDn, false)

// Timed alerts 
bool alertUp  = secondsUp >= delayInput * 60
bool alertDn  = secondsDn >= delayInput * 60
bool alertRev = alertUp   or alertDn

// Get colors 
color colorUp      = defaultScheme ? GREEN1     : DEEPSKY
color colorDown    = defaultScheme ? RED1       : DEEPPINK
color schemeColor  = defaultScheme ? trendUp    ? GREEN1 : RED1 :
                     trendUp       ? DEEPSKY    : DEEPPINK
color lineColor    = trendReversal ? CLEARBLACK : schemeColor
// }



// ———————————————————— Plotting {


plotshape(showDiamonds or showCircles ? stop : na, "Diamonds & Circles", showDiamonds ? shape.diamond : shape.circle, location.absolute, schemeColor)
plotchar(showArrows and trendUp ? stop : na, "Arrows Up", "⮝", location.absolute, colorUp)
plotchar(showArrows and not trendUp ? stop : na, "Arrows Dn", "⮟", location.absolute, colorDown)

plot(widthInput != 0 ? stop : na, "V-Stop", lineColor, widthInput)
plot(trendReversal   ? stop : na, "Beg. Circle", schemeColor, math.max(widthInput, 1) + 2, plot.style_circles)
plot(trendReversal   ? stop : na, "Beg. Small circle", #000000ff, math.max(widthInput, 1), plot.style_circles)
// }



// ———————————————————— Alerts {


bool  timed    = delayInput != 0 
float duration = trendChangeToUp ? secondsUp : secondsDn

if alertRev and revInput
    string alertMsg = "Trend Reversal " + (timed ? "Close crossed stop for " + str.tostring(duration / 60, "#.##m") : "")
    alert(alertMsg, freqInput)

if alertUp and upInput
    string alertMsg = "Breach of downtrend stop. " + (timed ? "Close was above stop for " + str.tostring(secondsUp / 60, "#.##m") : "")
    alert(alertMsg, freqInput)
  
if alertDn and dnInput
    string alertMsg = "Breach of uptrend stop. " + (timed ? "Close was above stop for " + str.tostring(secondsDn / 60, "#.##m") : "")
    alert(alertMsg, freqInput)
// }

Overview

Settings set the source, the ATR length and the ATR factor. The line can be drawn as a line, circles, diamonds or arrows in one of two colour schemes. Optional alerts fire on a trend reversal or on a breach of the stop, with an optional delay in minutes and a choice of alert frequency.

Details

Published by

ivansoft on GitHub

Code credit

TradingView, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescripttradingvew

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
SourcesourceclosesrcInput
ATR Lengthint20lenInput
ATR Factorfloat2.0atrInput
Color SchemestringC01colorInput
Indicator stylestringS01styleInput
Line Widthint2widthInput
Reversal AlertboolfalserevInput
Breach of downtrend stopboolfalseupInput
Breach of uptrend stopboolfalsednInput
Delay in minutesfloat0.0delayInput
Alert FrequencystringA1freqInput
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Chandelier Exit

by everget
947 stars

This indicator plots an ATR-based Chandelier Exit trailing stop on the price chart. It tracks a long stop under recent highs and a short stop above recent lows, then switches direction when price crosses the opposite stop. Buy and sell labels mark those flips.

Indicator v6 Volatility & bandsTrend Repaints
Updated 11 days ago · GPL-3.0 GitHub repository of Chandelier Exit

The Momentum Setup is a multi-indicator visual tool designed for trend and momentum analysis directly on the price chart. It combines Relative Strength Index (RSI), Money Flow Index (MFI), Commodity Channel Index (CCI), Supertrend, and a 250-bar percentage cha

Indicator v5 Oscillators & momentumTrend
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - Ankush Bajaj Momentum Investing Setup
330 stars

Range Filter [Alorse] is a technical indicator overlay for TradingView designed to filter market volatility and identify price trends. It calculates a smoothed range based on historical volatility using a configurable sampling period and multiplier applied to

Indicator v5 TrendVolatility & bands
Updated 2 months ago · None stated GitHub repository of Range Filter [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

NVDA Trend Ribbon

by longsurf-ai
280 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 2 days ago · MIT GitHub repository of NVDA Trend Ribbon

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

More Volatility & bands scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers