2009/12/04

Fast VWAP for AmiBroker

This is fast enough to use on a 5-second chart. No-loop calculation of VWAP and standard deviation, from Head2K at traderslaboratory:

ND = Day() != Ref(Day(), -1);

P = (H + L) / 2;
VWP = P * V;
BI = BarIndex();

BeginBI = ValueWhen(ND, BI);
BeginBI = BeginBI[BarCount -1];

if ( BeginBI < BarCount -1 )
{
Inrange = BI >= BeginBI;
CumV = Cum(V * InRange);
CumVWP = Cum(VWP * InRange);
VWAP = CumVWP / CumV;
S = Cum(Ref(CumV, -1) * V * (P - Ref(VWAP, -1))^2 / CumV);
Variance = S / CumV;
SD = sqrt(Variance);
VWAP = IIf(InRange, VWAP, Null);

Plot(VWAP, "VWAP", colorYellow, styleNoTitle );
Plot(VWAP + SD, "+1SD", colorGreen, styleDashed + styleNoTitle );
Plot(VWAP - SD, "-1SD", colorRed, styleDashed + styleNoTitle );
Plot(VWAP + 2*SD, "+2SD", colorSeaGreen, styleDashed + styleNoTitle );
Plot(VWAP - 2*SD, "-2SD", colorOrange, styleDashed + styleNoTitle );
}

Plot(Close, "C", colorGrey40, styleLine);

2009/12/02

Trading System Development