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

//@version=5
indicator("Adaptive Envelope", overlay=true)
int len = input.int(21, "Envelope Period", minval = 2)
string maType = input.string("EMA", "MA type", options = ["EMA", "SMA", "RMA", "WMA"])
float offset = input.float(2, "Envelope Deviation")
int count = input.int(2, "Moving Count", minval = 1, maxval = 5)

float centr = switch maType
    "EMA" => ta.ema(close, len)
    "SMA" => ta.sma(close, len)
    "RMA" => ta.rma(close, len)
    "WMA" => ta.wma(close, len)
    =>
        runtime.error("No matching MA type found.")
        float(na)
        
line1=centr+offset*ta.atr(len)
line2=centr-offset*ta.atr(len)
plot(centr,color=color.blue)
plot(line1,color=color.green)
plot(line2,color=color.red)

line3=centr+2*offset*ta.atr(len)
line4=centr-2*offset*ta.atr(len)

plot(count>1 ? line3 : na, color=color.green)
plot(count>1 ? line4 : na, color=color.red)
 
line5=centr+3*offset*ta.atr(len)
line6=centr-3*offset*ta.atr(len)

plot(count>2 ? line5 : na, color=color.green)
plot(count>2 ? line6 : na, color=color.red)

line7=centr+4*offset*ta.atr(len)
line8=centr-4*offset*ta.atr(len)

plot(count>3 ? line7 : na, color=color.green)
plot(count>3 ? line8 : na, color=color.red)

line9=centr+5*offset*ta.atr(len)
line10=centr-5*offset*ta.atr(len)

plot(count>4 ? line9 : na, color=color.green)
plot(count>4 ? line10 : na, color=color.red)
