RicardoSantos

[RS]MTF Volatility Stop V0

request for: jackmk

Open-source script

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.

Want to use this script on a chart?
//@version=2
study(title='[RS]MTF Volatility Stop V0', shorttitle='mtfVS', overlay=true)
multiplier = input(title='True Range Multiplier', type=float, defval=1.618)
tf = input(title='Timeframe to pool ATR:', type=string, defval='D', confirm=false)
length = input(title='ATR Length', type=integer, defval=20, minval=1)
range = security(tickerid, tf, atr(length))

trend = na(trend[1]) ? +1 : close > vstop[1] ? +1 : close < vstop[1] ? -1 : trend[1]
max_close = change(trend) != 0 ? close : close >= max_close[1] ? close : max_close[1]
min_close = change(trend) != 0 ? close : close <= min_close[1] ? close : min_close[1]
vstop = na(vstop[1]) ? hl2 :
         change(trend) > 0 ? close - (multiplier * range) : 
         trend > 0 ? max(vstop[1], max_close - (multiplier * range)) :
         change(trend) < 0 ? close + (multiplier * range) : 
         trend < 0 ? min(vstop[1], min_close + (multiplier * range)) : 
         vstop[1]

plot(title='mtfVS', series=vstop, style=circles, color=close>=vstop?lime:red, transp=0, linewidth=2)
plot(title='mtfVS', series=vstop, style=line, color=black, transp=0, linewidth=1)