Auto Anchored Volume Weighted Average Price - Custom AVWAP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AJ7919

//@version=4
study("AAVWAP - AJ", overlay = true, max_bars_back=5000)

tframe = input(defval="1D", title="Timeframe", type=input.resolution, options=["1D", "1W", "1M"])
period = input(defval=90, title="Period", type=input.integer)
display_as_hline = input(defval=false, title="Display as Horizontal Line", type=input.bool)
label_offset = input(defval=50, title="Label Offset", type=input.integer)
show_low_vwap_label = input(defval=true, title="Show Low VWAP label", type=input.bool)
show_high_vwap_label = input(defval=true, title="Show High VWAP label", type=input.bool)
show_vol_vwap_label = input(defval=true, title="Show Vol VWAP label", type=input.bool)

// Var declaration
var int block_size = 1
var currentBarTime = 0


time_0 = security(syminfo.tickerid, tframe, time, barmerge.gaps_off, barmerge.lookahead_on)
time_1 = security(syminfo.tickerid, tframe, time[period], barmerge.gaps_off, barmerge.lookahead_on)

var TPeriod = 0
var chart_timeframe_minutes = 0
var indicator_timeframe_minutes = 0
var real_period = 0

// low vwap vars
var low_vwap_volume_price_summation = 0.0
var low_vwap_volume_summation = 0.0
var low_vwap_counter = 1
var float low_vwap = na
var float lowest_vwap = na
var low_vwap_color = color.fuchsia
var string low_vwap_text = na

// high vwap vars
var high_vwap_volume_price_summation = 0.0
var high_vwap_volume_summation = 0.0
var high_vwap_counter = 1
var float high_vwap = na
var float highest_vwap = na
var high_vwap_color = color.aqua
var string high_vwap_text = na

// volume vwap vars
var vol_vwap_volume_price_summation = 0.0
var vol_vwap_volume_summation = 0.0
var vol_vwap_counter = 1
var float vol_vwap = na
var float volest_vwap = na
var vol_vwap_color = color.orange
var string vol_vwap_text = na

// Labels Initialization
if (tframe == "1D")
    low_vwap_text := tostring(period) + " " + "Day" + " Low"
    high_vwap_text := tostring(period) + " " + "Day" + " High"
    vol_vwap_text := tostring(period) + " " + "Day" + " Vol"
if (tframe == "1W")
    low_vwap_text := tostring(period) + " " + "Week" + " Low"
    high_vwap_text := tostring(period) + " " + "Week" + " High"
    vol_vwap_text := tostring(period) + " " + "Week" + " Vol"
if (tframe == "1M")
    low_vwap_text := tostring(period) + " " + "Month" + " Low"
    high_vwap_text := tostring(period) + " " + "Month" + " High"
    vol_vwap_text := tostring(period) + " " + "Month" + " Vol"


var low_vwap_label = label.new(na, na, low_vwap_text, xloc.bar_index)
var high_vwap_label = label.new(na, na, high_vwap_text, xloc.bar_index)
var vol_vwap_label = label.new(na, na, vol_vwap_text, xloc.bar_index)

var float delta = na


if (not na(time[period]))
    delta := time_0 - time_1
    
    for int i = 1 to 4999
        if (time - time[i] > delta)
            block_size := i
            break
            

if ((timenow - time) < delta)
    if (low_vwap_counter == 1)
        lowest_vwap := low
        low_vwap_volume_price_summation := volume * ohlc4
        low_vwap_volume_summation := volume
        low_vwap := low_vwap_volume_price_summation / low_vwap_volume_summation
        low_vwap_counter := low_vwap_counter + 1
    else 
        if (lowest_vwap > low)
            low_vwap_color := low_vwap_color == color.fuchsia ? color.fuchsia : color.fuchsia
            low_vwap_volume_price_summation := volume * ohlc4
            low_vwap_volume_summation := volume
            low_vwap := low_vwap_volume_price_summation / low_vwap_volume_summation
            lowest_vwap := low
        else
            low_vwap_volume_price_summation := low_vwap_volume_price_summation + volume * ohlc4
            low_vwap_volume_summation := low_vwap_volume_summation + volume
            low_vwap := low_vwap_volume_price_summation / low_vwap_volume_summation
            
        low_vwap_counter := low_vwap_counter + 1
    

if ((timenow - time) < delta)
    if (high_vwap_counter == 1)
        highest_vwap := high
        high_vwap_volume_price_summation := volume * ohlc4
        high_vwap_volume_summation := volume
        high_vwap := high_vwap_volume_price_summation / high_vwap_volume_summation
        high_vwap_counter := high_vwap_counter + 1
    else 
        if (highest_vwap < high)
            high_vwap_color := high_vwap_color == color.aqua ? color.aqua : color.aqua
            high_vwap_volume_price_summation := volume * ohlc4
            high_vwap_volume_summation := volume
            high_vwap := high_vwap_volume_price_summation / high_vwap_volume_summation
            highest_vwap := high
        else
            high_vwap_volume_price_summation := high_vwap_volume_price_summation + volume * ohlc4
            high_vwap_volume_summation := high_vwap_volume_summation + volume
            high_vwap := high_vwap_volume_price_summation / high_vwap_volume_summation
            
        high_vwap_counter := high_vwap_counter + 1
    

if ((timenow - time) < delta)
    if (vol_vwap_counter == 1)
        volest_vwap := volume
        vol_vwap_volume_price_summation := volume * ohlc4
        vol_vwap_volume_summation := volume
        vol_vwap := vol_vwap_volume_price_summation / vol_vwap_volume_summation
        vol_vwap_counter := high_vwap_counter + 1
    else 
        if (volest_vwap < volume)
            vol_vwap_color := vol_vwap_color == color.orange ? color.orange : color.orange
            vol_vwap_volume_price_summation := volume * ohlc4
            vol_vwap_volume_summation := volume
            vol_vwap := vol_vwap_volume_price_summation / vol_vwap_volume_summation
            volest_vwap := volume
        else
            vol_vwap_volume_price_summation := vol_vwap_volume_price_summation + volume * ohlc4
            vol_vwap_volume_summation := vol_vwap_volume_summation + volume
            vol_vwap := vol_vwap_volume_price_summation / vol_vwap_volume_summation
            
        vol_vwap_counter := vol_vwap_counter + 1

    
TPeriod := time - time[1]
TPeriod := na(TPeriod) ? 0 : TPeriod

// if barstate.islast

if show_low_vwap_label
    label.set_xloc(low_vwap_label, time + label_offset*TPeriod, xloc.bar_time)
    label.set_y(low_vwap_label, low_vwap)
    label.set_style(low_vwap_label, label.style_label_left)
    label.set_color(low_vwap_label, low_vwap_color)
else
    label.delete(low_vwap_label)

if show_high_vwap_label
    label.set_xloc(high_vwap_label, time + label_offset*TPeriod, xloc.bar_time)
    label.set_y(high_vwap_label, high_vwap)
    label.set_style(high_vwap_label, label.style_label_left)
    label.set_color(high_vwap_label, high_vwap_color)
else
    label.delete(high_vwap_label)

if show_vol_vwap_label
    label.set_xloc(vol_vwap_label, time + label_offset*TPeriod, xloc.bar_time)
    label.set_y(vol_vwap_label, vol_vwap)
    label.set_style(vol_vwap_label, label.style_label_left)
    label.set_color(vol_vwap_label, vol_vwap_color)
else
    label.delete(vol_vwap_label)
    
plot(high_vwap, title = "Anchor: Highest candle", color = high_vwap_color, trackprice = display_as_hline, style=plot.style_circles)
plot(low_vwap, title = "Anchor: Lowest candle", color = low_vwap_color, trackprice = display_as_hline, style=plot.style_circles)
plot(vol_vwap, title = "Anchor: Highest volume candle", color = vol_vwap_color, trackprice = display_as_hline, style=plot.style_circles)

alertcondition(cross(close, high_vwap) or cross(close, low_vwap) or cross(close, vol_vwap), title="AVWAP Cross", message='Price crossing {{plot("period")}} AVWAP')