Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

vwap Topic Rating:
Previous Topic · Next Topic Watch this topic · Print this topic ·
mdfalco
Posted : Friday, August 7, 2009 9:13:57 AM
Registered User
Joined: 8/30/2007
Posts: 33

Hi,

VWAP, only works for less than a day, time frames? (minutes, hours...). don´t it?

Could be works with days or week, time frame?

thanks

3dbeing
Posted : Friday, August 7, 2009 10:15:02 AM
Registered User
Joined: 6/24/2009
Posts: 178
Actually I am having a lot of trouble with vwap too, and I took a look at the code.  As the graph doesn't give me expected results, ie. vwap price = closing price, I wanted to step through the code....

Dim numer As Single 
Dim denom As Single
For i As Integer = 0 To price.bar.count - 1  ## what is price.bar.count?
    If i > 0 AndAlso price.bar.datevalue(i).day <> price.bar.datevalue(i - 1).day Then  ## what is <>?          
        ' it's a new day  ## what does this mean?
        numer = 0
        denom = 0
    End If   
    numer += price.bar.value(i) * volume.line.value(i)
    denom += volume.line.value(i)
    Me.addtoOutput(price.bar.datevalue(i), numer / denom)  ## I think this is incorrect please check out 
Next                                                                                                     ## please check out this site for
If numer > 0 Then                                                                             ## cumulative vs itterative algorithm
    Dim outDate As Date = price.bar.datevalue(price.bar.count - 1)   
    Me.addtoOutput(outdate, numer / denom)
                            ## I'm not sure what these 2 lines say?
End If

Thanks,

-=3db
Bruce_L
Posted : Friday, August 7, 2009 3:09:06 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
mdfalco,
Yes, the reason it works only with Time Frames shorter than Daily is that it is a 1-Day VWAP that resets at the start of each day. You can adjust when it resets by adjusting the following:

            If i > 0 AndAlso price.bar.datevalue(i).day <> price.bar.datevalue(i - 1).day Then          
                ' it's a new day

To the following to reset at the start of each week:

            If i > 0 AndAlso price.bar.datevalue(i).dayofweek < _
                price.bar.datevalue(i - 1).dayofweek Then           
                ' it's a new week

Or the following to reset at the start of each month:

            If i > 0 AndAlso price.bar.datevalue(i).day < price.bar.datevalue(i - 1).day Then          
                ' it's a new month

The ' it's a new day, ' it's a new week and ' it's a new month lines don't affect the results as they are just comments.

QUOTE (3dbeing)
Actually I am having a lot of trouble with vwap too, and I took a look at the code.  As the graph doesn't give me expected results, ie. vwap price = closing price...

That's part of the point of a Volume Weighted Average Price, the VWAP at the end of the day usually won't match the closing Price of at the end of the day. Let's take a simplified example with only 2 trades. The first trade of the day is for 900 Shares at $10 and the second trade of the day is for only 100 Shares but is at $20. The closing Price at the end of the day only reflects the $20 trade, but the VWAP is (900 * 10 + 100 * 20) / 1100 = 11000 / 1000 = $11.

Keep in mind that the Indicator uses the Bars of the selected Time Frame for its calculations. You would need to be using the Tick Time Frame to calculate a VWAP that actually used individual trades (and the Tick data available in StockFinder is only available during the trading day and very limited in far how back it goes).

QUOTE (3dbeing)
...I wanted to step through the code....

You may want to read completely through the RealCode Programmers Reference to get a better idea of the RealCode specific syntax that is added to VB.NET (Visual Basic) to allow it to interface with StockFinder.

You actually need to look at the Class tab instead of the Code tab for this since it uses manual looping. The following is everything below the Inherits line:

    Sub New
        autoloop = False
    End Sub
    Public Overrides Function Plot() As System.Single
        Dim numer As Single
        Dim denom As Single
        For i As Integer = 0 To price.bar.count - 1
            If i > 0 AndAlso price.bar.datevalue(i).day < price.bar.datevalue(i - 1).day Then           
                ' it's a new month
                numer = 0
                denom = 0
            End If   
            numer += price.bar.value(i) * volume.line.value(i)
            denom += volume.line.value(i)
            Me.addtoOutput(price.bar.datevalue(i), numer / denom)               
        Next
        If numer > 0 Then
            Dim outDate As Date = price.bar.datevalue(price.bar.count - 1)   
            Me.addtoOutput(outdate, numer / denom)                           
        End If
    End Function
End Class

QUOTE (3dbeing)
For i As Integer = 0 To price.bar.count - 1  ## what is price.bar.count?

It's the number of bars of data accessible for price within the RealCode.

QUOTE (3dbeing)
    If i > 0 AndAlso price.bar.datevalue(i).day <> price.bar.datevalue(i - 1).day Then  ## what is <>?

It's an operand that means not equal.

QUOTE (3dbeing)
        ' it's a new day  ## what does this mean?

It's a comment that indicates the condition for which the previous line was testing. It has no effect on the code.

QUOTE (3dbeing)
Me.addtoOutput(price.bar.datevalue(i), numer / denom)  ## I think this is incorrect please check out 
Next                                                                                                     ## please check out this site for
If numer > 0 Then                                                                            cumulative vs itterative algorithm

The calculation is not incorrect. The article in question uses a strawman with incorrect math for the "iterative" example.

QUOTE (referenced article)

The VWAP would be:

{($20.05 x 1000) + ($20.06 x 800) + ($20.04 x 100) + ($20.03 x 2000) + ($20.03 x 3000)} / (1000 + 800 + 100 + 2000 + 3000)

This translates into:

(20050 + 16048 + 2004 + 40060 + 60090) / (6900) = 20.0365. Therefore $20.0365 would be the "Cumulative VWAP"


This appears to be done correctly although we might want to expand the results slightly for comparison purposes in the next step to $20.036521739130434782608695652174.

QUOTE (referenced article)
  • 1st Iteration: (20.05 x 1000) / 1000 = 20050 / 1000 = $20.05 (correct math so far)
  • 2nd Iteration: $20.05 + {(20.06 - 25.05) x 800)} / (1000 + 800) = 20.0544 (correct math so far, but with more digits it comes to 20.054444444444444444444444444444)
  • 3rd Iteration: 20.054444444444444444444444444444 + {(20.04 - 20.054444444444444444444444444444) x 100} / (1800 + 100) = 20.0536 (correct math so far, but it has a rounding error, should be 20.053684210526315789473684210522)
  • 4th Iteration: 20.053684210526315789473684210522 + {(20.03 - 20.053684210526315789473684210522) x 2000) / (1900 + 2000) = 20.0311 (the math here is just wrong, it should be 20.041538461538461538461538461535)
  • 5th Iteration: 20.041538461538461538461538461535 + {(20.03 - 20.041538461538461538461538461535) x 3000) / (3900 + 3000) = 20.0306 (the math here is just wrong, it should be 20.036521739130434782608695652169)

Notice that the math done by the author of the article on the last two steps is incorrect. The algorithm I would use for doing this would actually be a bit different but gets the same results (well darned close anyway, I'm not using an infinite precision calculator):

    * 1st Iteration: (20.05 x 1000) / 1000 = 20050 / 1000 = 20.05

    * 2nd Iteration: (20.05 x 1000 + 20.06 x 800) / (1000 + 800) = 20.054444444444444444444444444444

    * 3rd Iteration: (20.054444444444444444444444444444 x 1800 + 20.04 x 100) / (1800 + 100) = 20.053684210526315789473684210526

    * 4th Iteration: (20.053684210526315789473684210526 x 1900 + 20.03 x 2000) / (1900 + 2000) = 20.041538461538461538461538461538

    * 5th Iteration: (20.041538461538461538461538461538 x 3900 + 20.03 x 3000) / (3900 + 3000) = 20.036521739130434782608695652174

Notice that the end result just doesn't get really close to the "cumulative" example, it matches exactly. Also note that at each point during the day, the "cumulative" calculations would match the "iterative" results.

{($20.05 x 1000)} / (1000) = (20050) / (1000) = 20.05

{($20.05 x 1000) + ($20.06 x 800)} / (1000 + 800) = (20050 + 16048) / (1800) = 20.054444444444444444444444444444

{($20.05 x 1000) + ($20.06 x 800) + ($20.04 x 100)} / (1000 + 800 + 100) = (20050 + 16048 + 2004) / (1900) = 20.053684210526315789473684210526

{($20.05 x 1000) + ($20.06 x 800) + ($20.04 x 100) + ($20.03 x 2000)} / (1000 + 800 + 100 + 2000) = (20050 + 16048 + 2004 + 40060) / (3900) = 20.041538461538461538461538461538

{($20.05 x 1000) + ($20.06 x 800) + ($20.04 x 100) + ($20.03 x 2000) + ($20.03 x 3000)} / (1000 + 800 + 100 + 2000 + 3000) = (20050 + 16048 + 2004 + 40060 + 60090) / (6900) = 20.036521739130434782608695652174

QUOTE (3dbeing)
    Dim outDate As Date = price.bar.datevalue(price.bar.count - 1)   
    Me.addtoOutput(outdate, numer / denom)
                            ## I'm not sure what these 2 lines say?

They just create a date for the last bar and use it to output the final value.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
3dbeing
Posted : Friday, August 7, 2009 3:22:26 PM
Registered User
Joined: 6/24/2009
Posts: 178
Thanks bruce.... this is going to be a long weekend, a lot of info you've given here...
3dbeing
Posted : Friday, August 7, 2009 3:54:04 PM
Registered User
Joined: 6/24/2009
Posts: 178
is there a way to tell SF to look down a level, say I'm in the daily, and I want it to look down to the hourly, or I'm in the hourly and I want it to look down to the .5hr?
Bruce_L
Posted : Friday, August 7, 2009 4:30:01 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
3dbeing,
You can set the Time Frame to just about anything you want in a Block Diagram based Indicator, but in RealCode, the Time Frame is inherited from the Chart. Block Diagrams have been de-emphasized in StockFinder but the Your First 3 Block Diagrams video covers creating Block Diagrams.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
3dbeing
Posted : Saturday, August 8, 2009 8:28:07 PM
Registered User
Joined: 6/24/2009
Posts: 178
actually here is a page that is actually the direction I was going...
mdfalco
Posted : Sunday, August 9, 2009 3:19:24 AM
Registered User
Joined: 8/30/2007
Posts: 33
thanks
Bruce_L
Posted : Monday, August 10, 2009 12:32:08 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
3dbeing,
Volume Weighted Average Price (VWAP) has a lot of different interpretations. A Volume Weighted Moving Average (VWMA) is one possible interpretation, but other interpretations appear to be more common. I'm happy to read that Volume Weighted Moving Averages meet your particular needs.

mdfalco,
You're welcome. Our pleasure.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.