Volume Spread Analysis Candle Patterns
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © deepu0010
//@version=4
study(title="Volume Spread Analysis Candle Patterns", shorttitle="VSA Candle Patterns", overlay=true)

// Define the inputs
barColorUp = input(color.green, title="Up Bar Color")
barColorDown = input(color.red, title="Down Bar Color")
barColorNeutral = input(color.gray, title="Neutral Bar Color")
barWidth = input(2, title="Bar Width")
ultraHighVolumeThreshold = input(500000, title="Ultra High Volume Threshold")

// Calculate the spread
spread = high - low

// Determine the bar color based on spread
barColor = spread > spread[1] ? barColorUp : (spread < spread[1] ? barColorDown : barColorNeutral)

// Calculate the buying and selling volume
buyVolume = barColor == barColorUp ? volume : 0
sellVolume = barColor == barColorDown ? volume : 0

// Calculate the cumulative buying and selling volume
cumulativeBuyVolume = cum(buyVolume)
cumulativeSellVolume = cum(sellVolume)

// Determine VSA candle patterns
noDemand = close < open and volume < volume[1]
noSupply = close > open and volume < volume[1]
hiddenBuying = close > open and volume > volume[1] and close == high
hiddenSelling = close < open and volume > volume[1] and close == low
upThrust = high > high[1] and close < low[1] and close < open[1] and close < (low[1] + high[1]) / 2
spring = low < low[1] and close > low[1] and close > open[1] and close > (low[1] + high[1]) / 2
ultraHighVolume = volume > ultraHighVolumeThreshold

// Plotting the bars and VSA candle patterns
plot(spread, title="Spread", color=barColor, linewidth=barWidth)

// Plotting VSA candle patterns on the chart
plotshape(noDemand, title="No Demand", style=shape.labelup, location=location.abovebar, color=color.blue, text="ND")
plotshape(noSupply, title="No Supply", style=shape.labeldown, location=location.belowbar, color=color.orange, text="NS")
plotshape(hiddenBuying, title="Hidden Buying", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(hiddenSelling, title="Hidden Selling", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(upThrust, title="Upthrust", style=shape.triangleup, location=location.belowbar, color=color.purple, size=size.small)
plotshape(spring, title="Spring", style=shape.triangledown, location=location.abovebar, color=color.teal, size=size.small)
plotshape(ultraHighVolume, title="Ultra High Volume", style=shape.diamond, location=location.belowbar, color=color.yellow, size=size.small)
