Implementing Weekly and Monthly OHLC Tracking with Conditional Resets in ProBuilder

16 Dec 2023
0 comment
0 attachment

This code snippet demonstrates how to calculate and display weekly and monthly Open, High, Low, and Close (OHLC) values in financial charts using the ProBuilder language. It includes improvements to handle the initialization of these values at the start of new periods and applies conditional formatting based on data availability.


weeklookback = 2
if opendayofweek < opendayofweek[1] then
    weekindex = weekindex + 1
    weekhigh = 0
    weeklow = close
    weekopen = open
    weekclose = close
if weekindex > weeklookback then
    for j = 1 to barindex
        if weekindex[j] = weekindex - weeklookback then
            myweekhigh = weekhigh[j]
            myweeklow = weeklow[j]
            myweekopen = weekopen[j]
            myweekclose = weekclose[j]
            break
        endif
    next
endif
endif
weekhigh = max(weekhigh,high)
weeklow = min(weeklow,low)
weekclose = close
c = 255
if myweekopen = 0 then
    myweekopen = close
    myweekclose = close
    myweekhigh = close
    myweeklow = close
    c = 0
endif
return myweekopen coloured(0,0,0,c)as "Week Open", myweekhigh coloured(0,128,0,c) as "Week High", myweeklow coloured(128,0,0,c) as "Week Low", myweekclose coloured(0,0,255,c) as "Week Close"

monthlookback = 2
if openmonth <> openmonth[1] then
    monthindex = monthindex + 1
    monthhigh = 0
    monthlow = close
    monthopen = open
    monthclose = close
if monthindex > monthlookback then
    for j = 1 to barindex
        if monthindex[j] = monthindex - monthlookback then
            mymonthhigh = monthhigh[j]
            mymonthlow = monthlow[j]
            mymonthopen = monthopen[j]
            mymonthclose = monthclose[j]
            break
        endif
    next
endif
endif
monthhigh = max(monthhigh,high)
monthlow = min(monthlow,low)
monthclose = close
c = 255
if mymonthopen = 0 then
    mymonthopen = close
    mymonthclose = close
    mymonthhigh = close
    mymonthlow = close
    c = 0
endif
return mymonthopen coloured(0,0,0,c)as "Month Open", mymonthhigh coloured (0,128,0,c) as "Month High", mymonthlow coloured (128,0,0,c) as "Month Low", mymonthclose coloured (0,0,255,c) as "Month Close"

The code is structured to handle weekly and monthly data separately but follows a similar logic for both. Here’s a breakdown:

  • Initialization: At the start of a new week or month, indices are incremented, and base values are set to either zero or the current close price.
  • Lookback Mechanism: The script looks back a specified number of periods (controlled by weeklookback and monthlookback) to find the corresponding historical values.
  • Conditional Formatting: If no open value is found (indicative of a new period starting), all values default to the current close, and the color is set to transparent (c = 0).
  • Output: The calculated OHLC values are then displayed on the chart with specific colors for each metric (Open, High, Low, Close).

This approach ensures that the chart remains informative and visually coherent, even during the transition between different time periods.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/monthly-high-low-close-indicator/page/3/#post-95626

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