Jurik Filter [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 Filter [Loxx]", overlay = true, shorttitle='JF [Loxx]', timeframe="", timeframe_gaps=true)

greencolor = #2DD204  
redcolor = #D2042D 

_a_jurik_filt(src, len, phase) =>
    len1 = math.max(math.log(math.sqrt(0.5 * (len-1))) / math.log(2.0) + 2.0, 0)
    pow1 = math.max(len1 - 2.0, 0.5)
    volty = 7.0, avolty = 9.0, vsum = 8.0, bsmax = 5.0, bsmin = 6.0, avgLen = 65
    
    del1 = src - nz(bsmax[1])
    del2 = src - nz(bsmin[1])
    
    div = 1.0 / (10.0 + 10.0 * (math.min(math.max(len-10, 0), 100)) / 100)
    volty := math.abs(del1) > math.abs(del2) ? math.abs(del1) : math.abs(del2)
    vsum := nz(vsum[1]) + div * (volty - nz(volty[10]))
    
    temp_avg = ta.sma(vsum, avgLen)
    
    y = bar_index + 1
    if(y <= avgLen + 1)
        avolty := nz(avolty[1]) + 2.0 * (vsum - nz(avolty[1])) / (avgLen + 1)	   
    else
        avolty := temp_avg

    dVolty = avolty > 0 ? volty / avolty : 0
    dVolty := dVolty > math.pow(len1, 1.0/pow1) ? math.pow(len1, 1.0/pow1) : dVolty
    dVolty := dVolty < 1 ? 1 : dVolty
    pow2 = math.pow(dVolty, pow1)
    len2 = math.sqrt(0.5 * (len - 1)) * len1
    Kv = math.pow(len2 / (len2 + 1), math.sqrt(pow2))
    
    bsmax := del1 > 0 ? src : src - Kv * del1
    bsmin := del2 < 0 ? src : src - Kv * del2
    
    phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
    alpha = math.pow(beta, pow2)
    jma1 = 0.0, e0 = 0.0, e1 = 0.0, e2 = 0.0
    e0 := (1 - alpha) * src + alpha * nz(e0[1])
    e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
    e2 := (e0 + phaseRatio * e1 - nz(jma1[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
    jma1 := e2 + nz(jma1[1])
    jma1

_filt(src, len, filter)=>
    price = src
    filtdev = filter * ta.stdev(src, len)
    price := math.abs(price - price[1]) < filtdev ? price[1] : price
    price

_hlcc4 = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)

filterop = input.string("Both", "Filter Options", options = ["Price", "Jurik MA", "Both"], group=  "Basic Settings")

src = _hlcc4
len = input.int(15, "Length", group= "Basic Settings")
filter = input.int(0, "Filter devaitions", minval = 0, group= "Basic Settings")
phase = input.float(0, "Jurik Phase", group= "Basic Settings")
double = input.bool(false, "Double Smooth?", group= "Basic Settings")

colorbars = input.bool(false, "Color bars?", group= "UI Options")

price = filterop == "Both" or filterop == "Price" ? _filt(src, len, filter) : src

jmaout = double ? _a_jurik_filt(_a_jurik_filt(price, len, phase), len, phase) : _a_jurik_filt(price, len, phase)

out = filterop == "Both" or filterop == "Jurik MA" ? _filt(jmaout, len, filter) : jmaout

plot(out,"Jurik Filter", color = src >= out ? greencolor : redcolor, linewidth = 3)
barcolor(colorbars ? src >= out ? greencolor : redcolor : na)




















