//@version=4
study(title="Volume Analysis", shorttitle="VA", overlay=false)
// Calculate target period
startTime = 0400
endTime = 0929
startCounter = timestamp(year, month, dayofmonth, startTime / 100, startTime % 100)
endCounter = timestamp(year, month, dayofmonth, endTime / 100, endTime % 100)
targetPeriod = time >= startCounter and time <= endCounter
// Calculate buying and selling volume
buyingVolume = volume * (close - low) / (high - low)
sellingVolume = volume * (high - close) / (high - low)
// Calculate total volume
var float volumeTotal = na
if targetPeriod and not targetPeriod[1]
volumeTotal := volume
else if targetPeriod
volumeTotal := volumeTotal[1] + volume
// Plot volume histogram
plot(volume, style=plot.style_columns, title="Volume Histogram", color=color.blue, linewidth=5)
// Plot buying and selling volume
plot(buyingVolume, style=plot.style_circles, title="Buying Volume", color=color.rgb(85, 206, 87), linewidth=4, join=true)
plot(sellingVolume, style=plot.style_circles, title="Selling Volume", color=color.red, linewidth=4, join=true)
// Calculate average volume
averageVolume = sma(volume, 50)
// Plot average volume line
plot(averageVolume, title="Average Volume", color=color.gray, style=plot.style_line)
// Determine greater volume type
isBuyersVolumeGreater = buyingVolume > sellingVolume
isSellersVolumeGreater = sellingVolume > buyingVolume
// Plot volume type label
labelText = isBuyersVolumeGreater ? "Buyers Volume > Sellers Volume" : isSellersVolumeGreater ? "Sellers Volume > Buyers Volume" : "Buyers Volume = Sellers Volume"
labelColor = isBuyersVolumeGreater ? color.green : isSellersVolumeGreater ? color.red : color.yellow
// Calculate buyer and seller percentage
totalVolume = volume + averageVolume
buyerPercentage = buyingVolume / totalVolume * 100
sellerPercentage = sellingVolume / totalVolume * 100
// Create a new table and store its reference
var table tbl = table.new(position.top_right, 3, 3)
// Constants for W, L, and WR
Buyer = tostring(round(buyerPercentage, 2)) + "%"
Seller = tostring(round(sellerPercentage, 2)) + "%"
Overall = labelText
// Add table rows and values
table.cell(tbl, 0, 0, "Current:", text_color=labelColor)
table.cell(tbl, 0, 1, "Buyer:", text_color=color.green)
table.cell(tbl, 0, 2, "Seller:", text_color=color.red)
// Display constants in the table
table.cell(tbl, 1, 0, tostring(Overall), text_color=labelColor)
table.cell(tbl, 1, 1, tostring(Buyer), text_color=color.green)
table.cell(tbl, 1, 2, tostring(Seller), text_color=color.red)