MAD indicator Enchanced (MADH, inspired by J.Ehlers)
// This source code is not the subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vsov

//@version=5

FIR(_src, _len) =>
    float fir = 0.0
    float coef = 0.0
    for count=1 to _len-1
        c = (1 - math.cos(2*math.pi*count / (_len +1)))
        coef:=coef+c
        fir:=fir+c*_src[count-1]
    if coef != 0
        fir := fir / coef
    fir

indicator("MAD indicator Enchanced (inspired by J.Ehlers)", shorttitle="MADH")


slen = input(8, title="Short length")
dc = input(27, title="Dom Cycle Length")
llen = math.avg(slen, dc)
src_ = input(close, title="Source")
pvtp = input(true, title="PVT?")

src = pvtp ? ta.cum(fixnan(10000 * ta.change(hl2) * (high - low) / volume)) : src_

sfir = FIR(src, slen)
lfir = FIR(src, llen)

MADH = lfir !=0 ? 100*(sfir-lfir)/lfir : 0.0
ROC = (slen /4*math.pi) * ta.change(MADH)


hline(0.0, color=color.gray)
plot(MADH, color=MADH>0 and ROC>0 ?color.green: MADH < 0 and ROC <0 ? color.red : MADH >0 and ROC <0 ? color.maroon : MADH <0 and ROC >0 ? color.teal : color.gray,  style=plot.style_histogram, linewidth=3)
plot(ROC, color=color.orange)
