Volume-Blended Candlesticks [QuantVue]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator('Volume-Blended Candlesticks [QuantVue]', overlay = true)

/////////////
// inputs //
sensitivity = input.string('Normal', 'Sensitivity Level', options = ['Normal', 'More', 'Less'], tooltip = 'More sensitive will require a smaller volume threshold to change candle opacity resulting in more extreme candles. Less sensitive will require a larger volume threshold resulting in more neutral candles.')
maLen       = input.int(50, 'Volume MA length')
colPrev     = input.bool(false, 'Color based on Previous Close', inline = '1')
upCol       = input.color(color.lime, 'Up Bar Color', inline = '2')
downCol     = input.color(color.red, 'Down Bar Color', inline = '2')
showPP      = input.bool(true, 'Show Pocket Pivots', inline = '3')
ppCol       = input.color(color.fuchsia, '', inline = '3')
shapeLoc    = input.string('Below Bar', '', options = ['Below Bar', 'Above Bar','Bottom', 'Top'], inline = '3', tooltip = 'Plots a diamond at selected location signifying a pocket pivot volume signature')
showTable   = input.bool(true, 'Show Volume Info', inline = '4')
yPos        = input.string('Bottom', ' ', options = ['Top', 'Middle', 'Bottom'], inline = '4')
xPos        = input.string('Right', ' ', options = ['Right','Center', 'Left'], inline = '4')
textColor   = input.color(color.rgb(255, 255, 255), 'Text Color', inline = '5')
bgColor     = input.color(color.new(color.rgb(192, 195, 206),70), 'Background Color', inline = '5')

/////////////
// switch //
ppLoc = switch shapeLoc
    'Below Bar' => location.belowbar
    'Above Bar' => location.abovebar
    'Top'       => location.top
    'Bottom'    => location.bottom

senseLevel = switch sensitivity
    'Normal' => 75
    'More' => 25
    'Less' => 125

////////////////
// 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
float volSec    = na
vols            = array.new<float>()
projVol         = float(volume)

///////////////////
// calculations //
avgVol          = ta.sma(volume, maLen)
up              = colPrev ? close >= close[1] : close >= open
down            = colPrev ? close < close[1] : close < open
avgDollarVol    = avgVol * close   
upVol           = close > close[1] ? volume : 0
dnVol           = close < close[1] ? volume : 0
sumUp           = math.sum(upVol, maLen)
sumDn           = math.sum(dnVol, maLen)
upDnVolRatio    = sumUp / sumDn

////////////////////
// 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

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

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

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

/////////////////
// bar colors //
barUpCol       = color.from_gradient(runRate, senseLevel*-1, senseLevel, color.new(upCol,99), upCol)
barDownCol     = color.from_gradient(runRate, senseLevel*-1, senseLevel,  color.new(downCol,99), downCol)

////////////
// plots //
plotshape(showPP and pocketpivot, 'PP', shape.diamond, ppLoc, ppCol, display = display.pane)
barcolor(up ? barUpCol : down ? barDownCol : na)

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

/////////////
// alerts //
alertcondition(runRate > 100, 'Heavy Volume', '{{ticker}} Projected Above Average Volume')