Implementing Weekly Bollinger Bands on Daily Charts

30 Oct 2022
0 comment
0 attachment

This code snippet demonstrates how to calculate and plot weekly Bollinger Bands on a daily chart using ProBuilder language. Bollinger Bands are a type of statistical chart characterizing the prices and volatility over time of a financial instrument or commodity, using a formulaic method involving moving averages and standard deviation.

//Weekly Bollinger Bands On Faster Time Frame chart
//PRTv11
//By Vonasi
//Date: 20200415

p = 20
deviations = 2

//store each week's close
if opendayofweek < opendayofweek[1] then
    a = a+1
    $price[a] = close[1]
endif

if a >= p then
    //get a mean of last p weeks' close price
    total = 0
    for b = a downto a-p+1
        total = total + $price[b]
    next
    avg = total/p

    //calculate standard deviation
    total = 0
    for b = a downto a-p+1
        total = total + (square($price[b]-avg))
    next
    stdev = sqrt(total/p)

    //calculate bands
    upper = avg + (stdev*deviations)
    lower = avg - (stdev*deviations)
endif

return upper as "upper", lower as "lower", avg as "avg"

The code snippet above is structured to perform the following steps:

  • Initialization: Set the period (p) to 20 and the number of standard deviations (deviations) to 2. These are typical settings for Bollinger Bands.
  • Storing Weekly Close Prices: It checks if the current day is the start of a new week by comparing the opening day of the week with the previous day. If true, it increments a counter a and stores the previous day’s close price in an array $price.
  • Calculating the Moving Average: Once there are at least p weeks of data, it calculates the average close price of the last p weeks.
  • Calculating Standard Deviation: It then calculates the standard deviation of the close prices over the same period, which measures the volatility.
  • Calculating Bollinger Bands: Using the average and standard deviation, it calculates the upper and lower Bollinger Bands.
  • Output: Finally, the code returns the upper band, lower band, and the average as outputs, which can be plotted on a chart.

This example is useful for understanding how to manipulate time series data and perform statistical calculations in ProBuilder language to create dynamic trading indicators.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/higher-time-frame-bollinger-band/#post-126260

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
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...