Need help with Vwap bands AFL Coding

Hi all,

I need help coding the following vwap bands coded on a price chart.

Upper band is the highest vwap at close in last X days/weeks (two parameters needed, one to put in X value and another to change from days to weeks)

Middle band is average of last X days vwap at close

Lower band is lowest of last X days vwap

Irrespective of the TF which I am using the bands on, the calculation for the value of vwap should be 3.30 close on 1 min chart.

These bands should be visible as horizontal lines throughout the day.

TIA.

-anayash

What effort have you made so far?

1 Like

Hi Trendsurfer,

Thanks for your response.

I am not much of a coder, so the vwap code is a bit complicated for me. I currently calculate these levels manually on two indices and put them as horizontal lines on the chart. But it's difficult to follow the manual process for 20-25 stocks daily. Hence, needed help coding the same.

-anayash

You need to post your attempted code so others can see that you have at least made an effort and can see what part/s of your code need correcting.

1 Like

@anayash,

Here is hint...
You need following list of functions:

DateNum() - for new day array flag
Ref() - for same one
SumSince() - for VWAP calculation (also see here)
TimeFrameCompress() - since you want end of day calculation for certain period of days
HHV() - for upper band
LLV() - for lower Band
MA() - for middle band
TimeFrameExpand() - expanding to selected interval
Plot() - for output of results on chart

Look up upper functions here
https://www.amibroker.com/guide/a_funref.html

Required time for code completion maximum 15 minutes.... rather 5 minutes as it is simple code where you just have to insert into upper functions.

So report back once you face issues or once you are finished.

2 Likes

Currently I am able to plot previous day vwap values using this.

_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");

Period = ParamList( "Base", "Hourly|Daily|Weekly|Monthly|Lastthursday|Yearly", 1 );

if(Period=="Daily" )
{//OR Interval()==3600
Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
}

if(Period=="Weekly" OR Interval()==24 * 3600 )
{
Bars_so_far_today = 1 + BarsSince(DayOfWeek() < Ref( DayOfWeek(), -1 ));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
Vol = TimeFrameGetPrice("V", inWeekly, 0);
TodayVolume = Sum(Vol,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * Vol, Bars_so_far_today ) / TodayVolume,0);
}

if(Period=="Monthly" )
{
Bars_so_far_today = 1 + BarsSince(Month() != Ref(Month(), -1));
Vol = TimeFrameGetPrice("V", inMonthly, 0);
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayVolume = Sum(Vol,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * Vol, Bars_so_far_today ) / TodayVolume,0);
}

Plot (VWAP, "Vwap",colorBlack | styleThick);

SetBarsRequired( 1500, 0 );

EndOfDay = Day() != Ref(Day(), 1);
DayStart = Day() != Ref(Day(), -1);

D2 = ValueWhen(EndOfDay, VWAP, 1);
D3 = ValueWhen(EndOfDay, VWAP, 2);
D4 = ValueWhen(EndOfDay, VWAP, 3);
Plot(D2, "D2", colorYellow, styleThick);
Plot(D3, "D3", colorRed, styleThick);
Plot(D4, "D4", colorBlack, styleThick);

_SECTION_END();

What I need help with is setting up the parameters as mentioned in my first post and plotting the highest, lowest and the average vwap from the range I select from the parameters.

e.g. Here I want to calculated average of last 4 days vwap, highest of last 4 days vwap and lowest of last 4 days vwap and plot them on the chart.

-anayash

Thanks . Will try this. How to put the no. of periods and type of period(day/week/month) in the Param?

You need to make more of an effort.

Also see this (especially about using Code Tags!)

2 Likes

I have posted the same above. Please have a look.

Please read suggested docs how-to-use-this-site and how-to-ask-a-good-question.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.