Hull Volume Waves
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FractalTrade15

//@version=5
indicator("Hull Volume Waves", format = format.volume)
    
    //Pro Advance/Decline Gradient

f_c_gradientAdvDecPro(_source, _center, _steps, _c_bearWeak, _c_bearStrong, _c_bullWeak, _c_bullStrong) =>
    var float _qtyAdvDec = 0.
    var float _maxSteps = math.max(1, _steps)
    bool _xUp = ta.crossover(_source, _center)
    bool _xDn = ta.crossunder(_source, _center)
    float _chg = ta.change(_source)
    bool _up = _chg > 0
    bool _dn = _chg < 0
    bool _srcBull = _source > _center
    bool _srcBear = _source < _center
    _qtyAdvDec := _srcBull ? _xUp ? 1 : _up ? math.min(_maxSteps, _qtyAdvDec + 1) : _dn ? math.max(1, _qtyAdvDec - 1) : _qtyAdvDec : _srcBear ? _xDn ? 1 : _dn ? math.min(_maxSteps, _qtyAdvDec + 1) : _up ? math.max(1, _qtyAdvDec - 1) : _qtyAdvDec : _qtyAdvDec
    var color _return = na
    _return := _srcBull ? color.from_gradient(_qtyAdvDec, 1, _maxSteps, _c_bullWeak, _c_bullStrong) : _srcBear ? color.from_gradient(_qtyAdvDec, 1, _maxSteps, _c_bearWeak, _c_bearStrong) : _return
    _return

hlen = input(10, title = "Hull Length")
hsrc = input(close, title = "Hull Source")
gsteps = input(10, title = "Gradient Steps")
mlb = input(50, title = "Lookback for Highs")
bullc1 = input(color.blue, title = "Strong Bullish Color")
bullc2 = input(color.navy, title = "Weak Bullish Color")
bearc1 = input(color.fuchsia, title = "Strong Bearish Color")
bearc2 = input(color.purple, title = "Weak Bearish Color")
hull = ta.hma(hsrc,hlen)
mhull = hull[0]
shull = hull[2]

//Wave Calulation
var float uvol = 0
if  mhull > shull
    uvol := volume+uvol[1]
else
    uvol := 0

var float dvol = 0    
if  mhull <= shull
    dvol := volume+dvol[1]
else
    dvol := 0

//Highest Bullish & Bearish Waves

float downvol = 0
for i = 1 to mlb by 1
    if dvol[i] > downvol
        downvol := dvol[i]
        downvol

float upvol = 0
for i = 1 to mlb by 1
    if uvol[i] > upvol
        upvol := uvol[i]
        upvol


huv = ta.highest(upvol, mlb)
hdv = ta.highest(downvol,mlb)
bcol = uvol > huv[1]? color.new(bullc1,90) : dvol > hdv[1]? color.new(bearc1, 90) : na 

hvol =  mhull > shull ? uvol : -dvol
gcol =  f_c_gradientAdvDecPro(hvol, 0, gsteps, bearc2,bearc1, bullc2, bullc1)
hvolplus =  mhull > shull ? uvol : dvol
plot(hvolplus, color = gcol, style=plot.style_columns)
bgcolor(bcol)