Volume peak based zones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Demech

//@version=5
indicator("Volume peak based Zones", overlay=true)

// Parameter
length = input.int(20, "Moving average for volume")
threshold = input.float(3, "Z-Score Threshold")
boxlen = input(20, title = "Box length")
srcb = input.source(low, title = "Bull Bottom")
srcs = input.source(high, title = "Sell Top")
srcmb = input.source(close, title = "Body")

boxcolor = input.color(color.new(#000000, 70))

// Volume SMA and standard deviation
meanVolume = ta.sma(volume, length)
stdDevVolume = ta.stdev(volume, length)

// Calculation of the Z-score
zScore = (volume - meanVolume) / stdDevVolume

isVolumeSpike = zScore > threshold


// Determine the direction of the candle and the volume of the candle
isBullish = close < open and isVolumeSpike
isBearish = close > open and isVolumeSpike

// Box drawing
if(isBullish)
    box.new(top_left = chart.point.from_index(bar_index, srcmb), bottom_right = chart.point.from_index(bar_index + int(boxlen), srcb), border_color = boxcolor, bgcolor = boxcolor)

if(isBearish)
    box.new(top_left = chart.point.from_index(bar_index, srcmb), bottom_right = chart.point.from_index(bar_index + int(boxlen), srcs), border_color = boxcolor, bgcolor = boxcolor)

// Alarms
combined_alert = isBearish or isBullish
alertcondition(combined_alert, title='Combined Alert', message='Box detectet')