Implementing Multi-Timeframe OHLC Data Retrieval in ProBuilder

01 Jun 2022
0 comment
0 attachment

This ProBuilder script is designed to retrieve and display Open, High, Low, and Close (OHLC) values from various timeframes including yearly, quarterly, monthly, weekly, daily, 4-hour, and 1-hour. It allows users to look back at historical OHLC data for a specified number of periods, which can be useful for analyzing past market behavior.

//Settings
LookBack = 1
Y = 0
Q = 0
M = 1
W = 0
D = 0
H4 = 0
H1 = 0
OpenClose = 1
HighLow = 1

//1 Hour
if h1 then
    if openhour <> openhour[1] then
        h1index = h1index + 1
        h1high = 0
        h1low = close
        h1open = open
        h1close = close
    if h1index > lookback then
        for j = 1 to barindex
            if h1index[j] = h1index - lookback then
                myh1high = h1high[j]
                myh1low = h1low[j]
                myh1open = h1open[j]
                myh1close = h1close[j]
                break
            endif
        next
    endif
endif
h1high = max(h1high,high)
h1low = min(h1low,low)
h1close = close
endif

//4 Hour
if h4 then
    if openhour <> openhour[1] and (openhour = 1 or openhour = 5 or openhour = 9 or openhour = 13 or openhour = 17 or openhour = 21) then
        h4index = h4index + 1
        h4high = 0
        h4low = close
        h4open = open
        h4close = close
    if h4index > lookback then
        for j = 1 to barindex
            if h4index[j] = h4index - lookback then
                myh4high = h4high[j]
                myh4low = h4low[j]
                myh4open = h4open[j]
                myh4close = h4close[j]
                break
            endif
        next
    endif
endif
h4high = max(h4high,high)
h4low = min(h4low,low)
h4close = close
endif

//Day
if d then
    if openday <> openday[1] and openday[1] <> 7 then
        dayindex = dayindex + 1
        dayhigh = 0
        daylow = close
        dayopen = open
        dayclose = close
    if dayindex > lookback then
        for j = 1 to barindex
            if dayindex[j] = dayindex - lookback then
                mydayhigh = dayhigh[j]
                mydaylow = daylow[j]
                mydayopen = dayopen[j]
                mydayclose = dayclose[j]
                break
            endif
        next
    endif
endif
dayhigh = max(dayhigh,high)
daylow = min(daylow,low)
dayclose = close
endif

//Week
if w then
    if opendayofweek < opendayofweek[1] then
        weekindex = weekindex + 1
        weekhigh = 0
        weeklow = close
        weekopen = open
        weekclose = close
    if weekindex > lookback then
        for j = 1 to barindex
            if weekindex[j] = weekindex - lookback 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
endif

//Month
if m then
    if openmonth <> openmonth[1] then
        monthindex = monthindex + 1
        monthhigh = 0
        monthlow = close
        monthopen = open
        monthclose = close
    if monthindex > lookback then
        for j = 1 to barindex
            if monthindex[j] = monthindex - lookback 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
endif

//Quarter
if q then
    if openmonth <> openmonth[1] and (openmonth = 1 or openmonth = 4 or openmonth = 7 or openmonth = 10) then
        quarterindex = quarterindex + 1
        quarterhigh = 0
        quarterlow = close
        quarteropen = open
        quarterclose = close
    if quarterindex > lookback then
        for j = 1 to barindex
            if quarterindex[j] = quarterindex - lookback then
                myquarterhigh = quarterhigh[j]
                myquarterlow = quarterlow[j]
                myquarteropen = quarteropen[j]
                myquarterclose = quarterclose[j]
                break
            endif
        next
    endif
endif
quarterhigh = max(quarterhigh,high)
quarterlow = min(quarterlow,low)
quarterclose = close
endif

//Year
if y then
    if openyear <> openyear[1] then
        yearindex = yearindex + 1
        yearhigh = 0
        yearlow = close
        yearopen = open
        yearclose = close
    if yearindex > lookback then
        for j = 1 to barindex
            if yearindex[j] = yearindex - lookback then
                myyearhigh = yearhigh[j]
                myyearlow = yearlow[j]
                myyearopen = yearopen[j]
                myyearclose = yearclose[j]
                break
            endif
        next
    endif
endif
yearhigh = max(yearhigh,high)
yearlow = min(yearlow,low)
yearclose = close
endif

//Remove zero value plotting at start and unwanted lines
if h1index < lookback or not h1 or not openclose then
    myh1open = undefined
    myh1close = undefined
endif
if h1index < lookback or not h1 or not highlow then
    myh1high = undefined
    myh1low = undefined
endif
if h4index < lookback or not h4 or not openclose then
    myh4open = undefined
    myh4close = undefined
endif
if h4index < lookback or not h4 or not highlow then
    myh4high = undefined
    myh4low = undefined
endif
if dayindex < lookback or not d or not openclose then
    mydayopen = undefined
    mydayclose = undefined
endif
if dayindex < lookback or not d or not highlow then
    mydayhigh = undefined
    mydaylow = undefined
endif
if weekindex < lookback or not w or not openclose then
    myweekopen = undefined
    myweekclose = undefined
endif
if weekindex < lookback or not w or not highlow then
    myweekhigh = undefined
    myweeklow = undefined
endif
if monthindex < lookback or not m or not openclose then
    mymonthopen = undefined
    mymonthclose = undefined
endif
if monthindex < lookback or not m or not highlow then
    mymonthhigh = undefined
    mymonthlow = undefined
endif
if quarterindex < lookback or not q or not openclose then
    myquarteropen = undefined
    myquarterclose = undefined
endif
if quarterindex < lookback or not q or not highlow then
    myquarterhigh = undefined
    myquarterlow = undefined
endif
if yearindex < lookback or not y or not openclose then
    myyearopen = undefined
    myyearclose = undefined
endif
if yearindex < lookback or not y or not highlow then
    myyearhigh = undefined
    myyearlow = undefined
endif

return myweekopen coloured(100,149,237) as "Week Open", myweekhigh coloured(0,128,0) as "Week High", myweeklow coloured(128,0,0) as "Week Low", myweekclose coloured(0,0,255) as "Week Close", mymonthopen coloured(100,149,237) as "Month Open", mymonthhigh coloured (0,128,0) as "Month High", mymonthlow coloured (128,0,0) as "Month Low", mymonthclose coloured (0,0,255) as "Month Close",mydayopen coloured(100,149,237) as "Day Open", mydayhigh coloured (0,128,0) as "Day High", mydaylow coloured (128,0,0) as "Day Low", mydayclose coloured (0,0,255) as "Day Close", myyearopen coloured(100,149,237) as "Year Open", myyearhigh coloured (0,128,0) as "Year High", myyearlow coloured (128,0,0) as "Year Low", myyearclose coloured (0,0,255) as "Year Close", myquarteropen coloured(100,149,237) as "Quarter Open", myquarterhigh coloured (0,128,0) as "Quarter High", myquarterlow coloured (128,0,0) as "Quarter Low", myquarterclose coloured (0,0,255) as "Quarter Close", myh4open coloured(100,149,237) as "H4 Open", myh4high coloured (0,128,0) as "H4 High", myh4low coloured (128,0,0) as "H4 Low", myh4close coloured (0,0,255) as "H4 Close", myh1open coloured(100,149,237) as "H1 Open", myh1high coloured (0,128,0) as "H1 High", myh1low coloured (128,0,0) as "H1 Low", myh1close coloured (0,0,255) as "H1 Close"

Explanation of the Code:

  • The script starts by setting the LookBack variable, which determines how many periods back the data should be retrieved.
  • It then checks for each timeframe (yearly, quarterly, etc.) whether the current bar is the start of a new period (e.g., a new day, week, month, etc.).
  • If it is the start of a new period, it initializes the OHLC values for that period.
  • As the period progresses, it continuously updates the high and low values.
  • At the end of the period, it stores the final OHLC values and uses them to display historical data based on the LookBack setting.
  • The script also includes conditions to handle undefined values at the start of the data series or when certain timeframes are not selected.

This script is useful for traders and analysts who need to access historical OHLC data across multiple timeframes for their technical analysis or trading strategies.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/conversion-tradingview-to-prorealtime-previous-ohlc/#post-95716

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