AFL programming help reqeust

#1
I am facing a strange problem with VWAP and MVWAP plotting.

I find that in 5 minute chart, some consecutive VWAP values are zero. Initially, I tried to code in such a way that if the previous values are zero, code will omit the zero value and take up the last non-zero value.

But in some cases, I am finding that still some cases, VWAP is getting picked up as zero but in other cases, its OK.

Also once the MVWAP value is getting zero, it is staying as zero value throughput. Not able to avoid this by coding. If someone help coding this properly, that will be of great help. I am not able to figure out what can be the correct expression to take care off this type of scenarios.

Code:
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_BEGIN("Volume At Price");
/*
f=14;
VWAP=Sum(V*C,f)/Sum(V,f);

*/
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 091500, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);

VWAP = Sum(C * V, Bars_so_far_today ) / TodayVolume; // Redundant IIF() removed

VWAP = IIf(Nz(VWAP) == 0, IIf(Nz(Ref(VWAP, -1))==0, IIf(Nz(Ref(VWAP, -2))==0, IIf(Nz(Ref(VWAP,-3))==0,Ref(VWAP,-4),Ref(VWAP,-3)),Ref(VWAP,-2)),Ref(VWAP,-1)), VWAP);

Plot (VWAP,"VWAP",colorOrange, styleThick);
Plot(EMA(vwap,50), "MVWAP", colorblue, styleThick);
_SECTION_END();

1589367250564.png
 

Romeo1998

Well-Known Member
#2
Sir @addadutta
this will be helpful, good luck :)

Code:
newday = DateNum()!=Ref(DateNum(),-1);
Bars_so_far_today = BarsSince(newday) +1;
StartBar = ValueWhen(newday, BarIndex());
TodayVolume = Sum(IIf(V!=0,V,1),Bars_so_far_today);

VWAP = Sum(C * IIf(V!=0,V,1), Bars_so_far_today ) / TodayVolume;

Plot (VWAP,"VWAP",colorOrange, styleThick);
Plot(EMA(vwap,50), "MVWAP", colorblue, styleThick);
 

Similar threads

Zerodha – Open Paperless Account

Open online account with Zerodha. Free delivery trading and Max Rs 20 for Intraday, F&O, Currency and Commodity Trading. Intraday High leverage with MIS, CO and BO.

Name:Phone:
Email:City:
State:
Are you a day trader?