We'd like to present you VStop indicator written in Pine Script. Please notice new Pine Script features used in this indicator: variables max_, min_, is_uptrend, vstop. We may refer to previous values of the indicator in the source code (e.g. vstop, is a vstop value on the previous bar) before the actual vstop variable definition. Enjoy and leave your comments!
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.
study("Volatility Stop", shorttitle="VStop", overlay=true) length = input(20) mult = input(2) atr_ = atr(length) max1 = max(nz(max_), close) min1 = min(nz(min_), close) is_uptrend_prev = nz(is_uptrend, true) stop = is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_ vstop_prev = nz(vstop) vstop1 = is_uptrend_prev ? max(vstop_prev, stop) : min(vstop_prev, stop) is_uptrend = close - vstop1 >= 0 is_trend_changed = is_uptrend != is_uptrend_prev max_ = is_trend_changed ? close : max1 min_ = is_trend_changed ? close : min1 vstop = is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1 plot(vstop, color = is_uptrend ? green : red, style=cross, linewidth=2)