'How to plot on chart only since a recent high or low?

I am trying to plot an Anchored VWAP where for example if the period is set to 20 and the highest bar in the past 20 bars is the 10th bar that the VWAP is only displayed from the 10th bar.

The recently added 'VWAP Auto Anchored' indicator by TradingView allows for plotting of a VWAP anchored to the highest bar within a period of bars and it is capable of plotting 10000 bars back. Therefore this should be possible, now. (an error message pops up when trying to view its source code)

//@version=5
indicator(title='Anchored VWAP', overlay=true)

src = input(hlc3)

period = input.int(20,title="Period")

highest = ta.highest(src, period)

//BOOL for if current bar is highest within period
ready = highest == src

//Calculates VWAP
sumSrc = src * volume
sumVol = volume
sumSrc := ready ? sumSrc : sumSrc + sumSrc[1]
sumVol := ready ? sumVol : sumVol + sumVol[1]

//Plot
temp = plot(sumSrc / sumVol)

The plot() function doesn't appear to allow for the deletion/modification of prior plots and the 'show_last=' only allows for an input integer.

Any suggestions would be appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source