JMA filter 2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RafaelZioni

//@version=5
indicator('JMA filter 2')



length = input(title='Length', defval=35)
phase = 0
power = 2


src = close

atrLength = 30
mult = 1.5

fastLength = 5
fastPhase = 100
fastPower = 2

calc_jma(_src, _length, _phase, _power) =>
    phaseRatio = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5

    beta = 0.45 * (_length - 1) / (0.45 * (_length - 1) + 2)
    alpha = math.pow(beta, _power)

    e0 = 0.0
    e0 := (1 - alpha) * _src + alpha * nz(e0[1])

    e1 = 0.0
    e1 := (_src - e0) * (1 - beta) + beta * nz(e1[1])

    jma = 0.0
    e2 = 0.0
    e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])

    jma := e2 + nz(jma[1])
    jma

jma = calc_jma(src, length, phase, power)

p = jma / close
p2 = p / -1
z = p2 - p + 2
x = z * 100

Fast = input(2)
Slow = input(4)
C = input(5)

co(sFast, sSlow, f, l, lC) =>
    fastC = ta.sma(sFast, f)
    slowC = ta.sma(sSlow, l)
    cC = fastC + slowC
    ta.sma(cC, lC)
x1 = co(x, x, Fast, Slow, C)
col = x1 >= 0 ? color.lime : color.red
plot(x1, linewidth=2, color=col)
plot(0)
trel = input(-50)
plot(trel, linewidth=2, color=color.new(color.gray, 0))
treh = input(45)
plot(treh, linewidth=2, color=color.new(color.gray, 0))


down = ta.crossunder(x1, treh)
up = ta.crossover(x1, trel)
plotshape(up, title='UP', style=shape.xcross, color=color.new(color.green, 0), location=location.bottom, textcolor=color.new(color.black, 0), text='UP', size=size.tiny)
plotshape(down, title='down', style=shape.cross, color=color.new(color.red, 0), location=location.top, textcolor=color.new(color.black, 0), text='Down', size=size.tiny)
