Volume With Color
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Amphibiantrading

//@version=5
indicator(title="Volume With Color", shorttitle="Vol W/ Color", format=format.volume)

//inputs
var g1 = 'Moving Average'
showMA =        input(true, "Show Moving Average", group = g1)
ilength =       input.int(50, "MA Length", minval = 1, maxval = 500, step = 1, inline = '1', group = g1)
macolor =       input(color.orange, 'MA Color', inline = '1', group = g1)
tfSwitch =      input.bool(false, 'Auto change MA length for weekly chart', group = g1)
weeklyLen =     input.int(10, 'Weekly MA Length', group = g1)

var g2 = 'Display'
idownday =      input(color.red, "Down Day Color", inline = "1", group = g2)
idday =         input(color.maroon, "Distribution Color", inline = "1", group = g2)
iupday =        input(color.green, "Up Day Color", inline = "2", group = g2)
iaday =         input(color.lime, "Acumulation Day Color", inline= "2", group = g2)
dryUpBool =     input.bool(false, 'Show Volume Dry Ups', inline = '3', group = g2)
dryUp =         input(color.orange, ' ', inline = '3',  group = g2)
plotstyle =     input.string('Columns', "Plot Style", options = ['Columns', 'Histogram', 'Line'])

var g3 = 'Info'
showpp =        input.bool(true, 'Show Pocket Pivots', inline = '1', group = g3)
tenDay =        input.bool(true, '10 Day P.P.', inline = '2', group = g3 )
pp10color =     input(color.yellow, '', inline = '2', group = g3)
fiveDay =       input.bool(true, '5 Day P.P.', inline = '2', group = g3 )
pp5color =      input(color.blue, '', inline = '2', group = g3)
showProjected = input.bool(true, 'Show Projected Volume Bar', inline = '3', group = g3)
projColor =     input.color(color.new(color.aqua,80), ' ', inline = '3', group = g3)
showTable =     input.bool(true, 'Show Volume Info', inline = '4', group = g3)
yPos =          input.string("Top", " ", options = ["Top", "Middle", "Bottom"], inline = '4',  group= g3)
xPos =          input.string("Right", " ", options = ["Right","Center", "Left"], inline = '4',  group = g3)
addInfo =       input.bool(false, 'Show Additional Volume Info', inline = '5', group = g3)
tableSize =     input.string('Normal', 'Text Size', options = ['Tiny', 'Small', 'Normal', 'Large'], inline = '6', group = g3)
textColor =     input.color(color.rgb(0, 0, 0), 'Text Color', inline = '7', group = g3)
bgColor =       input.color(color.new(color.rgb(192, 195, 206),70), 'Background Color', inline = '7', group = g3)

var g4 = 'Highest Volumes'
showSky =       input.bool(true, 'Show Skyscrappers', inline = '1', group = g4 )
showPer =       input.bool(true, 'Percentages', inline = '1', group = g4)
showHVE =       input.bool(false, 'Show Highest Volume Ever', inline = '2', group = g4)
showHV1 =       input.bool(false, 'Show Highest Volume In a Year', inline = '3', group = g4)
labelSize =     input.string('Normal', 'Text Size', options = ['Tiny', 'Small', 'Normal', 'Large'], inline = '4', group = g4)
colorHV =       input.bool(false, 'Text Color Based on Previous Close', inline = '5', group = g4)
showLines =     input.bool(false, 'Show 2x, 3x Volume Lines', inline = '6', group = g4)
lineColor =     input.color(color.black, ' ', inline = '6', group = g4)

sizer = switch tableSize
    'Normal'  => size.normal
    'Tiny'    => size.tiny
    'Small'   => size.small
    'Large'   => size.large

sizerLabel = switch labelSize
    'Normal'  => size.normal
    'Tiny'    => size.tiny
    'Small'   => size.small
    'Large'   => size.large

//define days
down_day =      close < close[1] 
up_day =        close > close[1]
avg_vol =       tfSwitch and timeframe.isweekly ? ta.sma(volume,weeklyLen) : ta.sma(volume,ilength)
d_day =         down_day and volume > avg_vol
a_day =         up_day and volume > avg_vol
highVolYear =   ta.highest(volume,252)
ph =            ta.pivothigh(volume,13,13)
twoX =          avg_vol * 2
threeX =        avg_vol * 3
avgDollarVol =  avg_vol * close   
upVol =         close > close[1] ? volume : 0
dnVol =         close < close[1] ? volume : 0
sumUp =         math.sum(upVol, 50)
sumDn =         math.sum(dnVol, 50)
upDnVolRatio =  sumUp / sumDn
volDry =        (volume / avg_vol * 100) < 60

//variables
var table volInfo = table.new(str.lower(yPos) + '_' + str.lower(xPos), 4, 2, color.new(color.white,100), 
     border_color = color.new(color.white,100), border_width = 2)
var float   h10 = 0
var float   h5 = 0
vols =      array.new<float>()
vols5 =     array.new<float>()
float       volSec = na
projVol  =  float(volume)
var hve =   volume
var label   hveLabel = na
var label   hv1Label = na
var label   phLabel = na
var line    twoXLine = na
var line    threeXLine = na

//select colors and style
colors =    down_day ? idownday : iupday
colors :=   d_day ? idday : a_day ? iaday : colors
colors :=   dryUpBool and volDry ? dryUp : colors

// pocket pivots
for i = 0 to 10
    if close[i] < close[i + 1]
        array.push(vols, volume[i])
        h10 := array.max(vols)

pocketpivot = close > close[1] and volume > h10

for i = 0 to 5
    if close[i] < close[i + 1]
        array.push(vols5, volume[i])
        h5 := array.max(vols5)

pocketpivot5 = close > close[1] and volume >h5

//projected volume 
timePassed =    timenow - time
timeLeft =      time_close - timenow

if timeLeft > 0
    volSec :=   (volume / timePassed)
    projVol :=  (volume + (volSec * timeLeft))

runRate =       (projVol / avg_vol * 100) - 100
volPerChng =    nz((volume / avg_vol * 100) - 100)

//highest volumes
if volume > hve
    hve := volume

if volume == hve and showHVE 
    hveLabel := label.new(bar_index,volume,'HVE ' + str.tostring(volume,format.volume) + '(' + str.tostring(volPerChng, '#') + '%)', 
         style = label.style_none, textcolor = (colorHV ? (close[13] > close[14] ? iaday : idday) : textColor), size = sizerLabel)
else if volume == highVolYear and showHV1
    hv1Label := label.new(bar_index,volume,'HVY ' + str.tostring(volume,format.volume) + '(' + str.tostring(volPerChng, '#') + '%)', 
         style = label.style_none, textcolor = (colorHV ? (close[13] > close[14] ? iaday : idday) : textColor), size = sizerLabel)
else if (showSky and ph)
    phLabel := label.new(bar_index[13], volume[13],(showPer ? str.tostring(volume[13],format.volume) +  '(' + str.tostring(volPerChng[13], '#') + '%)' : 
         str.tostring(volume[13],format.volume)) , style = label.style_none, 
         textcolor = (colorHV ? (close[13] > close[14] ? iaday : idday) : textColor), size = sizerLabel)
    if (volume[13] == hve and showHVE) or (volume[13] == highVolYear and showHV1)
        label.delete(phLabel)

//plots
styleplot = switch plotstyle
    'Histogram' =>  plot.style_histogram
    'Line' =>       plot.style_line
    =>              plot.style_columns

plot(volume, color = colors, style = styleplot, title="Volume")
plot(showMA ? avg_vol : na, color=macolor, title="Volume MA")
plot(showProjected and timeLeft > 0 ? projVol : na, 'Projected Vol.', color = projColor, style = plot.style_columns)
plotshape(showpp and tenDay ? pocketpivot : na, '10 Day Pocket Pivot', shape.diamond, color = pp10color, location = location.bottom, display = display.pane)
plotshape(showpp and fiveDay and not pocketpivot ? pocketpivot5 : na, '5 Day Pocket Pivot', style = shape.diamond, location = location.bottom, color = pp5color, display = display.pane)


if barstate.islast and showTable
    table.cell(volInfo, 0, 0, ' ')
    table.cell(volInfo, 0, 1, 'Volume: ' + str.tostring(volume,format.volume) + ', ' + 
         (runRate > 0 ? '+' + str.tostring(runRate, '#') + '%' : str.tostring(runRate, '#') + '%'), 
         bgcolor = bgColor, text_color = textColor, text_size = sizer)
    if addInfo
        table.cell(volInfo, 0, 0, 'Volume: ' + str.tostring(volume,format.volume) + ', ' + 
         (runRate > 0 ? '+' + str.tostring(runRate, '#') + '%' : str.tostring(runRate, '#') + '%'), 
         bgcolor = bgColor, text_color = textColor, text_size = sizer)
        table.cell(volInfo, 1, 0, 'Avg. Vol: ' + str.tostring(avg_vol, format.volume), 
         bgcolor =bgColor, text_color = textColor, text_size = sizer)
        table.cell(volInfo, 2, 0, 'Avg: $ Vol: ' + str.tostring(avgDollarVol, format.volume), 
         bgcolor =bgColor, text_color = textColor, text_size = sizer)
        table.cell(volInfo, 3, 0, 'U/D Ratio: ' + str.tostring(upDnVolRatio, '#.##'), bgcolor =bgColor, 
             text_color = textColor, text_size = sizer)
        table.clear(volInfo, 0, 1, 3, 1)

if barstate.islast and showLines
    (twoXLine[1]).delete(), (threeXLine[1]).delete()
    twoXLine := line.new(bar_index[1], twoX, bar_index + 1, twoX, color = lineColor, width = 2)
    threeXLine := line.new(bar_index[1], threeX, bar_index + 1, threeX, color = lineColor, width = 2)

//alerts
if pocketpivot
    alert('Pocket Pivot', alert.freq_once_per_bar_close)
else if volume >= twoX
    alert('2x Average Volume', alert.freq_once_per_bar)
else if volume >= threeX
    alert('3x Average Volume', alert.freq_once_per_bar)