Volume Weighted Volitility MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mohitkrishna010
//@version=4

study("Volume Weighted Volitility")
//+++++++++++   Inputs  +++++++++++++++++++++++++++++++++++++++++++++//
src_input = input(hlc3, title="Source", type=input.source)
period    = input(10, title="Volitility Period", minval=1, group="VWV settings")
usevwap   = input(true,group="VWAP(true) or Hamming Volume Weighted Moving Average(False)")
Hlength   = input(200, title="HWVMA", type=input.integer, minval=1, maxval=500, group="MACD settings")
fastperiod = input(12, title="fastperiod MACD", type=input.integer, minval=1, maxval=500, group="MACD settings")
slowperiod = input(26, title="slowperiod MACD", type=input.integer, minval=1, maxval=500, group="MACD settings")
signalperiod = input(9, title="signalperiod MACD", type=input.integer, minval=1, maxval=500, group="MACD settings")

kalman          = input(true,group="Kalman Filter")

klength         = input(14,group="Kalman Filter")
ac              = input(true,title="Lag Reduction",group="Kalman Filter")

f_kalman_filter(_src,length,ac) =>
    AC = ac ? 1 : 0
    out = 0.
    K = 0.
    src = _src + (_src - nz(out[1],_src))
    out := nz(out[1],src) + nz(K[1])*(src - nz(out[1],src)) + AC*(nz(K[1])*(src - sma(src,length)))
    K := abs(src - out)/(abs(src - out) + stdev(src,length)*length)
    
    out

f_HVWMA(src,length) =>
    c=int(na)
    pi = 3.14159265359
    c := nz(c[1],1) == length  ? 1 : nz(c[1],1) + 1
    s = 1/length 
    h_ = volume * (0.54 - 0.46 * cos((2*pi*c)/length ))
    
    hwma = sum(h_*src,length)/sum(h_,length)
    
src = src_input
totalVolume = sum(volume, period)
vwap_ = usevwap?vwap:f_HVWMA(src,Hlength)
vwv_=sqrt(volume*pow(src-vwap_,2)/totalVolume)
vwv = kalman?f_kalman_filter(vwv_,klength,ac):vwv_

fastMA = ema(vwv, fastperiod)
slowMA = ema(vwv, slowperiod)

macd = fastMA - slowMA
signal = ema(macd,signalperiod)
hist = macd - signal

plot(macd,   color=color.blue, linewidth=2)
plot(signal, color=color.red, linewidth=2)
plot(hist,   color=hist>hist[1]?#CA64EA:#7719AA, linewidth=4, style=plot.style_histogram)
plot(0,      color=color.black)