Zero Lag Detrended Price Oscillator (ZL DPO)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mdenson
//@version=5
//This code is a modified version of the ZLMA indicator by alexgrover and the Detrended Price Oscillator

indicator('ZL DPO', overlay=false)

//=====================================================================================================================
//ZLMA
//=====================================================================================================================

length  = 50
lag     = 1.4
src     = close
a       = 0.
b       = 0.
a       := nz(lag * src + (1 - lag) * b[length / 2] + a[1], src)
b       := ta.change(a, length) / length

//=====================================================================================================================
//DPO
//=====================================================================================================================

period_     = input.int(21, title="Length", minval=1)
maperiod    = input.int(5, title = "MA DPO Length", minval = 1)
isCentered  = input(true, title="Centered")
barsback    = period_/2 + 1
ma          = ta.change(close, period_)/length
dpo         = isCentered ? close[barsback] - ma : close - ma[barsback]
avg         = ta.sma(dpo, maperiod)

//=====================================================================================================================
//Plots
//=====================================================================================================================

plot(dpo, offset = isCentered ? -barsback : 0, title="Zero Lag DPO", color=color.green,linewidth = 1)
plot(avg, title = "MA of ZL DPO", color = color.red, linewidth =2)
