Anchored VWAP Channel Problem

In Traders July 2010 edition I found this code for the Anchored VWAP Channel
written by Tomasz Janeczko

dn = DateTime(); 
sd = SelectedValue( dn ); 

start = dn == sd; 

mp = (H+L)/2; 

PV = mp * V; 
CV = Cum( V ); 
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 ); 

Q1 = Param(„Percentage Upper”, 1, 0, 10, 0.01 ); 
Q2 = Param(„Percentage Lower”, 1, 0, 10, 0.01 ); 

Plot( C, Date() + „ Close”, colorBlack, styleBar ); 
Plot( M, „M” + _PARAM_VALUES(), colorBlue ); 
Plot( M * ( 1 + Q1 * 0.01 ), „Upper”, colorGreen ); 
Plot( M * ( 1 - Q2 * 0.01 ), „Lower”, colorRed );

As far as I saw this code works only if the SelectedValue is inside the visible window
which limits considerably the range of application of this indicator.
I didn't find the solution to make it work for SelectedValue outside the visible window,
the drawing of the lines stops always at the end of the visible window
and nothing can be seen any more if the SelectedValue is out of view.
Some help would be much appreciated.
Thank you for your attention.

It is unclear what you want to achieve instead.

You can define start date sd yourself.
Instead of SelectedValue(dn) your might use ParamDate or whatever.


You might use BeginValue instead of SelectedValue

dn = DateTime(); 
sd = BeginValue( dn ); 

AFL Function Reference - BEGINVALUE


  • Value of the array at the begin of the range

SYNTAX BeginValue( ARRAY )


FUNCTION This function gives the single value (number) of the ARRAY at the beginning of the selected range. If no range is marked then the value at the first bar is returned.

To select the range you have to double click in the chart at the beginning of the range and then double click in the chart at the end of the range. Then > < markers will appear above date axis.

So if you do not double click on chart then first datetime of array will be used as start date. So I guess that is what you seem to look for.

Or you might use

dn = DateTime(); 
sd = dn[0];


1 Like

Thank you very much for your answer.
What I want to achieve is simply the same result as I get inside the visible window
but by choosing the anchor outside of the visible window, for exemple on an important
low or high, and to see then the channel on all the following windows scrolling
through the chart.
I run mostly intradaycharts , so would need to combine Paramdate and Paramtime I guess,
also need check exactly which date and time I want, enter these data, it gets much more
With BeginValue I get no results at all, neither in the visible window nor outside.

Sorry, but I have no idea what the content in upper quote means (to say).
Choosing outside of visible window? Get outside of window the same result as inside?
What does that mean?
Perhaps you should do a video or picture.

Do you mean that you have multiple charts side by side.
Or do you mean to get results of chart in analysis?

Please read here.

No you don't.
Please read ParamDate documentation. It cearly says that it returns datetime if 3rd argument is 2.


  • 2 - return value is a NUMBER and holds DateTime. (new in 6.20)

What you say is incorrect. Zoom out completely or double-click on chart. The VWAP are possibly located in the lower price range of chart if BeginValue is first bar of array.


Just a quick follow up in regards to ParamDate. Since ParamDate with 3rd argument set to 2 returns just date too you may use DatetimeConvert function to combine date and time to datetime.

Also you should rather use DateTimeDiff to compare datetimes.

dt = DateTime(); 

pdate = ParamDate("Start Date", "2020-05-01", 0);
ptime = ParamTime("Start Time", "00:00");
sd = Lookup(dt, DateTimeConvert(2, pdate, ptime), -1);//BeginValue( dt ); 

start = DateTimeDiff(dt, sd) == 0;

// ... rest of the code here

Thank you very much for this specification.
Meanwhile a problem has appeared which blocks me in my investigations of this indicator.
I have no idea how it happened that now on all my charts and also on new charts a vertical line
has appeared and there seems no way to get rid of it. This line appears on all intradaytimeframes and becomes now the startingpoint for all these DateTime related issues.


@Achalendra, please see this part of the guide.

Scroll down to about half of the document for information on the Marking range.

thank you, I finally managed to get rid of it by simply by closing amibroker without
saving the changes and reopen it, fortunately this vertical line has disapeared.
I will remember this solution if ever it reappears...

this works indeed perfectly.
I had tried to apply datetime directly with 3rd argument to 2, but I'm not sure
how to write the datetime correctly in the 2nd argument, as it is considered a number
but apparently needs to be entered as string in the 2nd argument, but trying with
"08/05/2020 08:00" I got no effect at all.
The strange thing is that with the verical line

sd = BeginValue( dn );

worked well starting from this line, while now with the manual entered vertical line
it gives again no effect...