Donchian Anchored Vwap + Handoffs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © aj7919

// Credit to trader dysrupt for their Anchored VWAP Hand-off script for the AVWAP handoff code

//@version=5
indicator("Donchian Anchored Vwap + Handoffs", shorttitle="DAVWAP 1.0", overlay=true)


///////////////////////////////////////////////////////////////////////////////  INPUTS 

length = input.int(20, title = "Donchian Lookback", minval=1)
vws=input.source(high, "VWAP High Source")
vws2 = input.source(low, "VWAP Low Source")

avwaps = input.bool (true, "Display AVWAPS")
donc = input.bool (false, "Display Donchian Highs and Lows")
labels = input.bool (false, "Show High/Low Breaches Price Labels")


// COLOUR INPUTS
bull = input(color.new(color.green, 0), title='Bull Color')
bear = input(color.new(color.red, 0), title='Bear Color')
//basiscol = input(color.new(color.white, 50), title='Avg VWAP')
devcol = input(color.new(color.gray, 50), title='Average Color')
none = color.new(color.black,100)


///////////////////////////////////////////////////////////////////////////////  Determine Donchian Highs and Lows for initial AVWAP High and AVWAP Low 
l = ta.lowest(length)
h = ta.highest(length)
basis = math.avg(h, l)


/////////////////////////////////////////////////////////////////////////////// INITIAL ANCHORED VWAP FROM HIGH 

start = high >= h //ta.cross(high,h)

sumSrc = vws * volume
sumVol = volume
sumSrc := start ? sumSrc : sumSrc + sumSrc[1]
sumVol := start ? sumVol : sumVol + sumVol[1]

out1=sumSrc / sumVol

if start
    labels == true ? label.new(bar_index, na, text=str.tostring(close), yloc=yloc.abovebar) : na

/////////////////////////////////////////////////////////////////////////////// VWAP HIGH HAND OFF 1

start2 = high >= out1 and high < h // start == false ? ta.cross(high,out1) : na

sumSrc2 = vws * volume
sumVol2 = volume
sumSrc2 := start2 ? sumSrc2 : sumSrc2 + sumSrc2[1]
sumVol2 := start2 ? sumVol2 : sumVol2 + sumVol2[1]

out2=sumSrc2 / sumVol2

/////////////////////////////////////////////////////////////////////////////// VWAP HIGH HAND OFF 2

start3 = high >= out2 and high < h // start == false ? ta.cross(high,out2) : na

sumSrc3 = vws * volume
sumVol3 = volume
sumSrc3 := start3 ? sumSrc3 : sumSrc3 + sumSrc3[1]
sumVol3 := start3 ? sumVol3 : sumVol3 + sumVol3[1]

out3=sumSrc3 / sumVol3

// ************** LOWER ANCHORS ******************************

/////////////////////////////////////////////////////////////////////////////// INITIAL ANCHORED VWAP FROM LOW 

startlo = low <= l //ta.cross(low,l) 

sumSrclo = vws2 * volume
sumVollo = volume
sumSrclo := startlo ? sumSrclo : sumSrclo + sumSrclo[1]
sumVollo := startlo ? sumVollo : sumVollo + sumVollo[1]

low1=sumSrclo / sumVollo

if startlo
    labels == true ? label.new(bar_index, na, text=str.tostring(close), yloc=yloc.belowbar, style = label.style_label_up) : na

/////////////////////////////////////////////////////////////////////////////// VWAP LOW HAND OFF 1

startlo2 = low <= low1 and low > l // startlo == false ? ta.cross(low,low1) : na

sumSrclo2 = vws2 * volume
sumVollo2 = volume
sumSrclo2 := startlo2 ? sumSrclo2 : sumSrclo2 + sumSrclo2[1]
sumVollo2 := startlo2 ? sumVollo2 : sumVollo2 + sumVollo2[1]

low2=sumSrclo2 / sumVollo2

/////////////////////////////////////////////////////////////////////////////// VWAP LOW HAND OFF 2

startlo3 = low <= low2 and low > l //startlo == false ? ta.cross(low,low2) : na

sumSrclo3 = vws2 * volume
sumVollo3 = volume
sumSrclo3 := startlo3 ? sumSrclo3 : sumSrclo3 + sumSrclo3[1]
sumVollo3 := startlo3 ? sumVollo3 : sumVollo3 + sumVollo3[1]

low3=sumSrclo3 / sumVollo3


startlo4 = low <= low3 and low > l //startlo == false ? ta.cross(low,low2) : na

sumSrclo4 = vws2 * volume
sumVollo4 = volume
sumSrclo4 := startlo4 ? sumSrclo4 : sumSrclo4 + sumSrclo4[1]
sumVollo4 := startlo4 ? sumVollo4 : sumVollo4 + sumVollo4[1]

low4=sumSrclo4 / sumVollo4


startlo5 = low <= low4 and low > l //startlo == false ? ta.cross(low,low2) : na

sumSrclo5 = vws2 * volume
sumVollo5 = volume
sumSrclo5 := startlo5 ? sumSrclo5 : sumSrclo5 + sumSrclo5[1]
sumVollo5 := startlo5 ? sumVollo5 : sumVollo5 + sumVollo5[1]

low5=sumSrclo5 / sumVollo5


// // /////////////////////////////////////////////////////////////////////////////// CALCULATE VWAP AVERAGES 

avg = math.avg(out1,out2,out3,low1,low2,low3)       // Average of all VWAPS and Hand Offs



// /////////////////////////////////////////////////////////////////////////////// PLOT DONCHIAN HIGHS/LOWS
plot(h, "Low", linewidth=1, color= donc == true ? color.new(color.aqua, 50) : na )
plot(l, "High", linewidth=1, color= donc == true ? color.new(color.aqua, 50) : na )

// /////////////////////////////////////////////////////////////////////////////// AVWAP SENTIMENT COLOR CALCS 

// AVWAPS ONLY 
// UPPER VWAPS
avwapbull1 = close > out1 
avwapcol1 = avwapbull1 == true ? na : bear

avwapbull2 = close > out2 
avwapcol2 = avwapbull2 == true ? na : bear

avwapbull3 = close > out3 
avwapcol3 = avwapbull3 == true ? na : bear


//LOWER VWAPS:
avwapbull6 = close > low1 
avwapcol6 = avwapbull6 == true ? bull : na

avwapbull7 = close > low2 
avwapcol7 = avwapbull7 == true ? bull : na

avwapbull8 = close > low3 
avwapcol8 = avwapbull8 == true ? bull : na


allbasis = plot(avg, "VWAP Avg ALL", linewidth=1,  style=plot.style_cross, color=  devcol)  // AVERAGE OF ALL VWAPS 



// /////////////////////////////////////////////////////////////////////////////// PLOT AVWAPS

plot(out1, "AVWAP High", linewidth=2, style=plot.style_cross,                                             color= avwaps == true ? avwapcol1 : none)
plot(out2, "AVWAP High H/O 1", linewidth=1, style=plot.style_cross,                                       color= avwaps == true ? avwapcol2 : none) 
plot(out3, "AVWAP High H/O 2", linewidth=1, style=plot.style_cross,                                       color= avwaps == true ? avwapcol3 : none) 

plot(low1, "AVWAP Low", linewidth=2, style=plot.style_cross,                                              color= avwaps == true ? avwapcol6 : none)
plot(low2, "AVWAP Low H/O 1", linewidth=1, style=plot.style_cross,                                        color= avwaps == true ? avwapcol7 : none) 
plot(low3, "AVWAP Low H/O 2", linewidth=1, style=plot.style_cross,                                        color= avwaps == true ? avwapcol8 : none) 
