VWAP BANDS [qrsq]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © qrsq

//@version=5
indicator("VWAP BANDS", overlay=true, timeframe="")

getTheme(theme) =>
    color lineCol = na
    color bgCol = na
    if theme == "Light"
        lineCol := color.rgb(255,255,255,45)
        bgCol := color.rgb(255,255,255,85)
    if theme == "Dark"
        lineCol := color.new(#393b44, 30)
        bgCol := color.new(#393b44, 92)
    if theme == "Blue"
        lineCol := color.new(#2962ff,30)
        bgCol := color.new(#2962ff,92)
    if theme == "Red"
        lineCol := color.new(#e84b4c, 30)
        bgCol := color.new(#e84b4c, 92)
    if theme == "Orange"
        lineCol := color.new(#ffb228, 30)
        bgCol := color.new(#ffb228, 92)
    if theme == "Green"
        lineCol := color.new(#01c082, 30)
        bgCol := color.new(#01c082, 92)
    [lineCol, bgCol]

theme = input.string("Light", title="Theme", options=["Light", "Dark", "Blue", "Red", "Orange", "Green"], group="Styling")

color lineCol = na
color bgCol = na
// lineCol = color.rgb(255,255,255,45)
// bgCol = color.rgb(255,255,255,85)
[_lineCol, _bgCol] = getTheme(theme)
lineCol:=_lineCol
bgCol:=_bgCol

BV = high == low ? 0 : volume * (close - low) / (high - low)
SV = high == low ? 0 : volume * (high - close) / (high - low)
src = input(title = "Source", defval = hlc3, group="VWAP Settings")
length = input.int(100, title="Length", group="VWAP Settings")
stdevMultiplier = input.float(1.5, "SD Multiplier 1", group="VWAP Settings")
stdevMultiplier2 = input.float(2, "SD Multiplier 2", group="VWAP Settings")

showDouble = input.bool(true, "Show double bands", group="VWAP Settings")

sumBuy = math.sum(BV, length)
sumSrcBuy = math.sum(src*BV, length)
sumSrcSrcBuy = math.sum(BV*math.pow(src,2), length)
VWAPBuy = sumSrcBuy / sumBuy
varianceBuy = (sumSrcSrcBuy / sumBuy) - math.pow(VWAPBuy, 2)
stDevBuy = math.sqrt(varianceBuy < 0 ? 0 : varianceBuy)

sumSell = math.sum(SV, length)
sumSrcSell = math.sum(src*SV, length)
sumSrcSrcSell = math.sum(SV*math.pow(src,2),length)
VWAPSell = sumSrcSell / sumSell
varianceSell = (sumSrcSrcSell / sumSell) - math.pow(VWAPSell, 2)
stDevSell = math.sqrt(varianceSell < 0 ? 0 : varianceSell)



thresholdDown = VWAPBuy - stDevBuy * stdevMultiplier
thresholdUp = VWAPSell + stDevSell * stdevMultiplier

bottomUpper = plot(VWAPSell - stDevSell*stdevMultiplier, color=lineCol)
bottomLower = plot(thresholdDown, color=lineCol)
fill(bottomUpper, bottomLower, color=bgCol)

topLower = plot(VWAPBuy + stDevBuy*stdevMultiplier, color=lineCol)
topUpper = plot(thresholdUp, color=lineCol)
fill(topUpper, topLower, color=bgCol)

bottomDbl = plot(showDouble?VWAPSell - stDevSell*stdevMultiplier2:na, color=lineCol)
fill(bottomDbl, bottomLower, color=showDouble?bgCol:color.new(color.white, 100))

topDbl = plot(showDouble?VWAPBuy + stDevBuy*stdevMultiplier2:na, color=lineCol)
fill(topDbl, topUpper, color=showDouble?bgCol:color.new(color.white, 100))


