i had the idea to just store the start array and draw the VWAP at every refresh. Then you need not to worry about new bars coming in. I wanted to check that out so therefore I updated my button code.
You can also remove a single VWAP when you stand on the curve you want to remove and then press "Remove single VWAP"
// afl Code by E.M.Pottasch 2/2018
Version( 6.21 );
dn = DateTime();
sd = SelectedValue( dn );
start0 = dn == sd;
idx0 = LastValue( ValueWhen( start0, BarIndex() ) );
mp = C;
PV = mp * V;
CV = Cum( V );
VSS = CV - ValueWhen( start0, CV );
denom = IIf( VSS == 0, 1, VSS );
num = Cum( PV ) - ValueWhen( start0, Cum( PV ) );
M = IIf( BarsSince( start0 ), num / denom, mp );
SetChartOptions( 0, chartShowDates );
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
Plot( C, Date() + " No.VWAPs: " + Nz( StaticVarGet( "cnt" ) ) + " Close: ", colorYellow, styleCandle );
Plot( M, "M", colorBlue, styleLine, Null, Null, 0, 0, 2 );
GuiSetFont( "Lucida Console", 10 );
GuiButton( "Store VWAP", 1, 10, 50, 155, 30, 7 );
GuiButton( "Remove single VWAP", 2, 10, 85, 155, 30, 7 );
GuiButton( "Reset all VWAPs", 3, 10, 120, 155, 30, 7 );
GuiSetColors( 1, 3, 2, colorRed, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow );
id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );
if( id == 1 && event == 1 )
{
Say( "Store VWAP" );
StaticVarSet( "cnt", Nz( StaticVarGet( "cnt" ) ) + 1, True );
StaticVarSet( Name() + "M" + StaticVarGet( "cnt" ), start0, True );
}
if( id == 2 && event == 1 )
{
Say( "Remove single VWAPs" );
idx = 0;
cnt = Nz( StaticVarGet( "cnt" ) );
for( i = 1; i <= cnt; i++ )
{
start1 = StaticVarGet( Name() + "M" + i );
idx1 = LastValue( ValueWhen( start1, BarIndex() ) );
if( idx0 == idx1 )
{
idx = i;
StaticVarSet( Name() + "M" + i, 0, True );
}
}
if( idx != 0 )
{
for( i = 1; i <= cnt; i++ )
{
if( i > idx )
{
start1 = StaticVarGet( Name() + "M" + i );
StaticVarSet( Name() + "M" + ( i - 1 ), start1, True );
}
}
StaticVarSet( "cnt", Nz( StaticVarGet( "cnt" ) ) - 1, True );
}
}
if( id == 3 && event == 1 )
{
Say( "Reset all VWAPs" );
StaticVarRemove( Name() + "M*" );
StaticVarRemove( "cnt" );
}
for( i = 1; i <= Nz( StaticVarGet( "cnt" ) ); i++ )
{
start = StaticVarGet( Name() + "M" + i );
VSS = CV - ValueWhen( start, CV );
denom = IIf( VSS == 0, 1, VSS );
num = Cum( PV ) - ValueWhen( start, Cum( PV ) );
M = IIf( BarsSince( start ), num / denom, mp );
Plot( M, "M" + i, colorYellow, styleLine | styleNoRescale, Null, Null, 0, 0, 5 );
}