Adaptive Price Zone
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EduardoMattje

//@version=5
indicator("Adaptive Price Zone", overlay=true)

var length = input.int(5, "EMA length", minval=2)
var deviationFactor = input.float(1.5, "Deviation factor", minval=0.0)
var offset = input.int(0, "Offset")
var useTR = input.bool(false, "Use TR", tooltip="Use the true range for the volatility instead of the high and low difference.")

getRange() => 
    if useTR
        ta.tr
    else
        high - low

volatility = ta.ema(ta.ema(getRange(), length), length)

getBand(signal) =>
    hl2 + (volatility * deviationFactor * signal)

apzUpper = getBand(1)
apzLower = getBand(-1)

plot(apzUpper, "Upper band", color.red, offset=offset)
plot(apzLower, "Lower band", offset=offset)
