Vol Avg only during Market open

naquan24

Member
Good afternoon....can you help me with this. I'm trying to have the volavg start to calculate at the start of the market open at 9:30am instead of the length which is set in the script as the last 20 bars. I don't want to use the premarket info in the volavg calculation. I'm trying to have the volavg calculate based on the times used in the input begin and till time.
Below is the script I'm using: You can see currently that the plot volavg is calculate based on the Average (volume, length). Is there a way I can change it?

declare lower;
declare Hide_On_Daily;


#Inputs
input begin = 0930;
input till = 1600;
def bars = if GetDay() == GetLastDay() and SecondsFromTime(begin) >= 0 and
SecondsTillTime(till) > 0
then bars[1] + 1
else bars[1];
def todayvol = if GetDay() == GetLastDay() and
SecondsFromTime(begin) >= 0 and SecondsTillTime(till) > 0
then todayvol[1] + volume
else todayvol[1];



def O = open;
def h = high;
def l = low;
def c = close;

def V = volume;
def buying = V*(C-L)/(H-L);
def selling = V*(H-C)/(H-L);


input price = close;
input fastLength = 5;
input slowLength = 13;
input long_average = 50;
input averageType = AverageType.EXPONENTIAL;
input length = 20;
plot VolAvg = Average(volume, length);
 
Solution
Sleepy thank you for the response...is there a way to use the previous day volume as the start of the average instead of the time of 9:30?
Can I edit the Getday info to look at the previous day trading info to calculate the average?

Here is the AvgVol during RTH's over a 2 day period

Ruby:
#Inputs

input begin = 0930;
input till = 1600;

#BarCounts
#Yesterday
def ybars = if GetDay() < GetLastDay() - 1
            then 0
            else if GetDay() == GetLastDay() - 1 and  SecondsFromTime(begin) >= 0 and
                    SecondsFromTime(till) < 0
            then ybars[1] + 1
            else ybars[1];
#Today - carries over Yesterday's last RTH barnumber to Beginning of RTH's Today
def tbars = if GetDay() < GetLastDay()...
Good afternoon....can you help me with this. I'm trying to have the volavg start to calculate at the start of the market open at 9:30am instead of the length which is set in the script as the last 20 bars. I don't want to use the premarket info in the volavg calculation. I'm trying to have the volavg calculate based on the times used in the input begin and till time.
Below is the script I'm using: You can see currently that the plot volavg is calculate based on the Average (volume, length). Is there a way I can change it?

declare lower;
declare Hide_On_Daily;


#Inputs
input begin = 0930;
input till = 1600;
def bars = if GetDay() == GetLastDay() and SecondsFromTime(begin) >= 0 and
SecondsTillTime(till) > 0
then bars[1] + 1
else bars[1];
def todayvol = if GetDay() == GetLastDay() and
SecondsFromTime(begin) >= 0 and SecondsTillTime(till) > 0
then todayvol[1] + volume
else todayvol[1];



def O = open;
def h = high;
def l = low;
def c = close;

def V = volume;
def buying = V*(C-L)/(H-L);
def selling = V*(H-C)/(H-L);


input price = close;
input fastLength = 5;
input slowLength = 13;
input long_average = 50;
input averageType = AverageType.EXPONENTIAL;
input length = 20;
plot VolAvg = Average(volume, length);

This uses the bars during regular trading hours to compute the VolAvg. There is a test with a bubble and label.

Screenshot-2022-10-25-125925.png
Ruby:
declare lower;
declare hide_on_daily;

#Inputs

input begin = 0930;
input till = 1600;
def bars = if GetDay() == GetLastDay() and SecondsFromTime(begin) >= 0 and
SecondsTillTime(till) > 0
then bars[1] + 1
else bars[1];
def todayvol = if GetDay() == GetLastDay() and
SecondsFromTime(begin) >= 0 and SecondsTillTime(till) > 0
then todayvol[1] + volume
else todayvol[1];



def O = open;
def h = high;
def l = low;
def c = close;

def V = volume;
def buying = V * (c - l) / (h - l);
def selling = V * (h - c) / (h - l);


input price = close;
input fastLength = 5;
input slowLength = 13;
input long_average = 50;
input averageType = AverageType.EXPONENTIAL;
input length = 20;

def volsum  = if bars == 1 then volume
              else if SecondsTillTime(till) >= 0
              then volsum[1] + volume
              else Double.NaN;
plot VolAvg = volsum / bars;

input test = yes;
AddLabel(test, "VolAvg: " + Round(VolAvg, 0), Color.YELLOW);
AddChartBubble(yes, VolAvg, volume + "\n" + volsum + "\n" + Round(VolAvg, 0), Color.GRAY, yes);
 
Sleepy thank you for the response...is there a way to use the previous day volume as the start of the average instead of the time of 9:30?
Can I edit the Getday info to look at the previous day trading info to calculate the average?
 
Last edited:
Sleepy thank you for the response...is there a way to use the previous day volume as the start of the average instead of the time of 9:30?
Can I edit the Getday info to look at the previous day trading info to calculate the average?

Here is the AvgVol during RTH's over a 2 day period

Ruby:
#Inputs

input begin = 0930;
input till = 1600;

#BarCounts
#Yesterday
def ybars = if GetDay() < GetLastDay() - 1
            then 0
            else if GetDay() == GetLastDay() - 1 and  SecondsFromTime(begin) >= 0 and
                    SecondsFromTime(till) < 0
            then ybars[1] + 1
            else ybars[1];
#Today - carries over Yesterday's last RTH barnumber to Beginning of RTH's Today
def tbars = if GetDay() < GetLastDay() or
               GetDay() == GetLastDay() and SecondsFromTime(begin) < 0
            then 0
            else if GetDay() == GetLastDay() and SecondsFromTime(begin) == 0
            then ybars[1] + 1
            else if  SecondsFromTime(till) < 0
            then tbars[1] + 1
            else tbars[1];

#Volume Counts
#Yesterday
def yvol = if GetDay() < GetLastDay() - 1
           then 0
           else if GetDay() == GetLastDay() - 1 and  SecondsFromTime(begin) >= 0 and
                   SecondsFromTime(till) < 0
           then yvol[1] + volume
           else yvol[1];
#Today - carries over Yesterday's last RTH cumlative volume to Beginning of RTH's Today
def tvol = if GetDay() < GetLastDay() or
              GetDay() == GetLastDay() and SecondsFromTime(begin) < 0
           then 0
           else if GetDay() == GetLastDay() and SecondsFromTime(begin) == 0
           then yvol[1] + volume
           else if  SecondsFromTime(till) < 0
           then tvol[1] + volume
           else tvol[1];

#Last 2 day AvgVol
AddLabel(1, "AvgVol over 2 Days: " + Round(tvol / tbars, 0), Color.YELLOW);

#Test
input test = yes;

AddChartBubble(test and GetDay() == GetLastDay() - 1, low, yvol + "\n" + ybars + "\n" + Round(yvol / ybars, 0), Color.GRAY, no);
AddChartBubble(test and GetDay() == GetLastDay(), low, tvol + "\n" + tbars + "\n" + Round(tvol / tbars, 0), Color.GRAY, no);
 
Solution
Here is the AvgVol during RTH's over a 2 day period

Sorry, that was way overcomplicated. This is better if you are wishing to include the last day as part of the average. You can then change the input lookback to include other days that are displayed on your chart.

Screenshot-2022-10-27-150945.png
Ruby:
def active     = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()),
                         RegularTradingEnd(GetYYYYMMDD()));
input lookback = 2;
def start      = if GetDay() >= GetLastDay() - lookback and active
                 then start[1] + 1
                 else start[1];
def vol        = if GetDay() >= GetLastDay() - lookback and active
                 then vol[1] + volume
                 else vol[1];
addlabel(1, lookback +" day VolAvg: " + Round(vol / start, 0), color.yellow);

input test = yes;
AddChartBubble(test and start and active, low * .995, volume + "\n" + vol + "\n" + start + "\n" + Round(vol / start, 0), Color.GRAY, no);
 
Here is the AvgVol during RTH's over a 2 day period
SleepyZ thank you for this....question...instead of the gray chart bubbles....can I have it plot as a yellow line. I'm trying to have the yellow line go over the volume bars to show where there is high volume over the average.

Also in the formula, the lookback =2. Can you confirm that means the lookback is 2 days. If so, if I open it on Tuesday, Will it plot the average volume from Monday (the previous day) and friday from the week before?
 
Last edited:
SleepyZ thank you for this....question...instead of the gray chart bubbles....can I have it plot as a yellow line. I'm trying to have the yellow line go over the volume bars to show where there is high volume over the average.

Also in the formula, the lookback =2. Can you confirm that means the lookback is 2 days. If so, if I open it on Tuesday, Will it plot the average volume from Monday (the previous day) and friday from the week before?

I have added an AvgVol yellow line. Move this script to the volume panel. Do not choose to overlap volume to the price channel.

I used getday() as the lookback in the above. I have changed it to getyyyymmdd() as the basis below. If you are on Tuesday and input a lookback of 2, it will include Friday and Monday as well as the current day, Tuesday in this case.

Here is chart image with Tuesday as the current day and a lookback of 2 days.

Screenshot-2022-11-04-105718.png
Ruby:
def active     = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()),
                         RegularTradingEnd(GetYYYYMMDD()));
input lookback = 2;
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start    = if thisDay <= lookback and active
               then start[1] + 1
               else start[1];
def vol      = if thisDay <= lookback and active
               then vol[1] + volume
               else vol[1];
AddLabel(1, lookback + " day VolAvg: " + Round(vol / start, 0), Color.YELLOW);
plot avgvol   = Round(vol / start, 0);
avgvol.SetDefaultColor(Color.YELLOW);
input test = no;
AddChartBubble(test and start and active, volume, volume + "\n" + vol + "\n" + start + "\n" + Round(vol / start, 0), Color.GRAY, yes);
 
Last edited:
Thank you SleepyZ.....unfortunately it doesn't work but I see why. So I have a question....in the script you sent me, I see you were able indicate that you want regular trading day.
Is there a way I can have the lookback period be the length of bars along with the regular trading day. Or simply can I have it say lookback 20 bars from yesterday regular trading day?
 
Thank you SleepyZ.....unfortunately it doesn't work but I see why. So I have a question....in the script you sent me, I see you were able indicate that you want regular trading day.
Is there a way I can have the lookback period be the length of bars along with the regular trading day. Or simply can I have it say lookback 20 bars from yesterday regular trading day?
I do not understand why it does not work. I have provided 2 different scripts to do the same thing. One is more simply done is all.
 
This uses the bars during regular trading hours to compute the VolAvg. There is a test with a bubble and label.
Sleepyz this script is helpful but calculating the average based on todays volume. Is there a way I can adjust it so it starts of by using the average of the previous day volume. I wanted to use the last 20 days bars of the previous RTH. Is that possible?
 
Sleepyz this script is helpful but calculating the average based on todays volume. Is there a way I can adjust it so it starts of by using the average of the previous day volume. I wanted to use the last 20 days bars of the previous RTH. Is that possible?

That is the first script I posted in #2. The subsequent scripts in #3 and #4 include prior days. The script in #7 included the volavg line you requested. Below is that script with an input lookback set to 20 for last 20 days of RTH data. You must have those days on the chart you are viewing.

Screenshot-2022-11-11-030007.png
Ruby:
def active     = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()),
                         RegularTradingEnd(GetYYYYMMDD()));
input lookback = 20;
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start    = if thisDay <= lookback and active
               then start[1] + 1
               else start[1];
def vol      = if thisDay <= lookback and active
               then vol[1] + volume
               else vol[1];
AddLabel(1, lookback + " day VolAvg: " + Round(vol / start, 0), Color.YELLOW);
plot avgvol   = Round(vol / start, 0);
avgvol.SetDefaultColor(Color.YELLOW);
input test = no;
AddChartBubble(test and start and active, volume, volume + "\n" + vol + "\n" + start + "\n" + Round(vol / start, 0), Color.GRAY, yes);
 

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