Buyer to Seller Volume (BSV) Indicator 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Steversteves

//@version=5
indicator("Buyer to Seller Volume (BSV)")

BackgroundData(symbol, timeframe, data) =>
    isLive = barstate.isrealtime
    request.security(symbol, timeframe, data[isLive ? 1 : 0])[isLive ? 0 : 1]

InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
    not na(time(timeframe.period, sessionTimes, sessionTimeZone))


higherTimeFrame = input.timeframe("D", title="Time Frame")
AvgLength   = input.int(14, title="Average Length")

greencandle = (close > open)

redcandle = (open > close)

greencandlevolume = if greencandle
    volume 
    
redcandlevolume = if redcandle 
    volume
    
    
// Vol Over time

BuyAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(greencandlevolume, AvgLength))

SellAverage = BackgroundData(syminfo.tickerid, higherTimeFrame, math.sum(redcandlevolume, AvgLength))


buyvolmax = ta.highest(BuyAverage, AvgLength)
buyvolmin = ta.lowest(BuyAverage, AvgLength)

sellvolmax = ta.highest(SellAverage, AvgLength)
sellvolmin = ta.lowest(SellAverage, AvgLength)

bool buyvolmaxatmax = BuyAverage >= buyvolmax 
bool buyvolminatmin = BuyAverage <= buyvolmin 
bool sellvolmax2 = SellAverage >= sellvolmax
bool sellvolmin2 = SellAverage >= sellvolmin 



// Color 

color volBullColorInput = input.color(color.green, "Bull")
color volBearColorInput = input.color(color.red, "Bear")
color neutralinput = input.color(color.gray, "Neutral")
color maxbuyers = input.color(color.fuchsia, "Max Buyers")
color minbuyers = input.color(color.black, "Max Sellers")
color buyerneutral = input.color(color.black, "Buyers Neutral")

color buyavgcolor =  buyvolmaxatmax ? minbuyers : volBullColorInput 
color sellavgcolor = sellvolmax2 ? neutralinput : volBearColorInput

// SMA Vol 

volavg = (BuyAverage + SellAverage) / 2 

sma = ta.sma(volavg, AvgLength) 

plot(sma, "SMA", color=color.yellow)


//plot(br, style=plot.style_histogram, color = pallette) 


plot(BuyAverage, "Buyer Volume", color = volBullColorInput, linewidth=2)
plot(SellAverage, "Seller Volume", color = volBearColorInput, linewidth=2)