Stochastic ATR Volatility Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © spacekadet17
// @version=5

// NOTES: As x, k and d use;
// 21-10-3 for 1 Hour candles
// 5-4-2 for 4 Hour candles
// 21-21-3 for 1 Day candles

// Yellow plot is the main oscillator. Orange plot, and hlines at 20, 50 and 80 can be used as signal lines.
// I personally use hlines as the signal in 1H as it's the best timeframe for the indicator.

// If you are in a long position, sell when yellow plot crosses 80 or 50 line downwards;
// and buy when the line crosses 20, 50 or 75 upwards while you are not in a trade.

indicator(title="Stochastic ATR Volatility Oscillator", shorttitle="SATRVO", format=format.price, precision=2, timeframe="", timeframe_gaps=true, overlay = false)
x = input.int(21,"Length")
k = input.int(10," k Length")
d = input.int(3," d Length")

//percent change of candles / neutral
pcAtrn = ( ( low[1]>high ? low[1]: high ) / ( low>high[1] ? high[1] : low ) - 1 ) * 100
//percent change of candles / + or - considered
pcAtr = close>open ? ( ( close[1]>high ? close[1]: high ) / ( low>close[1] ? close[1] : low ) - 1 ) * 100 : ( ( low>close[1] ? close[1] : low ) / ( close[1]>high ? close[1]: high ) - 1 ) * 100 

//stochastic atr volatilty oscilator
smaorema = input.float(0.1, "0(sma) to 1(ema) slider")
sigma = input.float(2, "sigma of atr average")
avgPcAtr = ta.alma(pcAtr,x,smaorema,sigma)
stoatr = ta.stoch(avgPcAtr,avgPcAtr,avgPcAtr,k)

//smoothened oscilators
stoatr3 = ta.ema(ta.ema(stoatr,d),d)
stoatr3sig = ta.ema(stoatr3,d)

//plots
plot(stoatr3,color = color.yellow)
plot(stoatr3sig,color = color.orange)

// signal lines
h0 = plot(80, "Upper Band", color=color.red)
h1 = plot(20, "Lower Band", color=color.green)
h2 = plot(50, "Mid Band", color=color.orange)
fill(h0, h1, color=color.rgb(150, 150, 150, 95), title="Background")