SMA 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("SMA VWAP BANDS", overlay=true, timeframe="")

ma(type, source, length, offset, sigma) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        "VWAP" => ta.vwap(source)
        "ALMA" => ta.alma(source, length, offset, sigma)



len = input.int(200, title="Length", group="Options")
bandMultiplier = input.float(2, title="Bands Multiplier", step=0.1, group="Options")
showBands = input.bool(true, title="Show Bands", group="Options")
showHLArea = input.bool(true, title="Show High/Low Area", group="Options")
showClose = input.bool(true, title="Show Close", group="Options")
showCloudArea = input.bool(true, title="Show Cloud Area", group="Options")
typeMA = input.string(title = "Method 1", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "ALMA"], group="Smoothing")
offset = input.float(0.6, title="Offset (ALMA)")
sigma = input.float(2, title="Sigma (ALMA)")


volSma = ma(typeMA,volume, len, offset, sigma)
srcVolSmaClose = ma(typeMA,close*volume, len, offset, sigma)
srcVolSmaHigh = ma(typeMA,high*volume, len, offset, sigma)
srcVolSmaLow = ma(typeMA,low*volume, len, offset, sigma)


vwapHighClose = srcVolSmaClose / volSma
vwapHighHigh = srcVolSmaHigh / volSma
vwapHighLow = srcVolSmaLow / volSma


srcSrcVolSmaClose = ma(typeMA,volume * math.pow(close, 2), len, offset, sigma)
srcSrcVolSmaHigh = ma(typeMA,volume * math.pow(high, 2), len, offset, sigma)
srcSrcVolSmaLow = ma(typeMA,volume * math.pow(low, 2), len, offset, sigma)


stDevHighClose = math.sqrt(srcSrcVolSmaClose / volSma - math.pow(vwapHighClose, 2))
stDevHighHigh = math.sqrt(srcSrcVolSmaHigh / volSma - math.pow(vwapHighHigh, 2))
stDevHighLow = math.sqrt(srcSrcVolSmaLow / volSma - math.pow(vwapHighLow, 2))


edgeCol = color.new(#8aa29e, 0)
closeCol = color.new(#8aa29e, 0)
cloudCol = color.new(#686963, 95)


upperCloseVal = vwapHighClose + stDevHighClose * bandMultiplier
lowerCloseVal = vwapHighClose - stDevHighClose * bandMultiplier


upperClose = plot(showClose ? showBands ? upperCloseVal : na : na, title="Upper Close", color=closeCol)
lowerClose = plot(showClose ? showBands ? lowerCloseVal : na : na, title="Lower Close", color=closeCol)
midClose = plot(showClose ? vwapHighClose : na, title="Mid Close", color=closeCol)


midLow = plot(showHLArea ? vwapHighLow : na, title="MC Lower Edge", color=edgeCol)
midHigh = plot(showHLArea ? vwapHighHigh : na, title="MC Higher Edge", color=edgeCol)
lowLow = plot(showHLArea ? showBands ? vwapHighLow - stDevHighLow * bandMultiplier : na : na, title="LC Lower Edge", color=edgeCol)
lowHigh = plot(showHLArea ? showBands ? vwapHighHigh - stDevHighHigh * bandMultiplier : na : na, title="LC Upper Edge", color=edgeCol)
highLow = plot(showHLArea ? showBands ? vwapHighLow + stDevHighLow * bandMultiplier : na : na, title="HC Lower Edge", color=edgeCol)
highHigh = plot(showHLArea ? showBands ? vwapHighHigh + stDevHighHigh * bandMultiplier : na : na, title="HC Upper Edge", color=edgeCol)


fill(lowLow, lowHigh, color=showCloudArea?cloudCol:color.new(color.white, 100))
fill(midLow, midHigh, color=showCloudArea?cloudCol:color.new(color.white, 100))
fill(highLow, highHigh, color=showCloudArea?cloudCol:color.new(color.white, 100))

