ATR Adaptive EMA [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("ATR adaptive EMA [Loxx]", shorttitle = "ATRAEMA [Loxx]", overlay = true, timeframe="", timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

src = input.source(close, "Source", group = "Basic Settings")
len = input.int(28, "Length", group = "Basic Settings")
colorbars = input.bool(true, title='Color bars?', group = "UI Options")

_atr_adapt_ema(src, len)=>
    atr = ta.atr(len)
    atr /= len
    atrMax = ta.highest(atr, len)           
    atrMin = ta.lowest(atr, len)     
    coeff = atrMin != atrMax ? 1 - (atr - atrMin) / (atrMax - atrMin) : 0.5
    alpha = 2.0 / (1 + len * (coeff + 1.0) / 2.0)
    emaout = 0.0
    emaout := nz(emaout[1]) + alpha * (src - nz(emaout[1]))
    emaout
    
out = _atr_adapt_ema(src, len)

plot(out, color = src >= out ? greencolor : redcolor, linewidth = 3)
barcolor(color = src >= out ? greencolor : redcolor)