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:
This approach ensures that the chart remains informative and visually coherent, even during the transition between different time periods.
Check out this related content for more information:
https://www.prorealcode.com/topic/monthly-high-low-close-indicator/page/3/#post-95626
Visit Link