# Anchored VWAP Channel Problem

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

LISTING 1
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.

It is unclear what you want to achieve instead.

You can define start date sd yourself.

e.g.

You might use BeginValue instead of SelectedValue

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

AFL Function Reference - BEGINVALUE

### BeginValue

• Value of the array at the begin of the range

SYNTAX BeginValue( ARRAY )

RETURNS NUMBER

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];

etc...

1 Like

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
complicated...
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?

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

PARAMDATE

• 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...

I just noticed that I still have the same problem.
It is strange that the concept of 'visible window'
needs to be explained. Here is a simple exemple
with screenshots. First a screenshot where the
anchored VWAP is INSIDE the visible window
and therefor everything works like expected.

and now I just scroll a bit to the right side that
the starting point with the vertical line is out of view
( meaning outside the visible window ) and again
I make a screen shot but where now the anchored
VWAP has disappeared and so it's just a screenshot
of the chart.

@Achalendra you can solve your issue essentially following the @fxshrat suggestion in the second post using the BeginValue() function:

dn = DateTime();
sd = BeginValue( dn );
Plot( C, Date() + "Close", colorBlack, styleBar );
// BeginValue: if no range is marked then the value at the first bar is returned.
// do not draw VWAP in such a case
if (sd != dn[0])
{
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( M, "M" + _PARAM_VALUES(), colorBlue );
Plot( M * ( 1 + Q1 * 0.01 ), "Upper", colorGreen );
Plot( M * ( 1 - Q2 * 0.01 ), "Lower", colorRed );
}

then you must press F12 to set the selected start bar and Ctrl-F12 to deselect it (or use the mouse as per the user guide instructions - already linked above):

Marking range
To show range marker just double click the chart at the beginning of the range and double click again at the end of the range. You can also use F12 key in conjunction with "select" mode (described above). Just select quote and press F12 for begin and SHIFT+F12 for the range end. You can switch off the range marker by pressing CTRL+F12 key or double clicking in the same place twice.

Thank you ! This just worked now indeed.
I was just confused that it was not mentioned
in the description of 'selectedvalue'
https://www.amibroker.com/guide/afl/selectedvalue.html
where is mentioned the vertical line which doesn't depend on the visible window...