Intrabar VWAP not working correctly

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
chisound
Posts: 13
Joined: 29 Jun 2010
Contact:

Intrabar VWAP not working correctly

Postby chisound » 14 Sep 2011

Hi, i am trying to calculate an intrabar VWAP, ie the VWAP of each individual bar which i will compare to the close of the bar to see if it is above or below the close etc.. I am using intrabarpersist for this and have tried it with minute, tick and range charts and every time the returned intrabar VWAP is just the close of the bar which is not right. I do have "Update on every tick" checked under the study properties. The exact code i am using is below. I tried using barstatus to reset the PriceW and ShareW variables at the end of each bar but that did not seem to work either. This should work, anyone have any ideas why this does not work, or any suggestions on how to get it to work?

Many thanks for your help with this,

Code: Select all

vars:
intrabarpersist PriceW(0),
intrabarpersist ShareW(0),
IntrabarVWAP(0);

If time <> time[1] then begin
PriceW = 0;
ShareW = 0;
end;
PriceW = PriceW + (Close * (Upticks + DownTicks));
ShareW = ShareW + (Upticks + Downticks);
If ShareW > 0 then IntrabarVWAP = PriceW / ShareW;

Plot1(IntrabarVWAP,"BarVWAP");

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 14 Sep 2011

Hi, i am trying to calculate an intrabar VWAP, ie the VWAP of each individual bar which i will compare to the close of the bar to see if it is above or below the close etc.. I am using intrabarpersist for this and have tried it with minute, tick and range charts and every time the returned intrabar VWAP is just the close of the bar which is not right. I do have "Update on every tick" checked under the study properties. The exact code i am using is below. I tried using barstatus to reset the PriceW and ShareW variables at the end of each bar but that did not seem to work either. This should work, anyone have any ideas why this does not work, or any suggestions on how to get it to work?

Many thanks for your help with this,

vars:
intrabarpersist PriceW(0),
intrabarpersist ShareW(0),
IntrabarVWAP(0);

If time <> time[1] then begin
PriceW = 0;
ShareW = 0;
end;
PriceW = PriceW + (Close * (Upticks + DownTicks));
ShareW = ShareW + (Upticks + Downticks);
If ShareW > 0 then IntrabarVWAP = PriceW / ShareW;

Plot1(IntrabarVWAP,"BarVWAP");
1. Please use code tag when posting codes. I have tagged them for you in your above post.

2. What do you mean by "This should work"?
Can you draw a diagram(s) to illustrate how the indicator should look like?

chisound
Posts: 13
Joined: 29 Jun 2010
Contact:

Re: Intrabar VWAP not working correctly

Postby chisound » 14 Sep 2011

Hi thanks for the reply. I will plot this on the same subchart as the underlying barchart using the same scale as the barchart. Plot1 is set to a left or righttick with a different color to the barchart or candelchart so that it appears in the body of each bar in the chart as a horizontal tick. This will give you a visual comparison of plot1 ie IntrabarVWAP in each bar with the close of each bar in the chart.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 14 Sep 2011

Hi thanks for the reply. I will plot this on the same subchart as the underlying barchart using the same scale as the barchart. Plot1 is set to a left or righttick with a different color to the barchart or candelchart so that it appears in the body of each bar in the chart as a horizontal tick. This will give you a visual comparison of plot1 ie IntrabarVWAP in each bar with the close of each bar in the chart.
ok, so how should it look like?

can you draw some pictures to illustrate?

say, when price is higher?
or, when volume is lower?


What is the current code not doing that you want it to do?

chisound
Posts: 13
Joined: 29 Jun 2010
Contact:

Re: Intrabar VWAP not working correctly

Postby chisound » 14 Sep 2011

What i want it to do is calculate the VWAP internally of each bar, in other words, calculate for each bar the following:

IntrabarVWAP = Sum of (Price of trade X volume of trade ) / ( Sum of volume)

When a bar opens it needs to calculate (Price of Trade x volume of trade ) for each trade in a bar, sum them up, keep a running total of total traded volume during a bar, then when the bar closes it needs to calculate the above formula and then reset (Price of Trade x volume of trade ) and the running total of total volume traded for a bar to zero so that it can be calculated again for the next bar. It's the same as doing a regular VWAP calculation that is done for a trading day bar by bar, except the VWAP is calculated for each bar on a tick by tick or trade by trade basis.

The Current code is only returning the close of each bar for the IntrabarVWAP which is not possible. I hope this helps.

I am using Multicharts 7.0 and i don't think that the intrabarpersist variable declaration works as it is supposed to work even if you run the indicator with every tick.

Many thanks for your help.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 14 Sep 2011

What i want it to do is calculate the VWAP internally of each bar, in other words, calculate for each bar the following:

IntrabarVWAP = Sum of (Price of trade X volume of trade ) / ( Sum of volume)

When a bar opens it needs to calculate (Price of Trade x volume of trade ) for each trade in a bar, sum them up, keep a running total of total traded volume during a bar, then when the bar closes it needs to calculate the above formula and then reset (Price of Trade x volume of trade ) and the running total of total volume traded for a bar to zero so that it can be calculated again for the next bar. It's the same as doing a regular VWAP calculation that is done for a trading day bar by bar, except the VWAP is calculated for each bar on a tick by tick or trade by trade basis.

The Current code is only returning the close of each bar for the IntrabarVWAP which is not possible. I hope this helps.

I am using Multicharts 7.0 and i don't think that the intrabarpersist variable declaration works as it is supposed to work even if you run the indicator with every tick.

Many thanks for your help.
I hope you can see that your description here is quite different from what you had in your prevoous posts, and your code is nowhere near a mirror of either.

What you have right now is a continuous thought of things you have in mind. They are not logics. They are spaghettis.

You have to organize yourself better... cut down a complex task into small pieces, so that you can attack them one by one.

My suggestion:
1. rewrite your description in a line-by-line format. (just like coding, but in plain English)
ie. write down
one thought per line,
one logic per line,
one action per line,
no two thoughts on the same sentence,
no two sentences on the same line.

2. Draw up a variable table.
Work out how many variables you need to make the computation,
and write down the logic behind each variable before you start coding.

This is a start, you will discover more as you progress.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 14 Sep 2011

Code: Select all

PriceW = PriceW + (Close * (Upticks + DownTicks));
ShareW = ShareW + (Upticks + Downticks);
If ShareW > 0 then IntrabarVWAP = PriceW / ShareW;
The above code translates to the following:

Intrabarpersist means to reset to zero on every tick.

since
Upticks + Downticks = Ticks,

you can reduce the code to...

Code: Select all

PriceW = 0 + (Close * Ticks);
ShareW = 0 + Ticks;

IntrabarVWAP = (Close * Ticks) / Ticks;
i.e.
IntrabarVWAP = Close;

Hope the above helps.

chisound
Posts: 13
Joined: 29 Jun 2010
Contact:

Re: Intrabar VWAP not working correctly

Postby chisound » 16 Sep 2011

Hi TJ:

Thanks for your help. From what i understand about intrabarpersist if i use this in the variable declarations for the variables PriceW and ShareW, these variables will be updated on every tick of the bar. So i need these to be updated on every tick of the bar as follows:

Code: Select all

PriceW = PriceW + (Close * (Ticks));
ShareW = ShareW + (Ticks);
Therefore PriceW should increase with every new tick of the bar adding the price of a tick * volume for the tick to the value of PriceW for the last tick.

Then at the end of the bar the following line of code gets executed to calculate the IntrabarVWAP.

Code: Select all

If ShareW > 0 then IntrabarVWAP = PriceW / ShareW;
Now what remains to do is to reset the PriceW and ShareW variables to 0 so that they can start incrementing again for the ticks received in the next bar.

Would you do this on the closing tick of the bar or on the opening tick of the next bar, ie barstatus =2 or barstatus = 0?

If i reset PriceW and ShareW on the opening tick of the bar as per the code below i get a result for IntrabarVWAP which is not the close of every bar, however, the result is outside of the range of each bar, which is not possible.

Code: Select all

vars:
intrabarpersist PriceW(0),
intrabarpersist ShareW(0),
PriceX(0),
ShareX(0),
IntrabarVWAP(0);

If barstatus = 0 then begin
PriceW = 0;
ShareW = 0;
end;
PriceW = PriceW + (Close * (Ticks));
ShareW = ShareW + (Ticks);
PriceX = PriceW;
ShareX = ShareW;
If ShareX > 0 then IntrabarVWAP = PriceX / ShareX;

Plot1(IntrabarVWAP,"BarVWAP");
If i reset PriceW and ShareW on the closing tick of the bar as per the code below i get a result for IntrabarVWAP which is the close of every bar.

Code: Select all

vars:
intrabarpersist PriceW(0),
intrabarpersist ShareW(0),
PriceX(0),
ShareX(0),
IntrabarVWAP(0);


PriceW = PriceW + (Close * (Ticks));
ShareW = ShareW + (Ticks);
PriceX = PriceW;
ShareX = ShareW;
If ShareX > 0 then IntrabarVWAP = PriceX / ShareX;

If barstatus = 2 then begin
PriceW = 0;
ShareW = 0;
end;
Plot1(IntrabarVWAP,"BarVWAP");
Any ideas on how to reset PriceW and ShareW correctly?

Many thanks.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 16 Sep 2011

Hi TJ:
...
Any ideas on how to reset PriceW and ShareW correctly?

Many thanks.
Hint:

read my post#6 again,
especially suggestion #2.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Intrabar VWAP not working correctly

Postby TJ » 16 Sep 2011

one tool that can help you with the logic... draw a flow chart.

Image
Attachments
220px-Flowchart_Showing_Driving_to_a_Goal.png
(36.9 KiB) Downloaded 2868 times


Return to “User Contributed Studies and Indicator Library”