Multiple Anchored VWAP [Morty]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © M0rty

//@version=4
study("Multiple Anchored VWAP [Morty]", overlay=true, resolution="", resolution_gaps=true)


// ---------------------------------------------------------------------------
// inputs
show_label = input(true, "Show labels")

show1 = input(true, "aVWAP №1", inline="aVWAP #1")
src1 = input(hlc3, "", type=input.source, inline="aVWAP #1")
color1 = input(#f6c309, "", inline="aVWAP #1")
date1 = input(title="Date", type=input.time, defval=timestamp("2021-07-21T00:00+00:00"), inline="aVWAP #1")

show2 = input(true, "aVWAP №2", inline="aVWAP #2")
src2 = input(hlc3, "", type=input.source, inline="aVWAP #2")
color2 = input(#fb9800, "", inline="aVWAP #2")
date2 = input(title="Date", type=input.time, defval=timestamp("2021-08-05T00:00+00:00"), inline="aVWAP #2")

show3 = input(false, "aVWAP №3", inline="aVWAP #3")
src3 = input(hlc3, "", type=input.source, inline="aVWAP #3")
color3 = input(#fb6500, "", inline="aVWAP #3")
date3 = input(title="Date", type=input.time, defval=timestamp("2021-09-07T00:00+00:00"), inline="aVWAP #3")

show4 = input(false, "aVWAP №4", inline="aVWAP #4")
src4 = input(hlc3, "", type=input.source, inline="aVWAP #4")
color4 = input(#f60c0c, "", inline="aVWAP #4")
date4 = input(title="Date", type=input.time, defval=timestamp("2021-05-19T00:00+00:00"), inline="aVWAP #4")

// ---------------------------------------------------------------------------
// function to calculate Anchored VWAP
f_avwap(src, date) =>
    start = time >= date and time[1] < date
    sumSrc = src * volume
    sumVol = volume
    sumSrc := start ? sumSrc : sumSrc + sumSrc[1]
    sumVol := start ? sumVol : sumVol + sumVol[1]
    sumSrc / sumVol

// ---------------------------------------------------------------------------
// plot anchored vwap
plot(show1 ? f_avwap(src1, date1) : na, color=color1, title="aVWAP №1")
plot(show2 ? f_avwap(src2, date2) : na, color=color2, title="aVWAP №2")
plot(show3 ? f_avwap(src3, date3) : na, color=color3, title="aVWAP №3")
plot(show4 ? f_avwap(src4, date4) : na, color=color4, title="aVWAP №4")

// ---------------------------------------------------------------------------
// plot labels
plotshape(show_label and show1 ? time >= date1 and time[1] < date1 : na, text='aVWAP №1',
   textcolor=color.white, color=color.new(color1, 35), location=location.belowbar, style=shape.labelup, size=size.small)
plotshape(show_label and show2 ? time >= date2 and time[1] < date2 : na, text='aVWAP №2',
   textcolor=color.white, color=color.new(color2, 35), location=location.belowbar, style=shape.labelup, size=size.small)
plotshape(show_label and show3 ? time >= date3 and time[1] < date3 : na, text='aVWAP №3',
   textcolor=color.white, color=color.new(color3, 35), location=location.belowbar, style=shape.labelup, size=size.small)
plotshape(show_label and show4 ? time >= date4 and time[1] < date4 : na, text='aVWAP №4',
   textcolor=color.white, color=color.new(color4, 35), location=location.belowbar, style=shape.labelup, size=size.small)
