Rolling EMAVWAP with Standard Deviation Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pmk07
//@version=5

indicator(title='emaVWAP', overlay=true)

// Inputs

// Rolling
log_sv = input(false, title='Log space')
src_in = input (hlc3, title='Source')
src = log_sv ? math.log(src_in) : src_in

rolling_period = input(100, title='Rolling VWAP Period')
rolling_sv = input(true, title='Show Rolling VWAP')

b1_r = input.float(1.0, title='Band 1 multiplier', step=0.1)
b2_r = input.float(2.0, title='Band 2 multiplier', step=0.1)
b3_r = input.float(3.0, title='Band 3 multiplier', step=0.1)
b4_r = input.float(4.0, title='Band 4 multiplier', step=0.1)

b1_r_std_sv = input(false, title='Show Rolling VWAP STDEV Band 1')
b2_r_std_sv = input(false, title='Show Rolling VWAP STDEV Band 2')
b3_r_std_sv = input(false, title='Show Rolling VWAP STDEV Band 3')
b4_r_std_sv = input(false, title='Show Rolling VWAP STDEV Band 4')

// Calcs

xema(src, len) =>
    alpha = 2 / (len + 1)
    sum = 0.0
    sum := alpha * src + (1 - alpha) * nz(sum[1])
    sum

rVWAP(length) =>
    p = xema(src * volume, length)
    vol = xema(volume, length)
    v = p / vol
    sn = xema(volume * (src - nz(v[1])) * (src - v), length)
    std = math.sqrt(sn / vol)
    [v, std]

[vwap_r, std_r] = rVWAP(rolling_period)

b1_r_std_up = vwap_r + b1_r * std_r
b1_r_std_dn = vwap_r - b1_r * std_r
b2_r_std_up = vwap_r + b2_r * std_r
b2_r_std_dn = vwap_r - b2_r * std_r
b3_r_std_up = vwap_r + b3_r * std_r
b3_r_std_dn = vwap_r - b3_r * std_r
b4_r_std_up = vwap_r + b4_r * std_r
b4_r_std_dn = vwap_r - b4_r * std_r

// Plots

//VWAP

plot(rolling_sv == true ? (log_sv ? math.exp(vwap_r) : vwap_r) : na, title='Rolling VWAP', color=color.new(color.teal, 0), linewidth=1)

//BANDs

b1_r_std_up_plot = plot(b1_r_std_sv or b2_r_std_sv ? (log_sv ? math.exp(b1_r_std_up) : b1_r_std_up) : na, title='Rolling VWAP STDEV1', color=na, linewidth=1, editable=false, display=display.none)
b1_r_std_dn_plot = plot(b1_r_std_sv or b2_r_std_sv ? (log_sv ? math.exp(b1_r_std_dn) : b1_r_std_dn) : na, title='Rolling VWAP STDEV1', color=na, linewidth=1, editable=false, display=display.none)
b2_r_std_up_plot = plot(b2_r_std_sv or b3_r_std_sv ? (log_sv ? math.exp(b2_r_std_up) : b2_r_std_up) : na, title='Rolling VWAP STDEV2', color=na, linewidth=1, editable=false, display=display.none)
b2_r_std_dn_plot = plot(b2_r_std_sv or b3_r_std_sv ? (log_sv ? math.exp(b2_r_std_dn) : b2_r_std_dn) : na, title='Rolling VWAP STDEV2', color=na, linewidth=1, editable=false, display=display.none)
b3_r_std_up_plot = plot(b3_r_std_sv or b4_r_std_sv ? (log_sv ? math.exp(b3_r_std_up) : b3_r_std_up) : na, title='Rolling VWAP STDEV3', color=na, linewidth=1, editable=false, display=display.none)
b3_r_std_dn_plot = plot(b3_r_std_sv or b4_r_std_sv ? (log_sv ? math.exp(b3_r_std_dn) : b3_r_std_dn) : na, title='Rolling VWAP STDEV3', color=na, linewidth=1, editable=false, display=display.none)
b4_r_std_up_plot = plot(b4_r_std_sv ? (log_sv ? math.exp(b4_r_std_up) : b4_r_std_up) : na, title='Rolling VWAP STDEV4', color=na, linewidth=1, editable=false, display=display.none)
b4_r_std_dn_plot = plot(b4_r_std_sv ? (log_sv ? math.exp(b4_r_std_dn) : b4_r_std_dn) : na, title='Rolling VWAP STDEV4', color=na, linewidth=1, editable=false, display=display.none)

fill(b1_r_std_up_plot, b1_r_std_dn_plot, title='Rolling VWAP STDEV1', color=color.new(color.teal, 90))
fill(b2_r_std_dn_plot, b1_r_std_dn_plot, title='Rolling VWAP STDEV2', color=color.new(color.teal, 75))
fill(b3_r_std_dn_plot, b2_r_std_dn_plot, title='Rolling VWAP STDEV3', color=color.new(color.teal, 60))
fill(b4_r_std_dn_plot, b3_r_std_dn_plot, title='Rolling VWAP STDEV4', color=color.new(color.teal, 45))
fill(b2_r_std_up_plot, b1_r_std_up_plot, title='Rolling VWAP STDEV2', color=color.new(color.teal, 75))
fill(b3_r_std_up_plot, b2_r_std_up_plot, title='Rolling VWAP STDEV3', color=color.new(color.teal, 60))
fill(b4_r_std_up_plot, b3_r_std_up_plot, title='Rolling VWAP STDEV4', color=color.new(color.teal, 45))
