MAD as a hatter (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=4

f_nwma(_src, _period) =>
    fast = _period/2
    lambda = _period/fast
    alpha = lambda * (_period - 1)/(_period - lambda)
    average1 = wma(_src,_period)
    average2 = wma(average1,fast)
    nwma = (1+alpha)*average1 - alpha*average2

study("MAD as a hatter (inspired by J. Ehlers)", shorttitle="MADaH")

src = input(close, title="Source")
len = input(20, title="Averaging length", minval=6, step=1)
so = input(10, title="Smooth signal, 0-off", minval=0, step=1)

fast = f_nwma(src*volume, len)/f_nwma(volume, len)
slow = vwma(src, len)
MAD_=100*(fast - slow) / slow
MAD = so==0?MAD_:f_nwma(MAD_, so)
ROC_ = (len /4*math.pi) *change(MAD)
ROC = so==0?ROC_:f_nwma(ROC_, so)


hline(0.0, color=color.red)
plot(so==0?MAD:f_nwma(MAD, so), color = MAD>0?color.lime:color.maroon, linewidth=2, style=plot.style_histogram, title="MADaH")
plot(ROC, color=color.yellow, title="ROC")

