//@version=5
// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
// Detrended Ehlers Leading Indicator [CC] script may be freely distributed under the MIT license.
indicator('Detrended Ehlers Leading Indicator [CC]', max_bars_back=3000, overlay=false)
f_security(_symbol, _res, _src, _repaint) =>
request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
res = input.timeframe(title="Resolution", defval='')
rep = input.bool(title="Allow Repainting?", defval=false)
bar = input.bool(title="Allow Bar Color Change?", defval=false)
h = f_security(syminfo.tickerid, res, high, rep)
l = f_security(syminfo.tickerid, res, low, rep)
length = input.int(title='Length', defval=14, minval=1)
alpha = length > 2 ? 2.0 / (length + 1) : 0.67
price = (math.max(h, nz(h[1])) + math.min(l, nz(l[1]))) / 2
ma1 = 0.0
ma1 := (alpha * price) + ((1 - alpha) * nz(ma1[1], price))
ma2 = 0.0
ma2 := ((alpha / 2) * price) + ((1 - (alpha / 2)) * nz(ma2[1], price))
dsp = ma1 - ma2
ma3 = 0.0
ma3 := (alpha * dsp) + ((1 - alpha) * nz(ma3[1]))
deli = dsp - ma3
slo = dsp - deli
sig = slo > 0 ? slo > nz(slo[1]) ? 2 : 1 : slo < 0 ? slo < nz(slo[1]) ? -2 : -1 : 0
alertcondition(ta.crossover(sig, 1), "Strong Buy Signal", "Strong Bullish Change Detected")
alertcondition(ta.crossunder(sig, -1), "Strong Sell Signal", "Strong Bearish Change Detected")
alertcondition(ta.crossover(sig, 0), "Buy Signal", "Bullish Change Detected")
alertcondition(ta.crossunder(sig, 0), "Sell Signal", "Bearish Change Detected")
deliColor = sig > 1 ? color.green : sig > 0 ? color.lime : sig < -1 ? color.maroon : sig < 0 ? color.red : color.black
barcolor(bar ? deliColor : na)
hline(0)
plot(dsp, title="DSP", linewidth=2, color=deliColor)
plot(deli, title="DELI", linewidth=1, color=color.white)