Volume Buy/Sell (by iammaximov)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © iammaximov

//@version=5
indicator("Volume Buy/Sell (by iammaximov)", shorttitle="Volume B/S (by iammaximov)", format=format.volume)

maView = input(false, "MA", inline="ma")
maType = input.string("EMA", title="Type", inline="ma", options=["SMA", "HMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
maLen = input.int(14, "Length", inline="ma")

totalVol = input(true, "View total Volume?")
_vol24 = input(false, "View 24H Volume?", inline="vol24")

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Ma Volume

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        "HMA" => ta.hma(source, length)
        
pMa = plot(maView ? ma(volume, maLen, maType) : na, title="MA", color=color.new(#ffeb3b, 0), linewidth=1, style=plot.style_stepline)
p0 = plot(0, editable=false, display=display.none)
fill(p0, pMa, color=color.new(#ffeb3b, 80))


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//24H Volume

volumetype = input.string("Volume", title="Type", options=["Volume", "Volume * Price"], inline="vol24")

msIn24h = 24 * 60 * 60 * 1000

maxBufferSize = switch
    timeframe.isminutes or timeframe.isseconds => 24 * 60
    timeframe.isdaily => 24 * 60 / 5
    => 24
    
cumVolTF = switch
    timeframe.isminutes or timeframe.isseconds => "1"
    timeframe.isdaily => "5"
    => "60"

cum24hVol(s) =>
    src = s
    
    if bar_index == 0
        // Creating a buffer of 'maxBufferSize+1' for 'src' and 'time' to avoid the 'max_bars_back' error
        src := src[maxBufferSize+1] * time[maxBufferSize+1] * 0
        
    var cumSum = 0.
    var int firstBarTimeIndex = na
    
    if na(firstBarTimeIndex) // 24H have not elapsed yet
        sum = 0.
        for i = 0 to maxBufferSize
            if (time - time[i]) >= msIn24h
                firstBarTimeIndex := bar_index - i + 1
                break
            sum += src[i]
        cumSum := sum
    else
        cumSum += nz(src)
        for i = firstBarTimeIndex to bar_index
            if (time - time[bar_index - i]) < msIn24h
                firstBarTimeIndex := i
                break
            cumSum -= nz(src[bar_index - i])
    cumSum

noVolumeError = "The data vendor doesn't provide volume data for this symbol."
//if syminfo.volumetype == "tick" and syminfo.type == "crypto"
    //runtime.error(noVolumeError)
var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error(noVolumeError)
    
expr = volumetype == "Volume" ? volume : close * volume
vol24h = request.security(syminfo.tickerid, cumVolTF, cum24hVol(expr))
p_volume24 = plot(_vol24 ? vol24h : na, title="24H Volume", style=plot.style_columns, color=close > open ? color.new(#0ecb81, 75) : color.new(#f6465d, 75))


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Buy/Sell volume

totalamp = (high-low)/low*100
a = close > open ? ((open-low)/low*100)+((high-close)/close*100) : ((close-low)/low*100)+((high-open)/open*100)
b = a*1/totalamp/2

volBuy = close > open ? volume*b + (volume-(volume*b*2)) : volume*b
volSell = close < open ? volume*b + (volume-(volume*b*2)) : volume*b

ptotal = plot(totalVol ? volBuy + volSell : na, title="Total Volume", style=plot.style_columns, color=close > open ? color.new(#0ecb81, 50) : color.new(#f6465d, 50))
pbuy = plot(volBuy, title="Buy Volume", style=plot.style_columns, color=color.new(#0ecb81, 0))
psell = plot(volSell * -1, title="Sell Volume", style=plot.style_columns, color=color.new(#f6465d, 0))
hline(0, linestyle=hline.style_dotted, color=color.new(#ffeb3b, 0), editable=false)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Amplitude volume
amVol = input(false, "View %Bar/24H Volume?")
d = input.float(5, title="% Bar volume", step=0.01)
aVol = volume*100/vol24h
aVolPlot = plot(amVol and aVol > d and close > open ? volume : amVol and aVol > d and close < open ? -volume : na, title="% Volume", style=plot.style_columns, color=close > open ? color.new(#ff4cf3, 75) : color.new(#ffb74d, 75))
















