Calculate 1st hour average volume % over X days

Glefdar

Active member
I am thinking about how to do it and I think it's not necessarily that hard but I'm not sure. Something like:

1) Define logical condition for the first hour after market open. Could do it as "bars since" RTHopen() = 2 on a 30m candle chart maybe?
2) Sum only those volume candles over a specified number of iterations with each iteration being one day.
3) Then it's easy to get the average and show the current day's first hour as a percentage of that (e.g. 30% on a low volume day or 150% on a high volume day, etc.).

Wanted to post here about this in case it's already been done (I haven't seen this on the forums before, just the volume labels that are based on the daily aggregation) or in case there's a flaw in the approach I'm imagining 🙏
 
I am thinking about how to do it and I think it's not necessarily that hard but I'm not sure. Something like:

1) Define logical condition for the first hour after market open. Could do it as "bars since" RTHopen() = 2 on a 30m candle chart maybe?
2) Sum only those volume candles over a specified number of iterations with each iteration being one day.
3) Then it's easy to get the average and show the current day's first hour as a percentage of that (e.g. 30% on a low volume day or 150% on a high volume day, etc.).

Wanted to post here about this in case it's already been done (I haven't seen this on the forums before, just the volume labels that are based on the daily aggregation) or in case there's a flaw in the approach I'm imagining 🙏

this has the base code for collecting the data.
this version only collects data from 1 bar a day.
i need to make another version to collect data from each day open.
https://usethinkscript.com/threads/intraday-single-bar-relative-volume-rvol-for-thinkorswim.16051/
 
this has the base code for collecting the data.
this version only collects data from 1 bar a day.
i need to make another version to collect data from each day open.
https://usethinkscript.com/threads/intraday-single-bar-relative-volume-rvol-for-thinkorswim.16051/

Thanks, this appears to be perfect. It seems that for my purpose I only needed to modify your script with the following change to make the vol_today variable value persistently accessible for comparisons after the defined time for calculating relative volume has passed:

Code:
def vol_today_pre = if bn == 1 then 0
 else if today_en then vol_today_pre[1] + v
 else 0;

def vol_today = if vol_today_pre==0 then vol_today[1] else vol_today_pre;

I've tested it on a 30m 90 day chart using a previous days value of 85 (in case there were ever holidays) and it seems to work from what I can tell so far.

Edit: I'm trying to understand better how it works and if it's already coded in a way that will not break when there are holiday hours. For example, if the start time was defined as 1:00pm and the end time as 3:00pm and the study tried to include a market half day holiday such as Black Friday, would those bars just be skipped from the criteria for inclusion because curr_mins requires the condition !isNan(close)? Or does "prev_en" need to be changed as follows:

Code:
def prev_en = barz and days_en;

needs to be:

Code:
def prev_en = barz and days_en and !isNan(close);

If I’m not mistaken I think it is already not going to break from holidays, as you coded it.

One other point I was interested in but haven’t had time to attempt yet is that it would be useful to be able to define the price change between the start and end times. I think it could be done by just taking the close price of the bar before the start time and also of the open for the bar after the end time.
 
Last edited:
this has the base code for collecting the data.
this version only collects data from 1 bar a day.
i need to make another version to collect data from each day open.
https://usethinkscript.com/threads/intraday-single-bar-relative-volume-rvol-for-thinkorswim.16051/

I have been thinking about how to make historical comparisons between the rvol of the defined time segment and that of the entire session, along with the % change in price. I have a starting point that is just a simple modification of your code:

Code:
### entire session (rvol, and, prev. day's RTH close vs. current day's RTH close)

def prev_day_rth_close = close(period = aggregationPeriod.DAY)[1];

def start_0 = 0005;
def end_0 = 1600;
def segment_0 = if SecondsFromTime(start_0) >= 0 and SecondsTillTime(end_0) > 0 then 1 else 0;
def tmin_0 = SecondsFromTime(start_0) / 60;

#get current time in current day
def curr_mins_0 = highestall(if istoday and !isnan(close) and segment_0 then tmin_0 else 0);
def curr_hours_0 = curr_mins_0/60;

# desired bars, during each day
def barz_0 = (tmin_0 <= curr_mins_0 and segment_0);

# true on prev days , for the day count. exclude current day

def prev_en_0 = barz_0 and days_en and !isNan(close);
def today_en_0 = barz_0 and istoday;

def vol_today_pre_0 = if bn == 1 then 0
 else if today_en_0 then vol_today_pre_0[1] + v
 else 0;

def vol_today_0 = if vol_today_pre_0==0 then vol_today_0[1] else vol_today_pre_0;

#addchartbubble(today_en_0,close,vol_today_0,color.cyan);

def vol_prev_0 = if bn == 1 then 0
 else if prev_en_0 then vol_prev_0[1] + v
 else vol_prev_0[1];

# vol average of prev partial days
def vol_prev_avg_0 = vol_prev_0 / prev_days;

# compare todays vol , to prev days vol
def rvol_0 =  round(vol_today_0 / vol_prev_avg_0, 2);

def pricechg_today_pre_0 = if bn == 1 then ((prev_day_rth_close-close)*100)
 else if today_en_0 then vol_today_pre_0[1] + pricechg
 else 0;

def pricechg_today_0 = if pricechg_today_pre_0==0 then pricechg_today_0[1] else pricechg_today_pre_0;

#addchartbubble(today_en_0,close,pricechg_today_0,color.cyan);

def pricechg_prev_0 = if bn == 1 then 0
 else if prev_en_0 then pricechg_prev_0[1] + pricechg
 else pricechg_prev_0[1];

# pricechg average of prev partial days
def pricechg_prev_avg_0 = pricechg_prev_0 / prev_days;

# compare todays pricechg , to prev days pricechg
def rpricechg_0 =  round(pricechg_today_0 / pricechg_prev_avg_0, 2);

#-------------------

def barz_recent_0 = (tmin_0 == curr_mins_0 and segment_0);
def timz_0 = barz_recent_0 and days_en;

I think it would be very interesting to be able to make these comparisons because it would then be possible to try to find statistical relationships, e.g. maybe when the rvol of a particular period is within a certain threshold then the day's session closes green 70% of the time but when it's within another threshold maybe it's 40%.

EDIT: I initially was confused and thinking it would be necessary to use the fold function to make these comparisons from the rvol of a given time segment to the rvol and price change of the entire session, to look forward so that for every bar of the defined time segment the rvol of the whole session (as well as the end of day %change) can be accessed, but now I'm not sure. If the fold function is not used, would the historical comparisons just need to be made by referencing the bar that occurs at 1600? Seems like that would work.
 
Last edited:
Thanks, this appears to be perfect. It seems that for my purpose I only needed to modify your script with the following change to make the vol_today variable value persistently accessible for comparisons after the defined time for calculating relative volume has passed:

Code:
def vol_today_pre = if bn == 1 then 0
 else if today_en then vol_today_pre[1] + v
 else 0;

def vol_today = if vol_today_pre==0 then vol_today[1] else vol_today_pre;

I've tested it on a 30m 90 day chart using a previous days value of 85 (in case there were ever holidays) and it seems to work from what I can tell so far.

Edit: I'm trying to understand better how it works and if it's already coded in a way that will not break when there are holiday hours. For example, if the start time was defined as 1:00pm and the end time as 3:00pm and the study tried to include a market half day holiday such as Black Friday, would those bars just be skipped from the criteria for inclusion because curr_mins requires the condition !isNan(close)? Or does "prev_en" need to be changed as follows:

Code:
def prev_en = barz and days_en;

needs to be:

Code:
def prev_en = barz and days_en and !isNan(close);

If I’m not mistaken I think it is already not going to break from holidays, as you coded it.

One other point I was interested in but haven’t had time to attempt yet is that it would be useful to be able to define the price change between the start and end times. I think it could be done by just taking the close price of the bar before the start time and also of the open for the bar after the end time.
Hi bro, could you explain where do you def the variable " if bn == 1" ?
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
441 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top