[JL] EMA Trading Zone
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jesse.Lau

//@version=5

indicator('[JL] EMA Trading Zone',  shorttitle="EMATZ",overlay=true)

////////////
// INPUTS //


// MASLOPE
mas_len   = input.int(  50, title = "EMA Short Length",    minval=1)
mas_len2 = input.int(  200, title = "EMA Long Length",    minval=1)
mas_level    = input.int(  20, title = "MA Slope level",  minval=0)
backlen = input.int(  20, title = "Back Length",    minval=1)
atrperiod = input.int(  10, title = "ATR period",    minval=1)
atrindex = input.float(  2, title = "ATR index",    minval=0.1, step = 0.1)

e = ta.ema(close, mas_len)
el = ta.ema(close, mas_len2)

rad2degree=180 / 3.141592653589793238462643  //pi 
ma_s = 0.0
ma_s2 = 0.0
for i=1 to backlen
    ma_s := ma_s + rad2degree*math.atan((e[0]-nz(e[i]))/math.abs(nz(e[i])-nz(e[2*i])))
    ma_s2 := ma_s2 + rad2degree*math.atan((el[0]-nz(el[i]))/math.abs(nz(el[i])-nz(el[2*i])))



ma_slope = ma_s/backlen
ma_slope2 = ma_s2/backlen
color1 = ma_slope > mas_level ? color.lime : ma_slope < -1 * mas_level ? color.red : color.blue
color2 = ma_slope2 > mas_level ? #ADFF2F : ma_slope2 < -1 * mas_level ? #FA8072 : color.blue

color = ma_slope > mas_level and ma_slope2 > mas_level ? color.lime : ma_slope < -1 * mas_level and  ma_slope2 < -1 * mas_level ? color.red : color.blue
e1 = e + atrindex*ta.atr(atrperiod)
e2 = e - atrindex*ta.atr(atrperiod)
l1=plot(e1, color=color.blue, title="e1line",linewidth=1)
l2=plot(e2, color=color.blue, title="e2line",linewidth=1)

fill(l1, l2, title = "Background", color=color, transp=60)

plot(e, color=color1, linewidth=2)
plot(el, color=color2, linewidth=2)