FDI-Adaptive, Jurik-Filtered, TMA w/ Price Zones [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("FDI-Adaptive, Jurik-Filtered, TMA w/ Price Zones [Loxx]", 
     shorttitle="FDIAJFTMAPZ [Loxx]", 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxjuriktools/1
import loxx/loxxexpandedsourcetypes/4

greencolor = #2DD204  
redcolor = #D2042D 

fdip(float src, int per, int speedin)=>
    float fmax = ta.highest(src, per)
    float fmin = ta.lowest(src,  per)
    float length = 0
    float diff = 0
    for i = 1 to per - 1
        diff := (nz(src[i]) - fmin) / (fmax - fmin)
        if i > 0
            length += math.sqrt( math.pow(nz(diff[i]) - nz(diff[i + 1]), 2) + (1 / math.pow(per, 2)))
    float fdi = 1 + (math.log(length) + math.log(2)) / math.log(2 * per)
    float traildim = 1 / (2 - fdi)
    float alpha = traildim / 2
    int speed = math.round(speedin * alpha)
    speed
    
calcrng(per)=>
    float lsum = (per + 1) * low
    float hsum = (per + 1) * high
    float sumw = (per + 1)
    int k = per
    for j = 1 to per
        lsum += k * nz(low[j])
        hsum += k * nz(high[j])
        sumw += k
        k -= 1
    float out = (hsum / sumw - lsum / sumw)
    out

src = input.source(hl2, "Source", group= "Source Settings")
per = input.int(30, "Fractal Period Ingest", group = "Basic Settings")
speed = input.int(20, "Speed", group = "Basic Settings")

smthper = input.int(30, "Jurik Smoothing Period", group = "Jurik Settings")
smthphs = input.float(0., "Jurik Smoothing Phase", group = "Jurik Settings")

rngper = input.int(5, "Range Period", group = "Price Zone Settings")
dev = input.float(1.8, "Deviation", group = "Price Zone Settings")
colorbars = input.bool(true, "Color bars?", group = "UI Options")
showsignals = input.bool(true, "Show signals?", group = "UI Options")

fdiper = fdip(src, per, speed)

sum = (fdiper + 1) * src
sumw = (fdiper + 1)
k = fdiper

for j = 1 to fdiper 
    sum += k * nz(src[j])
    sumw += k
    k -= 1

tma = loxxjuriktools.jurik_filt(sum / sumw, smthper, smthphs)
sig = tma[1]

rng = calcrng(rngper)

uplvl = tma + dev * rng
dnlvl = tma - dev * rng

colorout = tma > sig ? greencolor : redcolor

plot(tma, "TMA", color = colorout, linewidth = 3)
plot(uplvl, "Upper Channel", color = color.gray, linewidth = 1)
plot(dnlvl, "Lower Channel", color = color.gray, linewidth = 1)

barcolor(colorbars ? colorout : na)

goLong = ta.crossover(tma, sig) 
goShort = ta.crossunder(tma, sig)  

plotshape(goLong and showsignals, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.belowbar, size = size.tiny)
plotshape(goShort and showsignals, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.abovebar, size = size.tiny)

alertcondition(goLong, title="Long", message="FDI-Adaptive, Jurik-Filtered, TMA w/ Price Zones [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="FDI-Adaptive, Jurik-Filtered, TMA w/ Price Zones [Loxx]]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")
