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

//@version=5
indicator("Candle Level of Volume Weighted Average Price", shorttitle = "LVWAP")

// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                   INPUT                                    |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
prcSrc  = input.source(close, "Price Source ", group = "VWAP Settings")
vwapSrc = input.source(hlc3,  "VWAP Source ",  group = "VWAP Settings")


numPerid = input.int(1, "Anchor Period   ", 1,        group = "VWAP Settings", inline = "1")
session  = input.string("Day", "", ["Day", "Week", "Month", "Year", "Length"], 
                                                      group = "VWAP Settings", inline = "1")
spkFltr  = input.bool(true, "Enable Spike Filtering", group = "VWAP Settings")


smthChk = input.bool(false, "Enable Smoothing",                     group = "Level VWAP Smoothing")
smthLen = input.int(14,     "Length", 1,                            group = "Level VWAP Smoothing")
smthTyp = input.string("WMA", "Type", ["RMA", "SMA", "EMA", "WMA"], group = "Level VWAP Smoothing")

// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                CALCULATION                                 |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// Smoothing Function
maType(scr) =>
	switch smthTyp
		"RMA" => ta.rma(scr, smthLen)
		"SMA" => ta.sma(scr, smthLen)
		"EMA" => ta.ema(scr, smthLen)
		=> ta.wma(scr, smthLen)

// Get Session
NewSession = switch session
    "Day"   => str.tostring(numPerid)    + "D"
    "Week"  => str.tostring(numPerid)    + "W"
    "Month" => str.tostring(numPerid)    + "M"
    "Year"  => str.tostring(numPerid*12) + "M"
    => na

// VWAP Calculation
vwap(scr, tf) =>
    var float volPrcSum     = scr * volume                  // Sum Price * Volume
    var float volSum        = volume                        // Sum Volume
    var float avgSamVarSum  = math.pow(scr, 2) * volume     // Sum Average Sample Variances 
    
    if session == "Length"
        volPrcSum    := math.sum(scr * volume,              numPerid)
        volSum       := math.sum(volume,                    numPerid)
        avgSamVarSum := math.sum(math.pow(scr, 2) * volume, numPerid)

    else
        if timeframe.change(tf)
            volPrcSum    := scr * volume
            volSum       := volume
            avgSamVarSum := math.pow(scr, 2) * volume
    
        else if bar_index > 0
            volPrcSum    := scr * volume + volPrcSum[1]
            volSum       := volume + volSum[1]
            avgSamVarSum := math.pow(scr, 2) * volume + avgSamVarSum[1]

    
    // VWAP
    vwap    = volPrcSum / volSum
    
    // VWAP Standard Deviation
    vwapSTD = math.sqrt( (avgSamVarSum/volSum) - math.pow(vwap, 2)) 
    
    [vwap, vwapSTD]
    

// Get VWAP & Std Values
[vwap, vwapSTD] = vwap(vwapSrc, NewSession)

// Candle Level of VWAP 
lVWAP  = (prcSrc - vwap)/vwapSTD
lVWAP := spkFltr and not na(lVWAP) ? (lVWAP > 5 ? 5 : lVWAP < -5 ? -5 : lVWAP) : lVWAP
lVWAP := smthChk ? maType(lVWAP) : lVWAP

// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                  PLOTTING                                  |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// VWAP Line
vwaplin = hline( 0, "VWAP", color = color.new(color.silver, 50), linewidth = 2, linestyle = hline.style_dotted)

// 1 STANDARD DEVIATION LINE
stdUp1  = hline( 1,  "1σ", color = color.new(color.white, 75), linewidth = 1, linestyle = hline.style_solid)
stdDn1  = hline(-1, "-1σ", color = color.new(color.white, 75), linewidth = 1, linestyle = hline.style_solid)
fill(stdUp1, vwaplin, color = color.new(color.white, 90), title = "VWAP  1σ Fill")
fill(stdDn1, vwaplin, color = color.new(color.white, 90), title = "VWAP -1σ Fill")

// 2 STANDARD DEVIATION LINE
stdUp2  = hline( 2,  "2σ", color = color.new(color.orange, 75), linewidth = 4, linestyle = hline.style_solid)
stdDn2  = hline(-2, "-2σ", color = color.new(color.orange, 75), linewidth = 4, linestyle = hline.style_solid)
fill(stdUp1, stdUp2, color = color.new(color.orange, 90), title = " 1σ  2σ Fill")
fill(stdDn1, stdDn2, color = color.new(color.orange, 90), title = "-1σ -2σ Fill")

// 3 STANDARD DEVIATION LINE
stdUp3  = hline( 3,  "3σ", color = color.new(color.green, 75), linewidth = 2, linestyle = hline.style_solid)
stdDn3  = hline(-3, "-3σ", color = color.new(color.green, 75), linewidth = 2, linestyle = hline.style_solid)
fill(stdUp3, stdUp2, color = color.new(color.green, 90), title = " 2σ  3σ Fill")
fill(stdDn3, stdDn2, color = color.new(color.green, 90), title = "-2σ -3σ Fill")

// Candle Level of VWAP Indicator
plot(lVWAP, "Candle Level of VWAP", color = lVWAP > 0 ? color.green : color.red, linewidth = 2)

