VWAP Reset Zones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Demech

//@version=5
// The indicator function initializes the indicator settings.
indicator(title="VWAP Reset Zones", shorttitle="VWAP RZ", overlay = true, timeframe = "",  timeframe_gaps = false)

//VWAP 1
// Input options for the anchor period
var anchor = input.string(defval = "Session", title="Anchor Period",
 options=["None", "Session", "Week", "Month", "Year"], group="VWAP 1 Settings")

// Source is set to the close price of the bar
src = input(title = "Source", defval = close, group="VWAP 1 Settings")

// Source 2 is set to the open price of the bar
src2 = input(title = "Source 2", defval = open, group="VWAP 1 Settings")

// Checkboxes for Band values
Resetzone = input(true, title="Reset Zone", group="VWAP 1 Settings", inline="Reset Zone")
Startzone = input(true, title="Start Zone", group="VWAP 1 Settings", inline="Start Zone")
Band_1 = input(false, title="", group="VWAP 1 Settings", inline="band_1")
Zones_1 = input(true, title = "Zones 1", group="VWAP 1 Settings", inline="Zones_1")
Band_2 = input(false, title="", group="VWAP 1 Settings", inline="band_2")
Zones_2 = input(true, title = "Zones 2", group="VWAP 1 Settings", inline="Zones_2")
Band_3 = input(false, title="", group="VWAP 1 Settings", inline="band_3")
Zones_3 = input(true, title = "Zones 3", group="VWAP 1 Settings", inline="Zones_3")
stdevMult_1 = input(1.618, title="Bands 1", group="VWAP 1 Settings", inline="band_1")
stdevMult_2 = input(2.618, title="Bands 2", group="VWAP 1 Settings", inline="band_2")
stdevMult_3 = input(3.618, title="Bands 3", group="VWAP 1 Settings", inline="band_3")

// Check for availability of volume data
if barstate.islast and ta.cum(volume) == 0
    runtime.error("No volume is provided by the data vendor.")

// Define timeframes for calculating VWAP
isNewPeriod = switch anchor
	"Session"   => timeframe.change("D")
	"Week"      => timeframe.change("W")
	"Month"     => timeframe.change("M")
	"Year"      => timeframe.change("12M")
	=> false

// Initialize VWAP values
float vwapValue = na
float vwapValue2 = na
float upperBandValue1 = na
float lowerBandValue1 = na
float upperBandValue2 = na
float lowerBandValue2 = na
float upperBandValue3 = na
float lowerBandValue3 = na
float upperBandValue1o = na
float lowerBandValue1o = na
float upperBandValue2o = na
float lowerBandValue2o = na
float upperBandValue3o = na
float lowerBandValue3o = na

// Calculate VWAP values
[_vwap, _stdevUpper, _] = ta.vwap(src, isNewPeriod, 1)
[_vwap2, _stdevUpper2, _] = ta.vwap(src2, isNewPeriod, 1)
stdev = _stdevUpper - _vwap
stdevc = _stdevUpper2 - _vwap2
vwapValue := _vwap
vwapValue2 := _vwap2
upperBandValue1 := _vwap + stdev * stdevMult_1
lowerBandValue1 := _vwap - stdev * stdevMult_1
upperBandValue2 := _vwap + stdev * stdevMult_2
lowerBandValue2 := _vwap - stdev * stdevMult_2
upperBandValue3 := _vwap + stdev * stdevMult_3
lowerBandValue3 := _vwap - stdev * stdevMult_3
upperBandValue1o := _vwap2 + stdevc * stdevMult_1
lowerBandValue1o := _vwap2 - stdevc * stdevMult_1
upperBandValue2o := _vwap2 + stdevc * stdevMult_2
lowerBandValue2o := _vwap2 - stdevc * stdevMult_2
upperBandValue3o := _vwap2 + stdevc * stdevMult_3
lowerBandValue3o := _vwap2 - stdevc * stdevMult_3


// Define colors for plot
upper_color = input(color.new (color.rgb(0, 255, 8), 0),"bull")
lower_color = input(color.new (color.rgb(255, 0, 0), 0),"bear")
resetcolor = input(color.new (color.yellow, 0), "Reset VWAP1", group="VWAP 1 Settings", inline="Reset Zone")
startzone = input(color.new (color.blue, 0), "Reset VWAP1", group="VWAP 1 Settings", inline="Start Zone")
zone1lower = input(color.new(color.gray, 50), "VWAP 1 Lower Zone 1", group="VWAP 1 Settings", inline="Zones_1")
zone2lower = input(color.new(color.gray, 50), "VWAP 1 Lower Zone 2", group="VWAP 1 Settings", inline="Zones_2")
zone3lower = input(color.new(color.gray, 50), "VWAP 1 Lower Zone 3", group="VWAP 1 Settings", inline="Zones_3")
zone1upper = input(color.new(color.gray, 50), "VWAP 1 Upper Zone 1", group="VWAP 1 Settings", inline="Zones_1")
zone2upper = input(color.new(color.gray, 50), "VWAP 1 Upper Zone 2", group="VWAP 1 Settings", inline="Zones_2")
zone3upper = input(color.new(color.gray, 50), "VWAP 1 Upper Zone 3", group="VWAP 1 Settings", inline="Zones_3")


// Plot VWAP lines and fill area between them
linecolor = _vwap > _vwap2 ? upper_color : lower_color
w1 = plot(vwapValue, title="VWAP 1 Source 1", color=linecolor)
w2 = plot(vwapValue2, title="VWAP 1 Source 2", color=linecolor)
fillcolor = _vwap > _vwap2 ? upper_color : lower_color
fill(w1, w2, color=fillcolor, title = "Fill VWAP 1")

// Plot Bands
b1 = plot(upperBandValue1, title="Upper Band Close 1 VWAP 1", color=color.new(color.red, 100))
b3 = plot(upperBandValue2, title="Upper Band Close 2 VWAP 1", color=color.new(color.red, 100))
b5 = plot(upperBandValue3, title="Upper Band Close 3 VWAP 1", color=color.new(color.red, 100))
b7 = plot(lowerBandValue1, title="Lower Band Close 1 VWAP 1", color=color.new(color.green, 100))
b9 = plot(lowerBandValue2, title="Lower Band Close 2 VWAP 1", color=color.new(color.green, 100))
b11 = plot(lowerBandValue3, title="Lower Band Close 3 VWAP 1", color=color.new(color.green, 100))

var float lastVWAPValue = na
var float lastVWAPValue2 = na
var float lastUpperBandValue1 = na
var float lastLowerBandValue1 = na
var float lastUpperBandValue2 = na
var float lastLowerBandValue2 = na
var float lastUpperBandValue3 = na
var float lastLowerBandValue3 = na
var float lastUpperBandValue1o = na
var float lastLowerBandValue1o = na
var float lastUpperBandValue2o = na
var float lastLowerBandValue2o = na
var float lastUpperBandValue3o = na
var float lastLowerBandValue3o = na
var float closing_vwapValuee = na
var float closing_vwapValue2e = na

if isNewPeriod
    lastVWAPValue := vwapValue[1]
    lastVWAPValue2 := vwapValue2[1]
    closing_vwapValuee := vwapValue
    closing_vwapValue2e := vwapValue2
    lastUpperBandValue1 := upperBandValue1[1]
    lastLowerBandValue1 := lowerBandValue1[1]
    lastUpperBandValue2 := upperBandValue2[1]
    lastLowerBandValue2 := lowerBandValue2[1]
    lastUpperBandValue3 := upperBandValue3[1]
    lastLowerBandValue3 := lowerBandValue3[1]
    lastUpperBandValue1o := upperBandValue1o[1]
    lastLowerBandValue1o := lowerBandValue1o[1]
    lastUpperBandValue2o := upperBandValue2o[1]
    lastLowerBandValue2o := lowerBandValue2o[1]
    lastUpperBandValue3o := upperBandValue3o[1]
    lastLowerBandValue3o := lowerBandValue3o[1]

// Plot today's closing VWAP values
p1 = plot(lastVWAPValue, color=color.new(color.yellow, 100), title = "last Period close VWAP 1", display = Resetzone ? display.all : display.none)
p2 = plot(lastVWAPValue2, color=color.new(color.yellow, 100), title = "last Period open VWAP 1", display = Resetzone ? display.all : display.none)
fill(p1, p2, color=resetcolor ,title = "last Period VWAP 1", display = Resetzone ? display.all : display.none)  // Fill area between today's closing VWAP lines

p3 = plot(closing_vwapValuee, color=color.new(color.blue, 100), title = "start new Persiod close VWAP 1", display = Startzone ? display.all : display.none)
p4 = plot(closing_vwapValue2e, color=color.new(color.blue, 100), title = "start new Persiod open VWAP 1", display = Startzone ? display.all : display.none)
fill(p3, p4, color=startzone, title = "Fill Start New Periode VWAP 1", display = Startzone ? display.all : display.none)  // Fill area between today's closing VWAP lines

// Plot today's closing VWAP Band Zones values
p5 = plot(lastLowerBandValue1, color=color.new(color.gray, 100), title = "Lower Band Close VWAP 1", display = Zones_1 ? display.all : display.none)
p6 = plot(lastLowerBandValue1o, color=color.new(color.gray, 100), title = "Lower Band Open VWAP 1", display = Zones_1 ? display.all : display.none)
fill(p5, p6, color=zone1lower, display = Zones_1 ? display.all : display.none , title = "Fill Lower Zone 1 VWAP 1")  // Fill area between today's closing VWAP lines

p7 = plot(lastLowerBandValue2, color=color.new(color.gray, 100), title = "Lower Band 2 Close VWAP 1", display = Zones_2 ? display.all : display.none)
p8 = plot(lastLowerBandValue2o, color=color.new(color.gray, 100), title = "Lower Band 2 Open VWAP 1", display = Zones_2 ? display.all : display.none)
fill(p7, p8, color=zone2lower, display = Zones_2 ? display.all : display.none, title = "Fill Lower Zone 2 VWAP 1")  // Fill area between today's closing VWAP lines

p9 = plot(lastLowerBandValue3, color=color.new(color.gray, 100), title = "Lower Band 3 Close VWAP 1", display = Zones_3 ? display.all : display.none)
p10 = plot(lastLowerBandValue3o, color=color.new(color.gray, 100), title = "Lower Band 3 Open VWAP 1", display = Zones_3 ? display.all : display.none)
fill(p9, p10, color=zone3lower, display = Zones_3 ? display.all : display.none, title = "Fill Lower Zone 3 VWAP 1")  // Fill area between today's closing VWAP lines

p11 = plot(lastUpperBandValue1, color=color.new(color.gray, 100), title = "Upper Band Close VWAP 1", display = Zones_1 ? display.all : display.none)
p12 = plot(lastUpperBandValue1o, color=color.new(color.gray, 100), title = "Upper Band Open VWAP 1", display = Zones_1 ? display.all : display.none)
fill(p11, p12, color=zone1upper, display = Zones_1 ? display.all : display.none, title = "Fill Upper Zone 1 VWAP 1")  // Fill area between today's closing VWAP lines

p13 = plot(lastUpperBandValue2, color=color.new(color.gray, 100), title = "Upper Band 2 Close VWAP 1", display = Zones_2 ? display.all : display.none)
p14 = plot(lastUpperBandValue2o, color=color.new(color.gray, 100), title = "Upper Band 2 Open VWAP 1",display = Zones_2 ? display.all : display.none)
fill(p13, p14, color=zone2upper, display = Zones_2 ? display.all : display.none, title = "Fill Upper Zone 2 VWAP 1")  // Fill area between today's closing VWAP lines

p15 = plot(lastUpperBandValue3, color=color.new(color.gray, 100), title = "Upper Band 3 Close VWAP 1", display = Zones_3 ? display.all : display.none)
p16= plot(lastUpperBandValue3o, color=color.new(color.gray, 100), title = "Upper Band 3 Open VWAP 1", display = Zones_3 ? display.all : display.none)
fill(p15, p16, color=zone3upper, display = Zones_3 ? display.all : display.none, title = "Fill Upper Zone 3 VWAP 1")  // Fill area between today's closing VWAP lines

// Plot VWAP Band values
upperBand_1 = plot(upperBandValue1, title="Upper Band VWAP 1 #1", color=color.green,  display = Band_1 ? display.all : display.none)
lowerBand_1 = plot(lowerBandValue1, title="Lower Band VWAP 1 #1", color=color.green,  display = Band_1 ? display.all : display.none)

upperBand_2 = plot(upperBandValue2, title="Upper Band VWAP 1 #2", color=color.olive,  display = Band_2 ? display.all : display.none)
lowerBand_2 = plot(lowerBandValue2, title="Lower Band  VWAP 1#2", color=color.olive,  display = Band_2 ? display.all : display.none)

upperBand_3 = plot(upperBandValue3, title="Upper Band VWAP 1 #3", color=color.teal,  display = Band_3 ? display.all : display.none)
lowerBand_3 = plot(lowerBandValue3, title="Lower Band VWAP 1 #3", color=color.teal,  display = Band_3 ? display.all : display.none)

// Fill Bands
fill(w1, upperBand_1, title="Bands Fill VWAP 1 #1", color= color.new(color.green, 95)    , display = Band_1 ? display.all : display.none)
fill(w1, lowerBand_1, title="Bands Fill VWAP 1 #1", color= color.new(color.green, 95)    , display = Band_1 ? display.all : display.none)
fill(upperBand_1, upperBand_2, title="Bands Fill VWAP 1 #2", color= color.new(color.olive, 95)    , display = Band_2 ? display.all : display.none)
fill(lowerBand_1, lowerBand_2, title="Bands Fill VWAP 1 #2", color= color.new(color.olive, 95)    , display = Band_2 ? display.all : display.none)
fill(upperBand_2, upperBand_3, title="Bands Fill VWAP 1 #3", color= color.new(color.teal, 95)    , display = Band_3 ? display.all : display.none)
fill(lowerBand_2, lowerBand_3, title="Bands Fill VWAP 1 #3", color= color.new(color.teal, 95)    , display = Band_3 ? display.all : display.none)

//VWAP 2
// Input options for the anchor period
var anchor2 = input.string(defval = "Week", title="Anchor Period 2",
 options=["None", "Session", "Week", "Month", "Year"], group="VWAP 2 Settings")

// Source is set to the close price of the bar
srcvwap2 = input(title = "Source", defval = close, group="VWAP 2 Settings")

// Source 2 is set to the open price of the bar
src2vwap2 = input(title = "Source 2", defval = open, group="VWAP 2 Settings")

// Checkboxes for Band values
Resetzonevwap2 = input(true, title="Reset Zone", group="VWAP 2 Settings", inline="Reset Zone")
Startzonevwap2 = input(true, title="Start Zone", group="VWAP 2 Settings", inline="Start Zone")


// Check for availability of volume data
if barstate.islast and ta.cum(volume) == 0
    runtime.error("No volume is provided by the data vendor.")

// Define timeframes for calculating VWAP
isNewPeriodvwap2 = switch anchor2
	"Session"   => timeframe.change("D")
	"Week"      => timeframe.change("W")
	"Month"     => timeframe.change("M")
	"Year"      => timeframe.change("12M")
	=> false

// Initialize VWAP values
float vwapValuevwap2 = na
float vwapValue2vwap2 = na


// Calculate VWAP values
[_vwapvwap2, _stdevUppervwap2, _] = ta.vwap(srcvwap2, isNewPeriodvwap2, 1)
[_vwap2vwap2, _stdevUpper2vwap2, _] = ta.vwap(src2vwap2, isNewPeriodvwap2, 1)
stdevvwap2 = _stdevUppervwap2 - _vwapvwap2
stdevcvwap2 = _stdevUpper2vwap2 - _vwap2vwap2
vwapValuevwap2 := _vwapvwap2
vwapValue2vwap2 := _vwap2vwap2



// Define colors for plot
upper_colorvwap2 = input(color.new (color.rgb(66, 6, 94), 0),"bull VWAP 2")
lower_colorvwap2 = input(color.new (color.rgb(243, 16, 186), 0),"bear VWAP 2")
resetcolorvwap2 = input(color.new (color.rgb(131, 1, 253), 0), "Reset VWAP2", group="VWAP 2 Settings", inline="Reset Zone")
startzonevwap2 = input(color.new (color.rgb(253, 0, 241), 0), "Reset VWAP2", group="VWAP 2 Settings", inline="Start Zone")


// Plot VWAP lines and fill area between them
linecolorvwap2 = _vwapvwap2 > _vwap2vwap2 ? upper_colorvwap2 : lower_colorvwap2
w1vwap2 = plot(vwapValuevwap2, title="VWAP 2 Source 1", color=linecolorvwap2)
w2vwap2 = plot(vwapValue2vwap2, title="VWAP 2 Source 2", color=linecolorvwap2)
fillcolorvwap2 = _vwapvwap2 > _vwap2vwap2 ? upper_colorvwap2 : lower_colorvwap2
fill(w1vwap2, w2vwap2, color=fillcolorvwap2, title = "Fill VWAP 2")


var float lastVWAPValuevwap2 = na
var float lastVWAPValue2vwap2 = na
var float closing_vwapValueevwap2 = na
var float closing_vwapValue2evwap2 = na

if isNewPeriodvwap2
    lastVWAPValuevwap2 := vwapValuevwap2[1]
    lastVWAPValue2vwap2 := vwapValue2vwap2[1]
    closing_vwapValueevwap2 := vwapValuevwap2
    closing_vwapValue2evwap2 := vwapValue2vwap2

// Plot today's closing VWAP values
p1vwap2 = plot(lastVWAPValuevwap2, color=color.new(color.red, 100), title = "last Period close VWAP 2")
p2vwap2 = plot(lastVWAPValue2vwap2, color=color.new(color.red, 100), title = "last Period open VWAP 2")
fill(p1vwap2, p2vwap2, color=resetcolorvwap2, title = "Fill last Period VWAP 2", display = Resetzonevwap2 ? display.all : display.none) 

p3vwap2 = plot(closing_vwapValueevwap2, color=color.new(color.green, 100), title = "start new Persiod close VWAP 2")
p4vwap2 = plot(closing_vwapValue2evwap2, color=color.new(color.green,100), title = "start new Persiod open VWAP 2")
fill(p3vwap2, p4vwap2, color=startzonevwap2, title = "Fill Start New Periode VWAP 2", display = Startzonevwap2 ? display.all : display.none)  