# Opening Volume vs 30d Average Volume

#### C4men

##### Member
I seem to be stuck. My code works in that it does two things:
• Calculates the average volume of the last 30 days
• Grabs the volume from the current day (first 20 minutes) and compares it to the 30d average (as a label)
It works perfectly on my chart. However, I want to scan intraday for the tickers where that OpenVol is greater than 20%. Something like:
• CF_OpenVolume()."OpenVol" is greater than or equal to 20
When I go to scan, it returns stocks that do not meet the criteria. It forces me to use 'D', otherwise giving me a secondary aggregation period error.

Is there any hope to edit the code to allow it to work on the chart (as it currently is) AND return results intraday for OpenVol > X?

Code:
``````## DEF Volume Data
def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);

### NEW LINE ###
plot TodayPctVs30Day = percentOf30Day;

## OPENING X MIN VOLUME
input startTime1 = 0930;
input endTime1 = 0949;

def Active1 = SecondsFromTime(startTime1) >= 0 and SecondsTillTime(endTime1) >= 0;
def VolMins1 = if Active1 and !Active1[1] then volume
else if Active1 then VolMins1[1] + volume
else VolMins1[1];
plot OpenVol = Round((VolMins1 / volLast30DayAvg) * 100, 0);
#AddLabel(1, "OpenVol: " + VolMins1 + " :: [" + OpenVol + "%] ", Color.CYAN);
AddLabel(1, "OpenVol: " + "[" + OpenVol + "%] " + "/ " + "Pct30d: " + "[" + TodayPctVs30Day + "%] ", Color.CYAN);``````

Solution
so heres the good news... it got it working for you! here you go:
Keep in mind the time interval (value between starttime and endtime must be a multiple of your chart time/aggregation)
as noted, because of the aggregation limitation and error in TOS extended intraday data the daily average volume might have a margin of error by about 1% which is almost nothing because you are averaging anyways.

Cheers, Happy New Years!

Volume at Specified Time Interval Compared to Daily Average

Code:
``````#Volume at Specified Time Interval Compared to Daily Average
###Please Keep Code Intact if you share
### By XeoNoX via usethinkscript.com
##Note time interval (value between starttime and endtime must be a...``````

#### XeoNoX

##### Well-known member
VIP
the first 10 minute total volume or the 1st 10 minute avg volume?

#### C4men

##### Member
the first 10 minute total volume or the 1st 10 minute avg volume?

For the 30D Average Volume, it would be the total volume for each day.

For the Opening Volume, it would be the first X minutes of a given/current day.

Trying to see where there is concentrated, heavy volume in the first 20 or so minutes compared to the total volume the stock has averaged over the last 30 days. So not comparing against yesterday's first 20 or anything like that. Comparing the first 20 today against the average.

So if XYZ has averaged 1,000,000 total per day over the last 30 days and suddenly today the volume in the first 20 minutes is 200k, something is happening (worth watching).

I just happen to typically use the 10min timeframe on my chart when I day trade.

#### XeoNoX

##### Well-known member
VIP
ok, ive tried all kind of variations shy of manually adding up bars with a calculator. I need to be able to get the days volume by lower aggregation, however im not sure how they OFFICIALLY tally up the final volume of the day. im not sure if im not fomulating it correctly or if TOS doesnt let me do it, but before i go any further I need you to find out how end of day volume is OFFICIALLY counted from:

beginning of premarket to end of afterhours
beginning of premarket to end of regular trading hours (closing bell)
beginning of regular trading hours (opening bell ) to end of regular trading hours (closing bell)
beginning of regular trading hours (opening bell ) to end of afterhours

#### XeoNoX

##### Well-known member
VIP
you want the good news or the bad news first?
pick only one

and by way, on any sp500 ticker, add up the 30 min volume total, and then get the 4hr volume total... i used AAPL... then compare what you added to the DAY total volume when u switch to DAY chart --- yes i actually did this, it was driving me nuts, lol... compare the 4hr and Day vol to another reliable data source.

Last edited:

#### C4men

##### Member
you want the good news or the bad news first?
pick only one

and by way, on any sp500 ticker, add up the 30 min volume total, and then get the 4hr volume total... i used AAPL... then compare what you added to the DAY total volume when u switch to DAY chart --- yes i actually did this, it was driving me nuts, lol... compare the 4hr and Day vol to another reliable data source.

#### XeoNoX

##### Well-known member
VIP
if you tried what i mentioned, you will notice the data feed for volume is off intraday from the OFFICIAL total volume given end of day. hence the final days volume average is slightly off because tos doesn't allow you to use lower aggregation than the highest aggregation used in the scan, therefore you are stuck with a slightly off number when tallying the end of day volume using anything lower than DAY aggregation. (this is something i just learned from doing this) TOS data is slightly off. this only matters if you are going to scan against, if you are just going to use for charting label this wont matter.

Are you ok with this?

note you will have to pick between the following:

exact data but only as a label
or
slightly off "Daily AVG" data however you will be able to scan against.

let me know what you decide

Last edited:

#### C4men

##### Member
@XeoNoX Slightly off to scan against. No doubt!

#### XeoNoX

##### Well-known member
VIP
so heres the good news... it got it working for you! here you go:
Keep in mind the time interval (value between starttime and endtime must be a multiple of your chart time/aggregation)
as noted, because of the aggregation limitation and error in TOS extended intraday data the daily average volume might have a margin of error by about 1% which is almost nothing because you are averaging anyways.

Cheers, Happy New Years!

Volume at Specified Time Interval Compared to Daily Average

Code:
``````#Volume at Specified Time Interval Compared to Daily Average
###Please Keep Code Intact if you share
### By XeoNoX via usethinkscript.com
##Note time interval (value between starttime and endtime must be a multiple of your chart time/aggregation)
input StartTime = 0930;
input Endtime = 940;
input Day_Average = 30;
def na = Double.NaN;
def today=getday()==getlastDay();
def SelectedTime = if GetLastDay() == GetDay() and SecondsFromTime(StartTime) >= 0 and SecondsFromTime(Endtime) < 0 then 1 else 0;
def Selected_Volume = TotalSum(if SelectedTime  then volume else 0);
def timedif = absValue(StartTime-endtime);
def total_min_vol_sum =totalsum(if today then (volume) else 0);
def avg30 = round(average(total_min_vol_sum,Day_Average),0);
def PCT_Open = (Selected_Volume / avg30);

AddLabel (yes, timedif + "Min Vol= " + Selected_Volume);
AddLabel (yes, Day_Average + "D Average= " + avg30);
AddLabel (yes, "Percentage Dif= " + aspercent(PCT_Open));``````

Last edited:

#### C4men

##### Member
@XeoNoX You're a life saver!!! I'll be testing tomorrow. THANK YOU.

#### XeoNoX

##### Well-known member
VIP
enjoy, i updated the info on POST #28

#### minime

##### New member
VIP
Opening 30 minutes average VOL for past 30 days

Opening 30 minutes is usually most active, very useful indicator for intraday trading.

I can get the current day open 30 minutes but don't know how to to get the last 30 days so I can average them.
Here is the code I use for the current day open.

Code:
``````input startTime = 0930;
input endTime = 1000;

def Active = SecondsFromTime(startTime) >= 0 and SecondsTillTime(endTime) >= 0;
def Vol30Mins = if Active and !Active[1] then volume
else if Active then Vol30Mins[1] + volume
else Vol30Mins[1];``````

Last edited:

#### minime

##### New member
VIP
Ahhh... Mis-read on my part... So, you want each of the past 30 days, individually, or the average of the past 30 days...???
average of the past 30 days.
I trade one-minute chart, so I want the past 30 days opening 30 minutes volume average out, then divided by 30 so I can compare the current vol bar to the past 30 days. Basically, an indicator if big vol moving at the open.

Last edited:

VIP

#### XeoNoX

##### Well-known member
VIP
Thx but the code suppose to work doesn't work intraday. All I need is Opening 30 minutes average VOL for past 30 days
looks like it would work to me, you just need to add the label and use the part that you need. and dived by 30. but the main code is there on that page. adds up the intraday timeframe you want of the past XYZ days. the only thing you have to do is average it by dividing by in your case 30 days. NOTE: it will only calculate what it can see on your screen, so if your chart doestnt go back 30 days neither will the script.

#### minime

##### New member
VIP
looks like it would work to me, you just need to add the label and use the part that you need. and dived by 30. but the main code is there on that page. adds up the intraday timeframe you want of the past XYZ days. the only thing you have to do is average it by dividing by in your case 30 days. NOTE: it will only calculate what it can see on your screen, so if your chart doestnt go back 30 days neither will the script.
Can you show me? Thank you.
Code:
``````# This is the average cumulative volume over the same time period from the previous 10 days
def cumVolumePrev = if IsActive and !IsActive[1]
then (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else if IsActive then cumVolumePrev[1] + (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else cumVolumePrev[1];``````

Last edited:

#### XeoNoX

##### Well-known member
VIP
That's the one, read the script BPD is the number of bars per day, youde have to count how many bars in your timeframe during regular trading hours, its important to the script if you want proper calculation. It's pretty self explanatory if you TRY.

Just make `volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1]` and repeat the cycle till you hit 30 since you want 30...then count number of bars in 1 trading day for "BPD" then just add divide by 30 instead of 10 for the average.

3 simple steps, the main code is there, its simple however time consuming since you want 30 days.

#### minime

##### New member
VIP
So for testing purposes, say for 10 days average.
All I need is to change BPD to 30 since I want opening 30 minutes vol, and start end time to 0930-1000?
and change my chart to 10 day1 minute? Like this ?
Code:
``````input STime = 0930 ; #hint STime: Start of normal trading hours
input ETime = 1000 ; #hint ETime: End of normal trading hours
def bpd = 30 ; # Bars per day during active trading hours
def daycnt = 10; # Number of days

def IsActive = if secondsTillTime(ETime) > 0 and
secondsFromTime(STime) >= 0
then 1
else 0;

# This is the average cumulative volume over the same time period from the previous 10 days
def cumVolumePrev = if IsActive and !IsActive[1]
then (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else if IsActive then cumVolumePrev[1] + (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else cumVolumePrev[1];

AddLabel(1, "Vol 30Mins per Min  = " + cumVolumePrev/30, Color.YELLOW);``````

I test it the number doesn't look right. I tried NIO it shows opening 30 mins average per min for past 10 days only 10060, normal value should be around 856000. What went wrong here?

Last edited:

#### KELTON98

##### New member
Im looking to do something similar to the above. Im looking to create a code to identify stock where the current volume is greater than the 60 day average volume by x percent.
Thanks for any help!!

#### XeoNoX

##### Well-known member
VIP
Im looking to do something similar to the above. Im looking to create a code to identify stock where the current volume is greater than the 60 day average volume by x percent.
Thanks for any help!!

Current Volume is XYZ Percent greater than the Average of the past 60 volume bars
Change 1 to your desired percent
Code:
``````def compared_to = average(volume,60);
def percent = (volume/ compared_to) * 100;
plot scan = percent > 1 ;``````

Last edited: