Relative Vol for FirstBarOfDay

DanielM

New member
I am trying to find a way to compare the volume of the first bar of today with the volume of the first bar on the last 20 days. Is that possible?
 
Solution
i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.


=== EDIT =====
well my original post had a few more errors than i thought. missing ; and def


here is the fixed code.
pick a time period with enough days to pull the data.

Code:
# volumefirstbar_xdays

# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20;
def bn = barnumber();

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day...
i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.


=== EDIT =====
well my original post had a few more errors than i thought. missing ; and def


here is the fixed code.
pick a time period with enough days to pull the data.

Code:
# volumefirstbar_xdays

# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20;
def bn = barnumber();

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
#---------------------------------------------

input agg = AggregationPeriod.DAY;

# dayz1st=if  !isnan(close(period = agg[-(dayz+1)] ) and isnan(close(period = agg)[-(dayz+2)] ) then 1 else 0;

# enable for valid period of days
def dayzen =
if   !isnan(close(period = agg)[-1] )
and
isnan(close(period = agg)[-(dayz+2)] )
then 1 else 0;

# vol of 1st bar
def vol1 = if bn == 1 then 0
else if (dayzen and firstBarOfDay) then volume else vol1[1];

# total vol in period
def vol1ttl = if bn == 1 then 0
else if ( dayzen and firstbarofday) then
vol1ttl[1] + volume
else vol1ttl[1];

def vol1avg = round(vol1ttl / dayz, 0);

def vol_ismore = ( vol1 > vol1avg);

addlabel(1,
"volume average of 1st bar, in " + dayz + " prev days, is " + vol1avg + ".  todays 1st bar vol is " + vol1,
(if vol_ismore then color.green else color.red) );
#
 
Last edited:
Solution

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

i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.

Code:
# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
#---------------------------------------------

input agg = AggregationPeriod.DAY;

# dayz1st=if  !isnan(close(period = agg[-(dayz+1)] ) and isnan(close(period = agg)[-(dayz+2)] ) then 1 else 0;

# enable for valid period of days
dayzen =
if   !isnan(close(period = agg)[-1] )
and
isnan(close(period = agg)[-(dayz+2)] )
then 1 else 0;

# vol of 1st bar
vol1 = if bn == 1 then 0
else if (dayzen and firstBarOfDay) then volume else vol1[1];

# total vol in period
vol1ttl = if bn == 1 then 0
else if ( dayzen and firstbarofday) then
vol1ttl[1] + volume
else vol1ttl[1];

volavg1 = round(vol1ttl / dayz, 0)

def vol_ismore = ( vol1 > vol1avg);

addlabel(1,
"volume average of 1st bar, in " + dayz + " prev days, is " + volavg1 + ".  todays 1st bar vol is " + vol1,
(if vol_ismore then color.green else color.red) );
#
is there a way to get a relative volume indicator that tells rel volume of a SPECIFIC candle compared to that same c
andle for the last 12 days?
 
i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.

Code:
# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
#---------------------------------------------

input agg = AggregationPeriod.DAY;

# dayz1st=if  !isnan(close(period = agg[-(dayz+1)] ) and isnan(close(period = agg)[-(dayz+2)] ) then 1 else 0;

# enable for valid period of days
dayzen =
if   !isnan(close(period = agg)[-1] )
and
isnan(close(period = agg)[-(dayz+2)] )
then 1 else 0;

# vol of 1st bar
vol1 = if bn == 1 then 0
else if (dayzen and firstBarOfDay) then volume else vol1[1];

# total vol in period
vol1ttl = if bn == 1 then 0
else if ( dayzen and firstbarofday) then
vol1ttl[1] + volume
else vol1ttl[1];

volavg1 = round(vol1ttl / dayz, 0)

def vol_ismore = ( vol1 > vol1avg);

addlabel(1,
"volume average of 1st bar, in " + dayz + " prev days, is " + volavg1 + ".  todays 1st bar vol is " + vol1,
(if vol_ismore then color.green else color.red) );
#
there is an error on line 7 and line 14
 
Dang @halcyonguy, you can type that on your phone?!? respect! I work in a text editor a lot, but I don't try that on a phone.
thank you.
many of my replies are typed up in a text file app called my notes. on my computer i usually type up the code in the tos editor, copying code snippets from text files. but if i'm away from my computer, i use the app. i tend to copy codes from this site to the app, and add some notes and key words to make it easier to find later.
https://play.google.com/store/apps/details?id=net.kreosoft.android.mynotes

@astro_phx , @jhilou
sorry guys, i had an idea at that moment and did the best i could to get it across with what i had. i will look over my code tonight. go ahead and give it a try, to fix the code.
 
i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.


=== EDIT =====
well my original post had a few more errors than i thought. missing ; and def


here is the fixed code.
pick a time period with enough days to pull the data.

Code:
# volumefirstbar_xdays

# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20;
def bn = barnumber();

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
#---------------------------------------------

input agg = AggregationPeriod.DAY;

# dayz1st=if  !isnan(close(period = agg[-(dayz+1)] ) and isnan(close(period = agg)[-(dayz+2)] ) then 1 else 0;

# enable for valid period of days
def dayzen =
if   !isnan(close(period = agg)[-1] )
and
isnan(close(period = agg)[-(dayz+2)] )
then 1 else 0;

# vol of 1st bar
def vol1 = if bn == 1 then 0
else if (dayzen and firstBarOfDay) then volume else vol1[1];

# total vol in period
def vol1ttl = if bn == 1 then 0
else if ( dayzen and firstbarofday) then
vol1ttl[1] + volume
else vol1ttl[1];

def vol1avg = round(vol1ttl / dayz, 0);

def vol_ismore = ( vol1 > vol1avg);

addlabel(1,
"volume average of 1st bar, in " + dayz + " prev days, is " + vol1avg + ".  todays 1st bar vol is " + vol1,
(if vol_ismore then color.green else color.red) );
#
thanks this is what I was looking for. Very cool trying it out now.
 
Would this be useful to determine if this was to be a trend day vs a range day, or to determine risk on vs risk off?
If I am using premarket data to drive an ORB target and then get a big price move in the opening bar I want to know that the big price range is due to abnormal volume in hopes that it can be sustained for a few bars. This is in comparison to a huge swing in price but little to no relative volume and therefore the price immediately reverses.
 
@halcyonguy First I really appericate this. Thank you so much. Very cool and it helps me out a lot with the scripting in regards to timing and start of day. However, Why wouldn't I be able to put this in a scan? I replaced the label with a "plot scan = vol_ismore;" and it doesn't return anything. But I can use it as a strategy on the chart and it works like a charm.

Thanks again, Daniel
 
@halcyonguy First I really appericate this. Thank you so much. Very cool and it helps me out a lot with the scripting in regards to timing and start of day. However, Why wouldn't I be able to put this in a scan? I replaced the label with a "plot scan = vol_ismore;" and it doesn't return anything. But I can use it as a strategy on the chart and it works like a charm.

Thanks again, Daniel
i don't think 2nd agg is allowed in scan scripts.
 
there is an error on line 7 and line 14
i am away from my computer, just typing into a text editor on my phone....
i hope this works or is close enough to get you what you want.


=== EDIT =====
well my original post had a few more errors than i thought. missing ; and def


here is the fixed code.
pick a time period with enough days to pull the data.

Code:
# volumefirstbar_xdays

# look at prev 20 days,
# read volume of 1st bar,
# add them up and calc an average,
# check if todays 1st bar vol > than the avg vol

#prev 20 days, exclude current day
input dayz = 20;
def bn = barnumber();

#---------------------------------------------
#firstbar , get from code in link below
#first last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
#---------------------------------------------

input agg = AggregationPeriod.DAY;

# dayz1st=if  !isnan(close(period = agg[-(dayz+1)] ) and isnan(close(period = agg)[-(dayz+2)] ) then 1 else 0;

# enable for valid period of days
def dayzen =
if   !isnan(close(period = agg)[-1] )
and
isnan(close(period = agg)[-(dayz+2)] )
then 1 else 0;

# vol of 1st bar
def vol1 = if bn == 1 then 0
else if (dayzen and firstBarOfDay) then volume else vol1[1];

# total vol in period
def vol1ttl = if bn == 1 then 0
else if ( dayzen and firstbarofday) then
vol1ttl[1] + volume
else vol1ttl[1];

def vol1avg = round(vol1ttl / dayz, 0);

def vol_ismore = ( vol1 > vol1avg);

addlabel(1,
"volume average of 1st bar, in " + dayz + " prev days, is " + vol1avg + ".  todays 1st bar vol is " + vol1,
(if vol_ismore then color.green else color.red) );
#
Hello,
Thanks for this script .I added this but some how the script is not showing current bar volume
Say for example in this TSLA example on 15minute chart first one bar volume is actually 2,459,654 but Script is displaying
1,129,989.

Please look into this and let me know if iam missing anything here

 
Hello,
Thanks for this script .I added this but some how the script is not showing current bar volume
Say for example in this TSLA example on 15minute chart first one bar volume is actually 2,459,654 but Script is displaying
1,129,989.

Please look into this and let me know if iam missing anything here

It is not possible to say where you went astray from the image.
If I were to hazard a guess I would say that you are erroneously comparing this thread's relative volume with actual volume

Some other guesses:
  • not having extended hours toggled on or off
  • wrong aggregation
  • attempting to access volume during non-market hours
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
161 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