# TOS Scan Multiple Aggregation - Calculating OHLC

#### Ginu09

##### Member
Hi all, does anybody have a code snippet for how to calculate the open, high, low, close in a scan for a higher timeframe in TOS? TOS limits multiple aggregation periods within a scan but I'd like to scan on a lower aggregation period with some analysis on higher timeframe candles. I'm not sure how to reference the OHLC of the higher time frame from within a scan. Thanks!

TOS scans cannot handle secondary aggregations, A work-around is to scan on one aggregation, save to a watchlist. Scan that watchlist with the 2nd aggregation.

TOS scans cannot handle secondary aggregations, A work-around is to scan on one aggregation, save to a watchlist. Scan that watchlist with the 2nd aggregation.

I thought about that. It’s a bit complicated but that doesn’t work in my case because I’m looking at referencing the values of OHLC for 10 minute candles on a 5 minute chart, not just looking at a condition being satisfied.

@Ginu09 We have no choice but to live within the limitations of the TOS platform and Thinkscript... What we can do in scans is not the same as what we can do on charts... We have to pick our battles wisely and adapt as best as possible... Trust me, I feel like my hands are tied behind my back at times when trying to code different scenarios...

There must be a way to look at elapsed time from market open and divide by number of candles on this timeframe and look at the resulting open, high, low or close.

I thought about that. It’s a bit complicated but that doesn’t work in my case because I’m looking at referencing the values of OHLC for 10 minute candles on a 5 minute chart, not just looking at a condition being satisfied.
i didn't test this in a scan, but i think it will work....
if your main time is 5min and you also want 10min, try looking at 2 bars.

def hi10 = highest(high,2);
def lo10 = lowest(low,2);
def opn10 = open[1];
def cls10 = close;

this will be like the 10minute bars are half overlapped, so may not be as desired.
need to find the start and end of the periods , then find the high and low.
i'm working on a chart version , will post in a bit.

i didn't test this in a scan, but i think it will work....
if your main time is 5min and you also want 10min, try looking at 2 bars.

def hi10 = highest(high,2);
def lo10 = lowest(low,2);
def opn10 = open[1];
def cls10 = close;

this will be like the 10minute bars are half overlapped, so may not be as desired.
need to find the start and end of the periods , then find the high and low.
i'm working on a chart version , will post in a bit.
Thanks, why do you say they are half overlapped?

EDIT: oh because you're calculating this every bar rather than every 2 bars.

this is an upper chart study. i'm not sure if it can be used in a scan, i didn't test it.
it works on after hours, but because of inconsistant quantity of bars in after hours, i recommend using it on normal trading hours.

in the middle of the code , are the 4 main variables for high, low, open, close.
notice the sides of the shading are vertical. it alternates between 2 clouds, so they are separate. if just 1 cloud was used, they would be joined.

good
it finds open, high, low, close levels, in custom time frames / periods.
any number of minutes can be used for the time period, from 2 to ...500, 1000,...
ex. 15/1, 15/3, 30/5, 25/5, 60/5 (user min / chart min)
if you use 195 minutes on a 1min chart, you will have 2 periods for the day. or try 195/5 , or 195/15,...
it tests if the user time is a multiple of the chart time and calculates how many bars are in a period.
it will show a warning label if invalid numbers are used.
if invalid numbers are used, it defaults to a period of 1 bar and doesn't draw anything.

it doesn't find OHLC values during the last/active time period. because of this if used in a scan, would have to look back x bars, to read valid OHLC data.
i tried a couple things, but didn't get it working. i thought i would share what i have so far and maybe i or someone can update it.

Code:
``````# bigagg_01

# ---------------------------------
# halcyonguy
# 21-08-03
# find OHLC levels, in user defined time periods
#   period minutes, (any number > 1)
#   (if user time is a multiple of chart time, ex. 15/1, 15/3, 30/5, 25/5,...)
# draw high/low lines and shading
# labels to display,
#   quantity of bars per time period
#   open,high,low,close values (currently shows n/a during day)
# options,
#   bubbles to show OHLC levels, on 1st bar or all bars
#   bubbles to show just high and low
# ---------------------------------

def bn = BarNumber();
def na = Double.NaN;
def barCount = HighestAll(If(IsNaN(close), 0, bn));

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
input bigcandlemin = 30;

# is chartmin a multiple of bigcandlemin?
def bigmulti = (bigcandlemin / chartmin) == floor(bigcandlemin / chartmin);
def bigbar2 = if (!bigmulti or bigcandlemin <= chartmin) then 1 else (bigcandlemin / chartmin);

# add offset -1 to bar#. want 2nd 30min start to be on bar31, not 30
def bnoff = -1;
# find first bar in a group of bigbar bars
def bigbarfirst = ((bn + bnoff) / bigbar2) == Floor((bn + bnoff) / bigbar2);
def bigbar = bigbar2;

addlabel(bigmulti, "group bars " + bigbar + " / " + bigcandlemin + "min", color.yellow);
addlabel(!bigmulti, "group minutes " + bigcandlemin + " is not a multiple of cchart time " + chartmin, color.yellow);

# count bar groups
def bigbarcnt = if bigbarfirst then bigbarcnt[1] + 1 else bigbarcnt[1];
def bigbareven = (bigbarcnt/2) == Floor(bigbarcnt/2);

# highest in bigbar period
def hi2 = if bigbarfirst then Highest(high[-(bigbar-1)], bigbar) else hi2[1];
# lowest in bigbar period
def lo2 = if bigbarfirst then lowest(low[-(bigbar-1)], bigbar) else lo2[1];

# ===============================
def bighi = round(hi2,3);
def biglo = round(lo2,3);
def bigopen = if bigbarfirst then open else bigopen[1];
def bigclose = if bigbarfirst then close[-(bigbar-1)] else bigclose[1];
# ===============================

plot hilvla = if bigbareven then bighi else na;
plot hilvlb = if !bigbareven then bighi else na;
hilvla.setdefaultcolor(color.green);
hilvlb.setdefaultcolor(color.green);

plot lolvla = if bigbareven then biglo else na;
plot lolvlb = if !bigbareven then biglo else na;
lolvla.setdefaultcolor(color.red);
lolvlb.setdefaultcolor(color.red);

input show_label_stats = yes;
Addlabel(show_label_stats and bigmulti, "O " + bigopen + " H " + bighi + " L " + biglo + " C" + bigclose , Color.yellow);

def bighieven = if show_shading and bigbareven then bighi else na;
def bighiodd = if show_shading and !bigbareven then bighi else na;

# test data ------------------------
#AddChartBubble(1, hi2, bn , Color.MAGENTA, yes);

input show_one_test_bubbles_OHLC = yes;
AddChartBubble(show_one_test_bubbles_OHLC and bigbarfirst and bigmulti, lo2, "O " + bigopen + "\nH " + bighi + "\nL " + biglo + "\nC " + bigclose , Color.MAGENTA, no);

input show_all_test_bubbles_OHLC = no;
AddChartBubble(show_all_test_bubbles_OHLC and bigmulti, lo2, bigopen + "\n" + bighi + "\n" + biglo + "\n" + bigclose , Color.MAGENTA, no);

input show_hilo_price_bubbles = no;
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, bighi, bighi, Color.dark_gray, yes);
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, biglo, biglo, Color.dark_gray, no);
#``````

this is an upper chart study. i'm not sure if it can be used in a scan, i didn't test it.
it works on after hours, but because of inconsistant quantity of bars in after hours, i recommend using it on normal trading hours.

in the middle of the code , are the 4 main variables for high, low, open, close.
notice the sides of the shading are vertical. it alternates between 2 clouds, so they are separate. if just 1 cloud was used, they would be joined.

good
it finds open, high, low, close levels, in custom time frames / periods.
any number of minutes can be used for the time period, from 2 to ...500, 1000,...
ex. 15/1, 15/3, 30/5, 25/5, 60/5 (user min / chart min)
if you use 195 minutes on a 1min chart, you will have 2 periods for the day. or try 195/5 , or 195/15,...
it tests if the user time is a multiple of the chart time and calculates how many bars are in a period.
it will show a warning label if invalid numbers are used.
if invalid numbers are used, it defaults to a period of 1 bar and doesn't draw anything.

it doesn't find OHLC values during the last/active time period. because of this if used in a scan, would have to look back x bars, to read valid OHLC data.
i tried a couple things, but didn't get it working. i thought i would share what i have so far and maybe i or someone can update it.

Code:
``````# bigagg_01

# ---------------------------------
# halcyonguy
# 21-08-03
# find OHLC levels, in user defined time periods
#   period minutes, (any number > 1)
#   (if user time is a multiple of chart time, ex. 15/1, 15/3, 30/5, 25/5,...)
# draw high/low lines and shading
# labels to display,
#   quantity of bars per time period
#   open,high,low,close values (currently shows n/a during day)
# options,
#   bubbles to show OHLC levels, on 1st bar or all bars
#   bubbles to show just high and low
# ---------------------------------

def bn = BarNumber();
def na = Double.NaN;
def barCount = HighestAll(If(IsNaN(close), 0, bn));

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
input bigcandlemin = 30;

# is chartmin a multiple of bigcandlemin?
def bigmulti = (bigcandlemin / chartmin) == floor(bigcandlemin / chartmin);
def bigbar2 = if (!bigmulti or bigcandlemin <= chartmin) then 1 else (bigcandlemin / chartmin);

# add offset -1 to bar#. want 2nd 30min start to be on bar31, not 30
def bnoff = -1;
# find first bar in a group of bigbar bars
def bigbarfirst = ((bn + bnoff) / bigbar2) == Floor((bn + bnoff) / bigbar2);
def bigbar = bigbar2;

addlabel(bigmulti, "group bars " + bigbar + " / " + bigcandlemin + "min", color.yellow);
addlabel(!bigmulti, "group minutes " + bigcandlemin + " is not a multiple of cchart time " + chartmin, color.yellow);

# count bar groups
def bigbarcnt = if bigbarfirst then bigbarcnt[1] + 1 else bigbarcnt[1];
def bigbareven = (bigbarcnt/2) == Floor(bigbarcnt/2);

# highest in bigbar period
def hi2 = if bigbarfirst then Highest(high[-(bigbar-1)], bigbar) else hi2[1];
# lowest in bigbar period
def lo2 = if bigbarfirst then lowest(low[-(bigbar-1)], bigbar) else lo2[1];

# ===============================
def bighi = round(hi2,3);
def biglo = round(lo2,3);
def bigopen = if bigbarfirst then open else bigopen[1];
def bigclose = if bigbarfirst then close[-(bigbar-1)] else bigclose[1];
# ===============================

plot hilvla = if bigbareven then bighi else na;
plot hilvlb = if !bigbareven then bighi else na;
hilvla.setdefaultcolor(color.green);
hilvlb.setdefaultcolor(color.green);

plot lolvla = if bigbareven then biglo else na;
plot lolvlb = if !bigbareven then biglo else na;
lolvla.setdefaultcolor(color.red);
lolvlb.setdefaultcolor(color.red);

input show_label_stats = yes;
Addlabel(show_label_stats and bigmulti, "O " + bigopen + " H " + bighi + " L " + biglo + " C" + bigclose , Color.yellow);

def bighieven = if show_shading and bigbareven then bighi else na;
def bighiodd = if show_shading and !bigbareven then bighi else na;

# test data ------------------------
#AddChartBubble(1, hi2, bn , Color.MAGENTA, yes);

input show_one_test_bubbles_OHLC = yes;
AddChartBubble(show_one_test_bubbles_OHLC and bigbarfirst and bigmulti, lo2, "O " + bigopen + "\nH " + bighi + "\nL " + biglo + "\nC " + bigclose , Color.MAGENTA, no);

input show_all_test_bubbles_OHLC = no;
AddChartBubble(show_all_test_bubbles_OHLC and bigmulti, lo2, bigopen + "\n" + bighi + "\n" + biglo + "\n" + bigclose , Color.MAGENTA, no);

input show_hilo_price_bubbles = no;
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, bighi, bighi, Color.dark_gray, yes);
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, biglo, biglo, Color.dark_gray, no);
#``````

Oh wow! Thanks, let me test this

this is an upper chart study. i'm not sure if it can be used in a scan, i didn't test it.
it works on after hours, but because of inconsistant quantity of bars in after hours, i recommend using it on normal trading hours.

in the middle of the code , are the 4 main variables for high, low, open, close.
notice the sides of the shading are vertical. it alternates between 2 clouds, so they are separate. if just 1 cloud was used, they would be joined.

good
it finds open, high, low, close levels, in custom time frames / periods.
any number of minutes can be used for the time period, from 2 to ...500, 1000,...
ex. 15/1, 15/3, 30/5, 25/5, 60/5 (user min / chart min)
if you use 195 minutes on a 1min chart, you will have 2 periods for the day. or try 195/5 , or 195/15,...
it tests if the user time is a multiple of the chart time and calculates how many bars are in a period.
it will show a warning label if invalid numbers are used.
if invalid numbers are used, it defaults to a period of 1 bar and doesn't draw anything.

it doesn't find OHLC values during the last/active time period. because of this if used in a scan, would have to look back x bars, to read valid OHLC data.
i tried a couple things, but didn't get it working. i thought i would share what i have so far and maybe i or someone can update it.

Code:
``````# bigagg_01

# ---------------------------------
# halcyonguy
# 21-08-03
# find OHLC levels, in user defined time periods
#   period minutes, (any number > 1)
#   (if user time is a multiple of chart time, ex. 15/1, 15/3, 30/5, 25/5,...)
# draw high/low lines and shading
# labels to display,
#   quantity of bars per time period
#   open,high,low,close values (currently shows n/a during day)
# options,
#   bubbles to show OHLC levels, on 1st bar or all bars
#   bubbles to show just high and low
# ---------------------------------

def bn = BarNumber();
def na = Double.NaN;
def barCount = HighestAll(If(IsNaN(close), 0, bn));

def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
input bigcandlemin = 30;

# is chartmin a multiple of bigcandlemin?
def bigmulti = (bigcandlemin / chartmin) == floor(bigcandlemin / chartmin);
def bigbar2 = if (!bigmulti or bigcandlemin <= chartmin) then 1 else (bigcandlemin / chartmin);

# add offset -1 to bar#. want 2nd 30min start to be on bar31, not 30
def bnoff = -1;
# find first bar in a group of bigbar bars
def bigbarfirst = ((bn + bnoff) / bigbar2) == Floor((bn + bnoff) / bigbar2);
def bigbar = bigbar2;

addlabel(bigmulti, "group bars " + bigbar + " / " + bigcandlemin + "min", color.yellow);
addlabel(!bigmulti, "group minutes " + bigcandlemin + " is not a multiple of cchart time " + chartmin, color.yellow);

# count bar groups
def bigbarcnt = if bigbarfirst then bigbarcnt[1] + 1 else bigbarcnt[1];
def bigbareven = (bigbarcnt/2) == Floor(bigbarcnt/2);

# highest in bigbar period
def hi2 = if bigbarfirst then Highest(high[-(bigbar-1)], bigbar) else hi2[1];
# lowest in bigbar period
def lo2 = if bigbarfirst then lowest(low[-(bigbar-1)], bigbar) else lo2[1];

# ===============================
def bighi = round(hi2,3);
def biglo = round(lo2,3);
def bigopen = if bigbarfirst then open else bigopen[1];
def bigclose = if bigbarfirst then close[-(bigbar-1)] else bigclose[1];
# ===============================

plot hilvla = if bigbareven then bighi else na;
plot hilvlb = if !bigbareven then bighi else na;
hilvla.setdefaultcolor(color.green);
hilvlb.setdefaultcolor(color.green);

plot lolvla = if bigbareven then biglo else na;
plot lolvlb = if !bigbareven then biglo else na;
lolvla.setdefaultcolor(color.red);
lolvlb.setdefaultcolor(color.red);

input show_label_stats = yes;
Addlabel(show_label_stats and bigmulti, "O " + bigopen + " H " + bighi + " L " + biglo + " C" + bigclose , Color.yellow);

def bighieven = if show_shading and bigbareven then bighi else na;
def bighiodd = if show_shading and !bigbareven then bighi else na;

# test data ------------------------
#AddChartBubble(1, hi2, bn , Color.MAGENTA, yes);

input show_one_test_bubbles_OHLC = yes;
AddChartBubble(show_one_test_bubbles_OHLC and bigbarfirst and bigmulti, lo2, "O " + bigopen + "\nH " + bighi + "\nL " + biglo + "\nC " + bigclose , Color.MAGENTA, no);

input show_all_test_bubbles_OHLC = no;
AddChartBubble(show_all_test_bubbles_OHLC and bigmulti, lo2, bigopen + "\n" + bighi + "\n" + biglo + "\n" + bigclose , Color.MAGENTA, no);

input show_hilo_price_bubbles = no;
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, bighi, bighi, Color.dark_gray, yes);
AddChartBubble(show_hilo_price_bubbles and bigbarfirst and bigmulti, biglo, biglo, Color.dark_gray, no);
#``````

FYI
I am working on a modification of this to show the wider candle body and candlewicks, to better represent longer agg times as 1 big candle.

UPDATE 8/5
making progress. have it working to show clouds representing bigger candle bodies and wicks.
working on getting it to display something on the last period

.

Last edited:
Is it possible to convert the Daily RV into hourly scan? Basically using daily RV in the hourly scan? Thanks!

Yes Sir, I know that. Was just curious to know if this can be deduced using intra-day time frame. Thanks !

87k+ Posts
258 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?