VolumeFlow
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Options360
// This uses altered pieces of code from:  Options360 "FibVIP" indicator, Tradingview "up / down volume" indicator, Tradingview "Accumulation/Distribution" indicator
//@version=5
indicator("VolumeFlow", shorttitle='VF')
////
normalize(src, min, max) =>
    var historicMin = 10e10
    var historicMax = -10e10
    historicMin := math.min(nz(src, historicMin), historicMin)
    historicMax := math.max(nz(src, historicMax), historicMax)
    min + (max - min) * (src - historicMin) / math.max(historicMax - historicMin, 10e-10)

rescale(src, oldMin, oldMax, newMin, newMax) =>
    newMin + (newMax - newMin) * (src - oldMin) / math.max(oldMax - oldMin, 10e-10)
//
UDVolume() =>
    posVol = 0.0
    negVol = 0.0
    
    switch
        close >  open     => posVol += volume
        close <  open     => negVol -= volume
        close >= close[1] => posVol += volume
        close <  close[1] => negVol -= volume

    [posVol, negVol]
    
lowerTimeframe = switch
    timeframe.isintraday    => "1"
    timeframe.isdaily       => "5"
    => "60"

[upV, downV] = request.security_lower_tf(syminfo.tickerid, '1', UDVolume())

upVolume = array.sum(upV)
downVolume = array.sum(downV)
delta = upVolume + downVolume
//
v = ta.change(close) >= 0 ? volume : -volume
//
var cumVol = 0.
cumVol += nz(volume)
ad = ta.cum(close==high and close==low or high==low ? 0 : (((2*close-low-high)/(high-low))*volume)/1000)
//
delta11 = input(1,"+Volume-")
delta33 = input(2,"VolumeFast")
delta22 = input(3,"VolumeSlow")
delta44 = input(5,"Accumu/Dist")
delta55 = input(1,"Total")
//
delta1 = ta.sma(delta, delta11)
delta2 = ta.sma(ta.rsi(v, delta22), delta22)
delta3 = ta.rsi(v, delta33)
delta4 = ta.sma(ta.rsi(ad, delta44), delta44)
d1 = normalize(delta1, -100, 100)
d2 = rescale(delta2, 0, 100, -100, 100)
d3 = rescale(delta3, 0, 100, -100, 100)
d4 = rescale(delta4, 0, 100, -100, 100)
d5 = ta.sma((d1 + d2 + d3 + d4)/4, delta55)
//
plot(d1, "+Volume-", color = delta > 0 ? color.new(#3cfe12, 0) : color.new(#ff0202, 0), linewidth=2)
plot(d1, "track+Volume-", color = delta > 0 ? color.new(#3cfe12, 0) : color.new(#ff0202, 0), linewidth=2, trackprice=true, offset=-9999, display=display.none)
plot(d3, 'VolumeFast', color.new(#0e2be8, 0), linewidth=2)
plot(d3, 'trackVolumeFast', color.new(#0e2be8, 0), linewidth=2, trackprice=true, offset=-9999, display=display.none)
plot(d2, 'VolumeSlow', color = #ffffff, linewidth=2)
plot(d2, 'trackVolumeSlow', color = #ffffff, linewidth=2, trackprice=true, offset=-9999, display=display.none)
plot(d4, "Accumu/Dist", color=#7b4246, linewidth=2)
plot(d4, "trackAccumu/Dist", color=#7b4246, linewidth=2, trackprice=true, offset=-9999, display=display.none)
plot(d5, "Total", color=#ff7700, linewidth=2)
plot(d5, "trackTotal", color=#ff7700, linewidth=2, trackprice=true, offset=-9999, display=display.none)
//
hline(0, 'zero', color=#ffffff, linestyle=hline.style_dotted, linewidth=1)
h1 = hline(100, 'high', color=#ffffff, linestyle=hline.style_dotted, linewidth=1)
h2 = hline(-100, 'low', color=#ffffff, linestyle=hline.style_dotted, linewidth=1)
h3 = hline(50, 'up', color=#ffffff, linestyle=hline.style_dotted, linewidth=1)
h4 = hline(-50, 'down', color=#ffffff, linestyle=hline.style_dotted, linewidth=1)
fill(h3, h4, color=color.new(#acc2fc, 95), title='Background', display=display.none)
////
