Jurik Volty 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("Jurik Volty Adaptive EMA [Loxx]", shorttitle = "JVAEMA [Loxx]", overlay = true, timeframe="", timeframe_gaps = true, max_bars_back = 2000)

greencolor = #2DD204
redcolor = #D2042D 

EMA(x, t) =>
    _ema = x
    _ema := na(_ema[1]) ? x : (x - nz(_ema[1])) * (2 / (t + 1)) + nz(_ema[1])
    _ema

_a_jurik_filt(src, len) =>
    //static variales
    volty = 0.0, avolty = 0.0, vsum = 0.0, bsmax = src, bsmin = src
    len1 = math.max(math.log(math.sqrt(0.5 * (len-1))) / math.log(2.0) + 2.0, 0)
    len2 = math.sqrt(0.5 * (len - 1)) * len1
    pow1 = math.max(len1 - 2.0, 0.5)
    beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
    div = 1.0 / (10.0 + 10.0 * (math.min(math.max(len-10, 0), 100)) / 100)
    bet = len2 / (len2 + 1)

    //Price volatility
    del1 = src - nz(bsmax[1])
    del2 = src - nz(bsmin[1])
    volty := math.abs(del1) > math.abs(del2) ? math.abs(del1) : math.abs(del2)

    //Relative price volatility factor
    vsum := nz(vsum[1]) + div * (volty - nz(volty[10]))
    avolty := nz(avolty[1]) + (2.0 / (math.max(4.0 * len, 30) + 1.0)) * (vsum - nz(avolty[1]))
    dVolty = avolty > 0 ? volty / avolty : 0
    dVolty := math.max(1, math.min(math.pow(len1, 1.0/pow1), dVolty))
    
    //Jurik volatility bands
    pow2 = math.pow(dVolty, pow1)
    Kv = math.pow(bet, math.sqrt(pow2))
    bsmax := del1 > 0 ? src : src - Kv * del1
    bsmin := del2 < 0 ? src : src - Kv * del2
    
    _temp= vsum != 0 ? avolty / vsum: 1
    _temp

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


emaout = EMA(src, len * math.ceil(_a_jurik_filt(src, len)))

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

barcolor(color = src >= emaout ? greencolor : redcolor)






































