Sorry, I’m new to Proraltime and couldn’t imagine that this so dificult in the software.
With arrays it would be no problem to get monthly , weekly , daily OHLC but without arrays its not so easy.
This might help:
if (opendayofweek = 1) or (opendayofweek < opendayofweek[1] and opendayofweek <> 0) then
lastweekhigh = weekhigh
lastweeklow = weeklow
lastweekopen = weekopen
lastweekclose = weekclose
weekhigh = 0
weeklow = close
weekopen = open
weekclose = close
endif
weekhigh = max(weekhigh,high)
weeklow = min(weeklow,low)
weekclose = close
if openmonth <> openmonth[1] then
lastmonthhigh = monthhigh
lastmonthlow = monthlow
lastmonthopen = monthopen
lastmonthclose = monthclose
monthhigh = 0
monthlow = close
monthopen = open
monthclose = close
endif
monthhigh = max(monthhigh,high)
monthlow = min(monthlow,low)
monthclose = close
return lastweekhigh as "Last Weeks High",lastweeklow as "Last Weeks Low",lastweekclose as "Last Weeks Close",lastweekopen as "Last Weeks Open", lastmonthhigh as "Last Months High",lastmonthlow as "Last Months Low",lastmonthclose as "Last Months Close",lastmonthopen as "Last Months Open"
It recognises the start of a new week (even if it is not a Monday) and the start of a new month and starts recording highs, lows, open and close. At the start of every new week and new month it records the previous weeks/months values.
I just realised that my code in the last post starts the week at the open of the first weekly candle from Monday onwards when it should really be the Sunday candle onwards as this is when a week actually starts trading. Just change line 1 to:
if opendayofweek < opendayofweek[1] then
thanks @vonasi, now the big problem is this: lets say I am on a lower TF chart , how to get the weekly / monthly OHLC from N weeks /months ago ?
how to get the weekly / monthly OHLC from N weeks /months ago ?
By creating our own index that updates whenever a new month or new week start is detected and then searching back using a FOR NEXT or WHILE WEND loop this should be possible. I will try to code something – lunch first though!
Easier to separate it into two indicators – a weekly one and a monthly one.
weeklookback is the variable for how many weeks you want to look back (1 = last week, 2 = two weeks ago). monthlookback in the monthly version.
weeklookback = 2
if opendayofweek < opendayofweek[1] then
weekindex = weekindex + 1
weekhigh = 0
weeklow = close
weekopen = open
weekclose = close
endif
weekhigh = max(weekhigh,high)
weeklow = min(weeklow,low)
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
return myweekhigh as "Week High", myweeklow as "Week Low", myweekclose as "Week Close", myweekopen as "Week Open"
monthlookback = 2
if openmonth <> openmonth[1] then
monthindex = monthindex + 1
monthhigh = 0
monthlow = close
monthopen = open
monthclose = close
endif
monthhigh = max(monthhigh,high)
monthlow = min(monthlow,low)
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
return mymonthhigh as "Month High", mymonthlow as "Month Low", mymonthclose as "Month Close", mymonthopen as "Month Open"
Wow, amazing code @vonasi !
I just moved the second IF inside the first IF block and it seems to run faster. In fact we only need to check past OHLC data when month or week changes, not on every bar.
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
return mymonthopen as "Month Open", mymonthhigh as "Month High", mymonthlow as "Month Low", mymonthclose as "Month Close"
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
return myweekopen as "Week Open", myweekhigh as "Week High", myweeklow as "Week Low", myweekclose as "Week Close"
Well spotted pableitor. In my defence I did code it while eating lunch as I was too impatient to get started on it!
Glad you like it – although I’m not sure what use there is in knowing what the OHLC was several months ago? Personally I’ve never found previous OHLC levels to offer any support or resistance of any worth but I’m willing to be proved wrong.
Here is a minor improvement that gets rid of the problem of values being zero until the first new week or month has been recognised. They are now all equal to close and not drawn on the chart. I’ve added some colour too.
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"
Thanks a lot – I will also try this!
One of the codes posted in this thread works fine on prt v 10.3 but not on v11.1.
I am displayed with the error ‘A positive integer field is expected with highest’ message
Thanks
If Month<>Month[1] then
monthlyH = Highest[BarIndex – lastMonthBarIndex](High)[1]
monthlyL = Lowest[BarIndex – lastMonthBarIndex](Low)[1]
lastMonthBarIndex = BarIndex
if trimH=0 then
trimH=monthlyH
endif
trimH=max(trimH,monthlyH)
if trimL=0 then
trimL=monthlyL
endif
trimL=min(trimL,monthlyL)
if openmonth=4 or openmonth=7 or openmonth=10 or openmonth=1 then
trimestrialH=trimH
trimestrialL=trimL
endif
Endif
return trimestrialH,trimestrialL
Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
Thank you 🙂
MacroT – Try this:
if barindex >= 1 then
If Month<>Month[1] then
monthlyH = Highest[BarIndex - lastMonthBarIndex](High)[1]
monthlyL = Lowest[BarIndex - lastMonthBarIndex](Low)[1]
lastMonthBarIndex = BarIndex
if trimH=0 then
trimH=monthlyH
endif
trimH=max(trimH,monthlyH)
if trimL=0 then
trimL=monthlyL
endif
trimL=min(trimL,monthlyL)
if openmonth=4 or openmonth=7 or openmonth=10 or openmonth=1 then
trimestrialH=trimH
trimestrialL=trimL
endif
Endif
endif
return trimestrialH,trimestrialL