All-In-One Auto Anchored VWAPs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Texmoonbeam

//@version=5
indicator("All-In-One Auto Anchored VWAPs", overlay = true, max_bars_back = 5000, max_labels_count = 500)
//Settings
s = "Settings"
zone = input.string('GMT+1', title='Timezone', options=['GMT-11', 'GMT-10', 'GMT-9', 'GMT-8', 'GMT-7', 'GMT-6', 'GMT-5', 'GMT-4', 'GMT-3', 'GMT-2', 'GMT-1', 'GMT', 'GMT+1', 'GMT+2', 'GMT+3', 'GMT+330', 'GMT+4', 'GMT+430', 'GMT+5', 'GMT+530', 'GMT+6', 'GMT+7', 'GMT+8', 'GMT+9', 'GMT+10', 'GMT+11', 'GMT+12'], group = s)
vwap_source = input.source(close, "VWAP Source", group = s)
std_dev  = input.float(defval = 0.5, minval = 0, maxval = 10, title = "Std Deviation Multiplier", group = s)
width = input.int(1, 'Line Width', minval=0, group = s)
leftbars  = input.int(defval = 30, title = "Left Bars for Pivots", minval = 0, group = s)
//rightbars  = input.int(defval = 10, title = "Right Bars for Pivots", minval = 0, group = s)
OSession = input.session("0800-1800:1234567", "Session Time", group=s)

// Anchors
g = "AVWAPs"
t = "NOTE: Pivots can only be used with High/Low anchors, Sessions can only be used with Open anchors"
show_hist  = input.bool(defval = true, title = "Show Historical Periods?", group = g, tooltip = "Ticked will show all historical anchor changes based on the period, unticked will just show the most recent period. Does not affect pivots or sessions.")
vwap1_reset = input.string(defval = "Monthly", title = "VWAP 1 Period", options = ["Daily", "Weekly", "Monthly", "Quarterly", "Yearly","All Time", "Pivot", "Session"], group = g, inline = "1", tooltip = t)
vwap_col1 = input.color(defval=color.new(color.maroon,25), title='Colour', inline = "1", group = g)
show_vwap1_Open  = input.bool(defval = true, title = "Open Anchor       ", group = g, inline="1a")
show_vwap1_Open_sd  = input.bool(defval = true, title = "With Std Deviation Band", group = g, inline="1a")
show_vwap1_High  = input.bool(defval = false, title = "High Anchor        ", group = g, inline="1b")
show_vwap1_High_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="1b")
show_vwap1_Low  = input.bool(defval = false, title = "Low Anchor        ", group = g, inline="1c")
show_vwap1_Low_sd  = input.bool(defval = false, title = "With Std Deviation Band\n", group = g, inline="1c")

vwap2_reset = input.string(defval = "Yearly", title = "VWAP 2 Period", options =["Daily", "Weekly", "Monthly", "Quarterly", "Yearly","All Time", "Pivot", "Session"], group = g, inline = "2", tooltip = t)
vwap_col2 = input.color(defval=color.new(color.maroon,25), title='Colour', inline = "2", group = g)
show_vwap2_Open  = input.bool(defval = false, title = "Open Anchor       ", group = g, inline="2a")
show_vwap2_Open_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="2a")
show_vwap2_High  = input.bool(defval = false, title = "High Anchor        ", group = g, inline="2b")
show_vwap2_High_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="2b")
show_vwap2_Low  = input.bool(defval = false, title = "Low Anchor        ", group = g, inline="2c")
show_vwap2_Low_sd  = input.bool(defval = false, title = "With Std Deviation Band\n", group = g, inline="2c")

vwap3_reset = input.string(defval = "All Time", title = "VWAP 3 Period", options =["Daily", "Weekly", "Monthly", "Quarterly", "Yearly","All Time", "Pivot", "Session"], group = g, inline = "3", tooltip = t)
vwap_col3 = input.color(defval=color.new(color.maroon,25), title='Colour', inline = "3", group = g)
show_vwap3_Open  = input.bool(defval = false, title = "Open Anchor       ", group = g, inline="3a")
show_vwap3_Open_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="3a")
show_vwap3_High  = input.bool(defval = false, title = "High Anchor        ", group = g, inline="3b")
show_vwap3_High_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="3b")
show_vwap3_Low  = input.bool(defval = false, title = "Low Anchor        ", group = g, inline="3c")
show_vwap3_Low_sd  = input.bool(defval = false, title = "With Std Deviation Band\n", group = g, inline="3c")

// vwap4_reset = input.string(defval = "Monthly", title = "VWAP 4 Period", options =["Daily", "Weekly", "Monthly", "Quarterly", "Yearly","All Time", "Pivot", "Session"], group = g, inline = "2")
// show_vwap4_Open  = input.bool(defval = false, title = "Open Anchor       ", group = g, inline="2a")
// show_vwap4_Open_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="2a")
// show_vwap4_High  = input.bool(defval = false, title = "High Anchor        ", group = g, inline="2b")
// show_vwap4_High_sd  = input.bool(defval = false, title = "With Std Deviation Band", group = g, inline="2b")
// show_vwap4_Low  = input.bool(defval = false, title = "Low Anchor        ", group = g, inline="2c")
// show_vwap4_Low_sd  = input.bool(defval = false, title = "With Std Deviation Band\n", group = g, inline="2c")

// List Of Anchors
// Daily Open
// Weekly Open
// Monthly Open
// Yearly Open
// Daily High
// Daily Low
// Weekly High
// Weekly Low
// Monthly High
// Monthly Low
// Yearly High
// Yearly Low
// All time high
// All time low
// All time open
// Customn Pivot Highs
// Custom Pivot Lows
// Session Open

//Functions

ResolutionToSec(res)=>
    mins = res == "1" ? 1 :
           res == "3" ? 3 :
           res == "5" ? 5 :
           res == "10" ? 10 :
           res == "15" ? 15 :
           res == "30" ? 30 :
           res == "45" ? 45 :
           res == "60" ? 60 :
           res == "120" ? 120 :
           res == "180" ? 180 :
           res == "240" ? 240 :
           res == "D" or res == "1D" ? 1440 :
           res == "W" or res == "1W" ? 10080 :
           res == "M" or res == "1M" ? 43200 :
           res == "" ? int(str.tonumber(timeframe.period)) : 
           str.substring(timeframe.period, 2, 3) == "S" ? int(str.tonumber(str.substring(timeframe.period, 0, 2)))/60 : int(str.tonumber(res))
    ms = mins * 60 * 1000

GetChartHighest(dataSeries = high) =>
    var chartHighest = dataSeries
    if dataSeries > nz(chartHighest, -1e10)
        chartHighest := dataSeries
    chartHighest

GetAllTimeHigh(dataSeries = high) =>
    highestHTF = request.security(syminfo.tickerid, "D", GetChartHighest(dataSeries)[1], lookahead=barmerge.lookahead_on)
    highestChartTF = GetChartHighest(dataSeries)
    math.max(highestHTF, highestChartTF)

GetChartLowest(dataSeries = low) =>
    var chartLowest = dataSeries
    if dataSeries < nz(chartLowest, 1e10)
        chartLowest := dataSeries
    chartLowest

GetAllTimeLow(dataSeries = low) =>
    lowestHTF = request.security(syminfo.tickerid, "D", 
         GetChartLowest(dataSeries)[1], lookahead=barmerge.lookahead_on)
    lowestChartTF = GetChartLowest(dataSeries)
    math.min(lowestHTF, lowestChartTF)

f_get_started (_session) => na(_session[1]) and _session

//Variables & Change Detection

int sess1 = time(timeframe.period, OSession, zone)

var dh = 0.0
var dl = 0.0
var wh = 0.0
var wl = 0.0
var mh = 0.0
var ml = 0.0
var qh = 0.0
var ql = 0.0
var yh = 0.0
var yl = 0.0

bar = ResolutionToSec(timeframe.period) 

vis_bars = ((chart.right_visible_bar_time - chart.left_visible_bar_time) / bar)

year_change = show_hist ? year(time, zone) != year(time[1], zone) : year(time, zone) != year(time[1], zone) and  time > (timenow - (86400000 * 366))
quarterly_change = show_hist ? ((month(time, zone) == 4 and month(time[1], zone) == 3) or (month(time, zone) == 7 and month(time[1], zone) == 6) or (month(time, zone) == 10 and month(time[1], zone) == 9) or (month(time, zone) == 1 and month(time[1], zone) == 12)) : ((month(time, zone) == 4 and month(time[1], zone) == 3) or (month(time, zone) == 7 and month(time[1], zone) == 6) or (month(time, zone) == 10 and month(time[1], zone) == 9) or (month(time, zone) == 1 and month(time[1], zone) == 12)) and time > (timenow - (86400000 * 94))
month_change = show_hist ? month(time, zone) != month(time[1], zone) : month(time, zone) != month(time[1], zone) and time > (timenow - (86400000 * 32))
week_change = show_hist ? dayofweek(time, zone) == 2 and dayofweek(time[1], zone) == 1 : dayofweek(time, zone) == 2 and dayofweek(time[1], zone) == 1 and time > (timenow - (86400000 * 8))
day_change = show_hist ? dayofweek(time, zone) != dayofweek(time[1], zone) : dayofweek(time, zone) != dayofweek(time[1], zone) and time > (timenow - (86460000 ))

if not(year_change)
    if high > yh
        yh := high
    if low < yl
        yl := low
else
    yh := high
    yl := low

if not(quarterly_change)
    if high > qh
        qh := high
    if low < ql
        ql := low
else
    qh := high
    ql := low

if not(month_change)
    if high > mh
        mh := high
    if low < ml
        ml := low
else
    mh := high
    ml := low
if not(week_change)
    if high > wh
        wh := high
    if low < wl
        wl := low
else
    wh := high
    wl := low

if not(day_change)
    if high > dh
        dh := high
    if low < dl
        dl := low
else
    dh := high
    dl := low

yh_change = show_hist ? yh > yh[1] or year_change : (yh > yh[1] or year_change) and time > (timenow - (86400000 * 366))
qh_change = show_hist ? qh > qh[1] or quarterly_change : (qh > qh[1] or quarterly_change) and time > (timenow - (86400000 * 94))
mh_change = show_hist ? mh > mh[1] or month_change : (mh > mh[1] or month_change ) and time > (timenow - (86400000 * 32))
wh_change = show_hist ? wh > wh[1] or week_change : (wh > wh[1] or week_change ) and time > (timenow - (86400000 * 8))
dh_change = show_hist ? dh > dh[1] or day_change : (dh > dh[1] or day_change) and time > (timenow - (86460000 ))


yl_change = show_hist ? yl < yl[1] or year_change :(yl < yl[1] or year_change) and time > (timenow - (86400000 * 366))
ql_change = show_hist ? ql < ql[1] or quarterly_change : (ql < ql[1] or quarterly_change) and time > (timenow - (86400000 * 94))
ml_change = show_hist ? ml < ml[1] or month_change : (ml < ml[1] or month_change ) and time > (timenow - (86400000 * 32))
wl_change = show_hist ? wl < wl[1] or week_change : (wl < wl[1] or week_change ) and time > (timenow - (86400000 * 8))
dl_change = show_hist ? dl < dl[1] or day_change : (dl < dl[1] or day_change) and time > (timenow - (86460000 ))

ath = GetAllTimeHigh(high)
ath_change = ath > ath[1]

atl = GetAllTimeLow(high)
atl_change = atl < atl[1]

ph = ta.pivothigh(leftbars, 0)
pl = ta.pivotlow(leftbars, 0)

ph_change = not na(ph)
pl_change = not na(pl)

sess_start = f_get_started(sess1)

vwap1_open_change = vwap1_reset == "Daily" ? day_change : vwap1_reset == "Weekly" ? week_change : vwap1_reset == "Monthly" ? month_change : vwap1_reset == "Quarterly" ? quarterly_change : vwap1_reset == "Yearly" ? year_change : vwap1_reset == "All Time" ? barstate.isfirst : vwap1_reset == "Session" ? sess_start : na
vwap1_high_change = vwap1_reset == "Daily" ? dh_change : vwap1_reset == "Weekly" ? wh_change : vwap1_reset == "Monthly" ? mh_change : vwap1_reset == "Quarterly" ? qh_change : vwap1_reset == "Yearly" ? yh_change : vwap1_reset == "All Time" ? ath_change : vwap1_reset == "Pivot" ? ph_change :  na
vwap1_low_change = vwap1_reset == "Daily" ? dl_change : vwap1_reset == "Weekly" ? wl_change : vwap1_reset == "Monthly" ? ml_change : vwap1_reset == "Quarterly" ? ql_change : vwap1_reset == "Yearly" ? yl_change : vwap1_reset == "All Time" ? atl_change : vwap1_reset == "Pivot" ? pl_change : na

vwap2_open_change = vwap2_reset == "Daily" ? day_change : vwap2_reset == "Weekly" ? week_change : vwap2_reset == "Monthly" ? month_change : vwap2_reset == "Quarterly" ? quarterly_change : vwap2_reset == "Yearly" ? year_change : vwap2_reset == "All Time" ? barstate.isfirst : vwap2_reset == "Session" ? sess_start : na
vwap2_high_change = vwap2_reset == "Daily" ? dh_change : vwap2_reset == "Weekly" ? wh_change : vwap2_reset == "Monthly" ? mh_change : vwap2_reset == "Quarterly" ? qh_change : vwap2_reset == "Yearly" ? yh_change : vwap2_reset == "All Time" ? ath_change : vwap2_reset == "Pivot" ? ph_change :  na
vwap2_low_change = vwap2_reset == "Daily" ? dl_change : vwap2_reset == "Weekly" ? wl_change : vwap2_reset == "Monthly" ? ml_change : vwap2_reset == "Quarterly" ? ql_change : vwap2_reset == "Yearly" ? yl_change : vwap2_reset == "All Time" ? atl_change : vwap2_reset == "Pivot" ? pl_change : na

vwap3_open_change = vwap3_reset == "Daily" ? day_change : vwap3_reset == "Weekly" ? week_change : vwap3_reset == "Monthly" ? month_change : vwap3_reset == "Quarterly" ? quarterly_change : vwap3_reset == "Yearly" ? year_change : vwap3_reset == "All Time" ? barstate.isfirst : vwap3_reset == "Session" ? sess_start : na
vwap3_high_change = vwap3_reset == "Daily" ? dh_change : vwap3_reset == "Weekly" ? wh_change : vwap3_reset == "Monthly" ? mh_change : vwap3_reset == "Quarterly" ? qh_change : vwap3_reset == "Yearly" ? yh_change : vwap3_reset == "All Time" ? ath_change : vwap3_reset == "Pivot" ? ph_change :  na
vwap3_low_change = vwap3_reset == "Daily" ? dl_change : vwap3_reset == "Weekly" ? wl_change : vwap3_reset == "Monthly" ? ml_change : vwap3_reset == "Quarterly" ? ql_change : vwap3_reset == "Yearly" ? yl_change : vwap3_reset == "All Time" ? atl_change : vwap3_reset == "Pivot" ? pl_change : na

//debug
// if wh_change
//     label.new(bar_index, high, text = "h")


// Plot all the vwaps!

[vwap1_open_vwap, upper_vwap1_open_vwap, lower_vwap1_open_vwap] = ta.vwap(show_vwap1_Open ? vwap_source : na, vwap1_open_change, std_dev)
[vwap1_high_vwap, upper_vwap1_high_vwap, lower_vwap1_high_vwap] = ta.vwap(show_vwap1_High ? vwap_source : na, vwap1_high_change, std_dev)
[vwap1_low_vwap, upper_vwap1_low_vwap, lower_vwap1_low_vwap] = ta.vwap(show_vwap1_Low ? vwap_source : na, vwap1_low_change, std_dev)

[vwap2_open_vwap, upper_vwap2_open_vwap, lower_vwap2_open_vwap] = ta.vwap(show_vwap2_Open ? vwap_source : na, vwap2_open_change, std_dev)
[vwap2_high_vwap, upper_vwap2_high_vwap, lower_vwap2_high_vwap] = ta.vwap(show_vwap2_High ? vwap_source : na, vwap2_high_change, std_dev)
[vwap2_low_vwap, upper_vwap2_low_vwap, lower_vwap2_low_vwap] = ta.vwap(show_vwap2_Low ? vwap_source : na, vwap2_low_change, std_dev)

[vwap3_open_vwap, upper_vwap3_open_vwap, lower_vwap3_open_vwap] = ta.vwap(show_vwap3_Open ? vwap_source : na, vwap3_open_change, std_dev)
[vwap3_high_vwap, upper_vwap3_high_vwap, lower_vwap3_high_vwap] = ta.vwap(show_vwap3_High ? vwap_source : na, vwap3_high_change, std_dev)
[vwap3_low_vwap, upper_vwap3_low_vwap, lower_vwap3_low_vwap] = ta.vwap(show_vwap3_Low ? vwap_source : na, vwap3_low_change, std_dev)



plot(show_vwap1_Open_sd ? upper_vwap1_open_vwap : na, "VWAP 1 Open Upper", vwap_col1, width, style = plot.style_line)
plot(vwap1_open_vwap, "VWAP 1 Open", vwap_col1, width, style = plot.style_line)
plot(show_vwap1_Open_sd ? lower_vwap1_open_vwap : na, "VWAP 1 Open Lower", vwap_col1, width, style = plot.style_line)

plot(show_vwap1_High_sd ? upper_vwap1_high_vwap : na, "VWAP 1 High Upper", vwap_col1, width, style = plot.style_line)
plot(vwap1_high_vwap, "VWAP 1 High", vwap_col1, width, style = plot.style_line)
plot(show_vwap1_High_sd ? lower_vwap1_high_vwap : na, "VWAP 1 High Lower", vwap_col1, width, style = plot.style_line)

plot(show_vwap1_Low_sd ? upper_vwap1_low_vwap : na, "VWAP 1 Low Upper", vwap_col1, width, style = plot.style_line)
plot(vwap1_low_vwap, "VWAP 1 Low", vwap_col1, width, style = plot.style_line)
plot(show_vwap1_Low_sd ? lower_vwap1_low_vwap : na, "VWAP 1 Low Lower", vwap_col1, width, style = plot.style_line)

plot(show_vwap2_Open_sd ? upper_vwap2_open_vwap : na, "VWAP 2 Open Upper", vwap_col2, width, style = plot.style_line)
plot(vwap2_open_vwap, "VWAP 2 Open", vwap_col2, width, style = plot.style_line)
plot(show_vwap2_Open_sd ? lower_vwap2_open_vwap : na, "VWAP 2 Open Lower", vwap_col2, width, style = plot.style_line)

plot(show_vwap2_High_sd ? upper_vwap2_high_vwap : na, "VWAP 2 High Upper", vwap_col2, width, style = plot.style_line)
plot(vwap2_high_vwap, "VWAP 2 High", vwap_col2, width, style = plot.style_line)
plot(show_vwap2_High_sd ? lower_vwap2_high_vwap : na, "VWAP 2 High Lower", vwap_col2, width, style = plot.style_line)

plot(show_vwap2_Low_sd ? upper_vwap2_low_vwap : na, "VWAP 2 Low Upper", vwap_col2, width, style = plot.style_line)
plot(vwap2_low_vwap, "VWAP 2 Low", vwap_col2, width, style = plot.style_line)
plot(show_vwap2_Low_sd ? lower_vwap2_low_vwap : na, "VWAP 2 Low Lower", vwap_col2, width, style = plot.style_line)

plot(show_vwap3_Open_sd ? upper_vwap3_open_vwap : na, "VWAP 3 Open Upper", vwap_col3, width, style = plot.style_line)
plot(vwap3_open_vwap, "VWAP 3 Open", vwap_col3, width, style = plot.style_line)
plot(show_vwap3_Open_sd ? lower_vwap3_open_vwap : na, "VWAP 3 Open Lower", vwap_col3, width, style = plot.style_line)

plot(show_vwap3_High_sd ? upper_vwap3_high_vwap : na, "VWAP 3 High Upper", vwap_col3, width, style = plot.style_line)
plot(vwap3_high_vwap, "VWAP 3 High", vwap_col3, width, style = plot.style_line)
plot(show_vwap3_High_sd ? lower_vwap3_high_vwap : na, "VWAP 3 High Lower", vwap_col3, width, style = plot.style_line)

plot(show_vwap3_Low_sd ? upper_vwap3_low_vwap : na, "VWAP 3 Low Upper", vwap_col3, width, style = plot.style_line)
plot(vwap3_low_vwap, "VWAP 3 Low", vwap_col3, width, style = plot.style_line)
plot(show_vwap3_Low_sd ? lower_vwap3_low_vwap : na, "VWAP 3 Low Lower", vwap_col3, width, style = plot.style_line)



// //debug
// if barstate.islast and (timenow - time) >= (timenow - ph_time)

//     label.new(bar_index, high, "x")