Implementing Time-Anchored VWAP with Standard Deviation Bands in ProBuilder

21 Dec 2022
0 comment
0 attachment

This code snippet demonstrates how to calculate a time-anchored Volume Weighted Average Price (VWAP) along with its standard deviation bands, starting from a specific time each trading day. This is particularly useful for intraday traders who need to reset their VWAP calculation at a specific time, such as the market open.

//PRC_VWAP Time anchored | indicator
//08.02.2019
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge

// --- settings
startTime = 064200
viewSD = 1 //1 = true / 0 = false
// --- end of settings

VWAP=undefined
SDup1 = undefined
SDlw1 = undefined
SDup2 = undefined
SDlw2 = undefined
SDup3 = undefined
SDlw3 = undefined

if time=startTime and date=today then
    startbar=barindex
endif

if time>=startTime and startbar>0 then
    barcount=barindex-startbar
    d = max(1, barcount)
    VWAP = SUMMATION[d](volume*typicalprice)/SUMMATION[d](volume)
    
    if(barcount=0) then
        sd = 0
    else
        sd = SUMMATION[d](max(abs(high-vwap),abs(vwap-low)))/d
    endif
    
    if viewSD then
        SDup1 = vwap+sd
        SDlw1 = vwap-sd
        SDup2 = vwap+sd*2
        SDlw2 = vwap-sd*2
        SDup3 = vwap+sd*3
        SDlw3 = vwap-sd*3
    endif
    
    if vwap>vwap[1] then
        color = 1
    else
        color = -1
    endif
endif

RETURN VWAP coloured by color as "VWAP", SDup1 coloured(102,102,102) as "upper 1 STD", SDlw1 coloured(102,102,102) as "lower 1 STD", SDup2 coloured(102,102,102) as "upper 2 STD", SDlw2 coloured(102,102,102) as "lower 2 STD", SDup3 coloured(102,102,102) as "upper 3 STD", SDlw3 coloured(102,102,102) as "lower 3 STD"

The code is structured as follows:

  • Settings: Defines the start time for the VWAP calculation and whether to display standard deviation bands.
  • Initialization: Variables for VWAP and standard deviations are initialized as undefined.
  • Start Condition: Checks if the current time and date match the start time and today’s date to set the starting bar index.
  • VWAP Calculation: If the current time is after the start time and the start bar has been set, it calculates the VWAP based on the volume and typical price since the start time.
  • Standard Deviation Calculation: Calculates the standard deviation of the price from the VWAP to form the deviation bands if enabled.
  • Color Coding: Changes the color of the VWAP line based on its direction (upward or downward).
  • Output: Returns the VWAP and its standard deviation bands with specified colors.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/conversion-du-code-midas-volume-damibroker/#post-90855

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...