Kviatek - Multi Day VWAP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kviateq
// @version=5


indicator('Kviatek - Multi Day VWAP', overlay=true, max_boxes_count = 500) 

showDOW = input(true, title="Show day of the week labels?")
tmzn    = input(defval="GMT+7", title = "Timezone") 
source  = input.source(ohlc4, "Source for VWAPs")

showMon = input.bool(true, "Show Monday VWAP?", inline = "Monday")
showTue = input.bool(true, "Show Tuesday VWAP?", inline = "Tueday")
showWed = input.bool(true, "Show Wednesday VWAP?", inline = "Wednesday")
showThu = input.bool(true, "Show Thursdayy VWAP?", inline = "Thursday")
showFri = input.bool(true, "Show Friday VWAP?", inline = "Friday")

var datePrev = 0
var weekPrev = 0
var H = 0.0
var L = 0.0

date = time('D')
datePrev := date
newDay = date!=datePrev

dow = dayofweek(date + 1)
bcolor =
 dow == dayofweek.sunday ? color.gray :
 dow == dayofweek.monday ? color.rgb(12, 187, 236) :
 dow == dayofweek.tuesday ? color.rgb(68, 248, 59) :
 dow == dayofweek.wednesday ? color.rgb(247, 243, 59) :
 dow == dayofweek.thursday ? color.rgb(255, 162, 0) :
 dow == dayofweek.friday ? color.rgb(245, 10, 10) :
 color.silver
Day_switch = timeframe.change("D")

open_bar(t) => na(t[1]) and not na(t) or t[1] < t ? 1 : 0 

mon = time("W", "0400-0400:3", tmzn)
tue = time("W", "0400-0400:4", tmzn)
wed = time("W", "0400-0400:5", tmzn)
thu = time("W", "0400-0400:6", tmzn)
fri = time("W", "0400-0400:7", tmzn)
 
mon_switch = ta.change(open_bar(mon)) > 0
tue_switch = ta.change(open_bar(tue)) > 0
wed_switch = ta.change(open_bar(wed)) > 0
thu_switch = ta.change(open_bar(thu)) > 0
fri_switch = ta.change(open_bar(fri)) > 0

Monday_vwap     = ta.vwap(source, mon_switch)
Monday_vwapH    = ta.vwap(high, mon_switch)
Monday_vwapL    = ta.vwap(low, mon_switch)
Tuesday_vwap    = ta.vwap(source, tue_switch)
Tuesday_vwapH   = ta.vwap(high, tue_switch)
Tuesday_vwapL   = ta.vwap(low, tue_switch)
Wednesday_vwap  = ta.vwap(source, wed_switch)
Wednesday_vwapH = ta.vwap(high, wed_switch)
Wednesday_vwapL = ta.vwap(low, wed_switch)
Thursday_vwap   = ta.vwap(source, thu_switch)
Thursday_vwapH  = ta.vwap(high, thu_switch)
Thursday_vwapL  = ta.vwap(low, thu_switch)
Friday_vwap     = ta.vwap(source, fri_switch)
Friday_vwapH    = ta.vwap(high, fri_switch)
Friday_vwapL    = ta.vwap(low, fri_switch)

Mon = dayofweek == dayofweek.monday //? true : false
Tue = dayofweek == dayofweek.tuesday //? true : false
Wed = dayofweek == dayofweek.wednesday //? true : false
Thu = dayofweek == dayofweek.thursday //? true : false
Fri = dayofweek == dayofweek.friday //? true : false

W_vwap = ta.vwap(source, timeframe.change("W"))
M_vwap = ta.vwap(source, timeframe.change("M"))

plot(W_vwap, "Weekly VWAP", color = #41ecbe, style = plot.style_cross, display = display.pane)
plot(M_vwap, "Monthly VWAP", color = #f48fb1, style = plot.style_cross, display = display.pane)

plot(Mon or Tue and not mon_switch ? Monday_vwap : na, "Monday VWAP", color = color.aqua, style = plot.style_linebr, linewidth = 2, display = display.pane)
plot(Tue or Wed and not tue_switch ? Tuesday_vwap : na, "Tuesday VWAP", color = color.green, style = plot.style_linebr, linewidth = 2, display = display.pane)
plot(Wed or Thu and not wed_switch ? Wednesday_vwap : na, "Wednesday VWAP", color = color.yellow, style = plot.style_linebr, linewidth = 2, display = display.pane)
plot(Thu or Fri and not thu_switch ? Thursday_vwap : na, "Thursday VWAP", color = color.orange, style = plot.style_linebr, linewidth = 2, display = display.pane)
plot(Fri or Mon and not fri_switch ? Friday_vwap : na, "Friday VWAP", color = color.red, style = plot.style_linebr, linewidth = 2, display = display.pane)

plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.monday    : false, offset=0, style=shape.diamond, text="Monday"   , color=color.rgb(12, 187, 236), location = location.bottom, textcolor=color.rgb(12, 187, 236))
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.tuesday   : false, offset=0, style=shape.diamond, text="Tuesday"  , color=color.rgb(70, 204, 61) , location = location.bottom, textcolor=color.rgb(70, 204, 61))
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.wednesday : false, offset=0, style=shape.diamond, text="Wednesday", color=color.rgb(234, 223, 15), location = location.bottom, textcolor=color.rgb(234, 223, 15))
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.thursday  : false, offset=0, style=shape.diamond, text="Thursday" , color=color.rgb(243, 139, 13), location = location.bottom, textcolor=color.rgb(243, 139, 13))
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.friday    : false, offset=0, style=shape.diamond, text="Friday"   , color=color.rgb(241, 69, 12) , location = location.bottom, textcolor=color.rgb(241, 69, 12))
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.saturday  : false, offset=0, style=shape.diamond, text="Saturday" , color=color.gray  , location = location.bottom, textcolor=color.gray)
plotshape(showDOW ? hour == 2 and minute == 0 and dayofweek == dayofweek.sunday    : false, offset=0, style=shape.diamond, text="Sunday"   , color=color.gray  , location = location.bottom, textcolor=color.gray)

bgcolor(Day_switch ? color.new(color.white,85) : na, title= "Start of a new day") 