script("Gold balance zones", kind="indicator") input minSignal = 2 input length = 7 input mapLength = 7 input deviationMultiplier = 1.28 input minimumAtrWidth = 0.35 input useVolume = 1 input confirmedOnly = 1 input showBoxes = 1 input showVega = 1 input showCompass = 1 input showDashboard = 1 input dotRadius = 2 input backgroundOpacity = 0.12 input supplyColor = "#EF6355" input centerColor = "#42BED0" input demandColor = "#69B85A" input bullColor = "#69D97D" input bearColor = "#EA493A" input returnColor = "#E6A43C" input balanceColor = "#8D929B" confirmBars = math.clamp(math.floor(minSignal), 1, 3) cooldownBars = math.clamp(math.floor(length), 1, 200) window = math.clamp(math.floor(mapLength), 2, 200) typical = (high + low + close) / 3 safeVolume = volume > 0 ? volume : 0 volumeSum = ta.sum(safeVolume, window) weightedPrice = typical * safeVolume weightedSquare = typical * typical * safeVolume priceSum = ta.sum(weightedPrice, window) squareSum = ta.sum(weightedSquare, window) simpleCenter = ta.sma(typical, window) simpleDeviation = ta.stdev(typical, window) atrValue = ta.atr(cooldownBars) // Any missing/zero volume in the window switches the entire window to price-only mode. missingVolume = volume > 0 ? 0 : 1 missingCount = ta.sum(missingVolume, window) volumeReady = useVolume == 1 and volumeSum > 0 and missingCount == 0 candidateCenter = volumeReady ? priceSum / volumeSum : simpleCenter variance = volumeReady ? math.max(0, squareSum / volumeSum - candidateCenter * candidateCenter) : 0 deviation = volumeReady ? math.sqrt(variance) : simpleDeviation halfWidth = math.max(deviation * math.clamp(deviationMultiplier, 0.1, 5), atrValue * math.clamp(minimumAtrWidth, 0.05, 5)) ready = barIndex + 1 >= math.max(window, cooldownBars) and not na(candidateCenter) and not na(halfWidth) confirmed = confirmedOnly == 0 or isClosed upper = nz(upper[1], na) center = nz(center[1], na) lower = nz(lower[1], na) lastBox = nz(lastBox[1], 0) outsideUp = nz(outsideUp[1], 0) outsideDown = nz(outsideDown[1], 0) bias = nz(bias[1], 0) vega = nz(vega[1], 0) newBox = 0 if ready and confirmed { // Confirmation compares closes with the existing box, before installing the next one. outsideUp = not na(upper) and close > upper ? outsideUp + 1 : 0 outsideDown = not na(lower) and close < lower ? outsideDown + 1 : 0 replaceBox = na(center) or (barIndex - lastBox >= cooldownBars and (outsideUp >= confirmBars or outsideDown >= confirmBars)) if replaceBox { center = candidateCenter upper = center + halfWidth lower = center - halfWidth lastBox = barIndex outsideUp = 0 outsideDown = 0 newBox = 1 } direction = close > upper ? 1 : close < lower ? -1 : 0 previousVega = nz(vega[1], 0) vega = direction != 0 ? direction : (previousVega == 1 or previousVega == -1 ? 2 : 0) if direction != 0 { bias = direction } } visibleUpper = showBoxes == 1 ? upper : na visibleCenter = showBoxes == 1 ? center : na visibleLower = showBoxes == 1 ? lower : na plot(visibleUpper, title="Supply", color=supplyColor, width=dotRadius, style="circles") plot(visibleCenter, title="Balance center", color=centerColor, width=dotRadius, style="circles") plot(visibleLower, title="Demand", color=demandColor, width=dotRadius, style="circles") barcolor(showVega == 1 and not na(center) and vega == 1, color=bullColor) barcolor(showVega == 1 and not na(center) and vega == -1, color=bearColor) barcolor(showVega == 1 and not na(center) and vega == 2, color=returnColor) barcolor(showVega == 1 and not na(center) and vega == 0, color=balanceColor) compassUp = showCompass == 1 and bias == 1 compassDown = showCompass == 1 and bias == -1 bgcolor(compassUp, color=bullColor, opacity=backgroundOpacity) bgcolor(compassDown, color=bearColor, opacity=backgroundOpacity) if showDashboard == 1 { table.cell("balance", 0, 0, "Gold balance zones", position="top_right") table.cell("balance", 0, 1, volumeReady ? "Candle-volume weighted" : "Price-only ยท no complete volume", position="top_right") } alertcondition(newBox == 1 and isClosed, title="New balance box", message="New independent balance levels") alertcondition(bias == 1 and nz(bias[1], 0) != 1 and isClosed, title="Bullish bias", message="Close above the balance box") alertcondition(bias == -1 and nz(bias[1], 0) != -1 and isClosed, title="Bearish bias", message="Close below the balance box")