Detrended Price Rate of Change
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OmegaTools

//@version=5
indicator("Detrended Price Rate of Change", "DPROC")

lnt = input(21, "Length")
smth = input(3, "Smoothing factor")
heik = input.bool(false, "Candle mode")
heiklnt = input(10, "Candle sensitivity")

mid = ta.sma(close, lnt*3)
roc = (close - close[lnt])
dp = close -  mid

dproc = ta.sma(math.avg(dp, roc), smth)
dpsma = ta.sma(dproc, heiklnt)

[middle, upper, lower] = ta.bb(dproc, 200, 1)
[middle2, upper2, lower2] = ta.bb(dproc, 200, 2)

p1 = plot(heik == false ? dproc : na, "DPROC", color = color.purple)
plotcandle(heik == true ? dpsma : na, dproc, heik == true ? dpsma : na, dproc, "DPROC Candle", dproc > dpsma ? #2962ff : #e91e63)
hline(0, color = color.new(color.gray, 50), linestyle = hline.style_dotted)
p2 = plot(upper, color = color.new(#e91e63, 50))
p3 = plot(upper2, color = color.new(#e91e63, 0))
p4 = plot(lower, color = color.new(#2962ff, 50))
p5 = plot(lower2, color = color.new(#2962ff, 0))
p12 = plot(0, "", color.gray, display = display.none, editable = false)

fill(p1, p2, dproc > upper ? color.new(#e91e63, 90) : na)
fill(p1, p3, dproc > upper2 ? color.new(#e91e63, 90) : na)
fill(p1, p4, dproc < lower ? color.new(#2962ff, 90) : na)
fill(p1, p5, dproc < lower2 ? color.new(#2962ff, 90) : na)
fill(p1, p12, dproc > lower and dproc < upper ? color.new(color.gray, 95) : na)