// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


_SECTION_BEGIN("VWAP MULTI");

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 5, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );
nbars = Param( "nbars", 50, 0, 500, 0.1 );
ww = Param( "width", 5, 0, 10, 1 );
dn = DateTime();
sd = SelectedValue( dn );
start = dn == sd;
//start = DateNum() == 1081031; // Midas example start point
stidx = LastValue( ValueWhen( start, BarIndex() ) );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

if( tr[stidx] )
{
    mp = L;
}
else
    if( pk[stidx] )
    {
        mp = H;
    }
    else
    {
        mp = ( H + L ) / 2;
    }


function NewBarJustArrived() {
    // function by T. Janeczko
    vname = "lbe" + GetChartID();
    prev = Nz( StaticVarGet( vname ) );
    curr = Status( "lastbarend" );
    StaticVarSet( vname, curr );
    return curr != prev;
}

/// VWAP-Multi-Storage AFL
/// by fxshrat
/// it uses CTRL + mouse button click toi set lines (read instructions in chart title
/// code updates lines on new incoming price data.
/// @link http://forum.amibroker.com/t/multiple-anchored-vwap/4739/10
/// modified from GUI version
/// @link http://forum.amibroker.com/t/multiple-anchored-vwap/4739/4

dt = DateTime();
sdt = SelectedValue(dt);
newbar = NewBarJustArrived();

gcmb = GetCursorMouseButtons();
lmb = gcmb == 9;
mmb = gcmb == 12;
CTRL = GetAsyncKeyState(17) < 0;
start = dt == sdt;
num = cumpvol = Cum( mp * V ) - ValueWhen( Ref( start, 1 ), Cum( mp * V ) );
denom = Cum( V ) - ValueWhen( Ref( start, 1 ), Cum( V ) );
vwap = IIf( BarsSince(start) > 0, num / Max(1, denom), mp );

nm = Name();
vwapstr = nm + "VWAP";
cntstr = nm + "VWAP_cnt";
dnstr = nm + "VWAP_DN";

if( CTRL && lmb ) {
	staticget = Nz( StaticVarGet( cntstr ) );
	//Say( "Store V.W.A.P."+(staticget+1) );	
	StaticVarSet( cntstr, staticget+1, True );
	StaticVarSet( vwapstr + staticget, vwap, True );
	StaticVarSet(dnstr + staticget, sdt, True );	
}

if( CTRL && mmb ) {
	//Say( "Reset VWAPs" );
	StaticVarRemove( vwapstr + "*" );
	StaticVarRemove( dnstr + "*" );
	StaticVarRemove( cntstr );
}

// Output
SetChartOptions( 0, chartShowDates | chartWrapTitle );
//Plot( C, Date() + " - Close", colorYellow, styleBar );
Plot( vwap, "VWAP" , colorOrange, styleThick );

for( i = 0; i < Nz(StaticVarGet( cntstr )); i++ ) {
    if( newbar ) {
		//Say( "This is new bar!" );
		staticsd = StaticVarGet( dnstr + i );
		staticstart = dt == staticsd;
		staticnum = CumPV - ValueWhen( staticstart, CumPV );
		staticdenom = CV - ValueWhen( staticstart, CV );
		static_vwap = IIf( BarsSince( staticstart ) > 0, staticnum / Max(1, staticdenom), mp );		
		StaticVarSet( vwapstr + i, static_vwap, True );
	}
   
    Plot( StaticVarGet(vwapstr + i ), "VWAP" + (i+1), colorYellow, styleThick );
}

if( Nz( LastValue(StaticVarGet( vwapstr + "0" ) )) == 0)
Title = EncodeColor(colorRed) + "1. Ctrl + left mouse to create lines\n2. Ctrl + Middle mouse button to delete lines\n{{VALUES}}";