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

1gUNz71.png


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

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

Not going to work as a intraday scanner because you have multiple aggregation periods in your script.
That's what I was worried about. Do you think there is a way to get the 30 day average volume AND the first 20 minutes volume so they can be compared? Maybe two separate scripts?
 
You have to set the aggregation to day in the scan…I tested it and a few results came back, I didn't really study your code so I have no clue what it does.

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 ###
def 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];
def OpenVol = Round((VolMins1 / volLast30DayAvg) * 100, 0);

plot scan = openvol > 20;
 
@XeoNoX Appreciate the reply. I'm still stuck, but trying to figure it out. I am trying to do three things:

1. Calculate the average volume of the last 30 days - DONE
2. Calculate the volume of the first 20 minutes of the day - DONE
3. Scan when #2 is 20% or greater than #1 - SEEMS IMPOSSIBLE

#1 is daily, #2 is intraday. I cannot figure out how to make the two work together.
 
@XeoNoX Appreciate the reply. I'm still stuck, but trying to figure it out. I am trying to do three things:

1. Calculate the average volume of the last 30 days - DONE
2. Calculate the volume of the first 20 minutes of the day - DONE
3. Scan when #2 is 20% or greater than #1 - SEEMS IMPOSSIBLE

#1 is daily, #2 is intraday. I cannot figure out how to make the two work together.
post the current code you have that you are trying to accomplish this with
 
post the current code you have that you are trying to accomplish this with

It's the same code as above, but here's the breakdown:

- I can get the average volume of the last 30 days
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;

- I can get the volume of the first X minutes (20 in this case)
Code:
### DEFINITIONS & PLOTS ###
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);
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: " + "[" + OpenVol + "%] " + "/ " + "Pct30d: " + "[" + TodayPctVs30Day + "%] ", Color.CYAN);

But what I want to then do is compare the "Opening 20 Min Volume" to the "30D average" and express as a percentage. That way, I could scan intraday to 'return all the stocks who had Open Volume in the first 20 minutes at 20% or greater than the 30-day average'.

In my trading, I have found this is a pretty good signal that there might be some good action here today.
 
#1) Is your label calculating everything the way you want it to calculate? in other words "openvol" returning what you want it to?

#2) you want to be able to scan against "openvol"??
 
#1) Is your label calculating everything the way you want it to calculate? in other words "openvol" returning what you want it to?

#2) you want to be able to scan against "openvol"??

#1 - The label works correctly, yes.
#2 - This is where the issue is. I essentially want to scan where today's OpenVol >= 20% of the 30d average

So, for example, let's say:
  • XYZ has 30d average volume of 1,000,000.
  • Today, OpenVol = 250,000 (or 25% of the 30 day average)
  • I would want the scan to return XYZ (since the OpenVol of 25% is greater than the criteria (20%) in the scan)

Thus far, I cannot figure out how to scan an intraday condition (OpenVol) against a daily one (30d average).
 
does your label already give the calculations for " today's OpenVol "??

It does. But that percentage does not seem to work in the scan. It will return stocks that do NOT meet at least that percent. Which is to say, if I set the scan to 20, it might return stocks with 5 or 9 or whatever.
 
Your first issue was the use of round in the code and then trying to scan against it, I fixed that as coded below

Your second issue is you are trying to scan with a DAY aggregation therefore you cant scan lower once you set the scanner to day. To fix this you need to use seconds from time and tally up the volume from premarket to afterhours and use that as the days volume, then you can use the "sum" feature to average it out..... (or you can add it like you did by adding 30 times then dividing:eek:)

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;


### DEFINITIONS & PLOTS ###
def today = volume(period = "DAY");
def percentOf30Day = ((today / volLast30DayAvg) * 100);
def 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];
def OpenVol = ((VolMins1 / volLast30DayAvg) *100 );


plot scan = openvol > 20;
 
Firstly - THANK YOU for helping. I think I am with you, but not sure.

To fix this you need to use seconds from time and tally up the volume from premarket to afterhours and use that as the days volume, then you can use the "sum" feature to average it out.....

How would I go about achieving that piece of it? Would something like this work?

plot volLast30DayAvg = Sum(volume, 30) / 30;

I feel I am not quite there lol.
 
yes... that's one way to do it... but instead of "volume" it has to be the syntax for whatever you used to capture the daily volume via secondsfromtime

Code:
plot volLast30DayAvg = Sum(syntax_for_daily_vol_via_seconds_from_time, 30) / 30;
 
I'm still struggling with this. Do you accept Venmo lol?

I would gladly throw you 20 to be able to make this work!
thanks for the offer, ill pass on the donations, donate it to your local privatized no kill animal shelter.
im a little tired today, i can help you with this later today or by eod tomorrow.
 
thanks for the offer, ill pass on the donations, donate it to your local privatized no kill animal shelter.
im a little tired today, i can help you with this later today or by eod tomorrow.

Will do! No rush at all. I have broken it into two studies (hoping that helps). Now I am down to calculating 30d average volume without using the 'DAY' aggregation.

Script 1 (Daily)

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;

plot AvgDailyVol_30 = volLast30DayAvg;

### DEFINITIONS & PLOTS ###
def today = volume(period = "DAY");
def AvgDailyVolPCT_Daily = Round((today / volLast30DayAvg) * 100, 0);


AddLabel(1, "Test Volume Daily: " + AvgDailyVolPCT_Daily, Color.LIGHT_GREEN);

Script 2 (Intraday - stuck)

Code:
#Opening Volume: Looking for >= 10%

## DEF OPENING X 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];

### DEFINITIONS & PLOTS ###
def today = volume(period = "DAY"); ## STUCK ##
def VolLast30DayAvg = Sum(today, 30)/ 30;

plot PCT_Open = Round((VolMins1 / volLast30DayAvg) * 100, 0);

AddLabel(1, "Test Volume Spike: " + PCT_Open , Color.LIGHT_GREEN);


Thanks for whatever help you are willing to provide. It's greatly appreciated!!!
 
what do you want "today" to do?

As I look at it, probably not necessary if the code changes. I thought I needed it to essentially do this:

Opening Volume ('volmins1') divided by Average Volume of Last 30 Days (calculated somehow in 10min agg.)

If I could somehow calculate the 30d average volume using the 10min aggregation (as opposed to 'DAY'), I think I'd be perfect.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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