How to get after hours trading volume

Solution
Hi, I would like to get the after hours trading volume, and I found the link below from July 2022, but it does not work properly.

Does anyone know how to do this?

Thanks in advance!

https://usethinkscript.com/threads/afterhours-volume-scan.12019/#post-105521

what is the after hours?
4pm to 8pm?
4am to 9:30 and 4pm to 8pm?
i used both time periods and added up the volume in each period.
has labels to list the accumulated volume in all 3 time periods

Code:
#after_hours_vol

#https://usethinkscript.com/threads/how-to-get-after-hours-trading-volume.20098/
#How to get after hours trading volume

def na = double.nan;
def bn = barnumber();
def v = volume;

input prestart = 0400;
input daystart = 0930;
input dayend =...
Hi, I would like to get the after hours trading volume, and I found the link below from July 2022, but it does not work properly.

Does anyone know how to do this?

Thanks in advance!

https://usethinkscript.com/threads/afterhours-volume-scan.12019/#post-105521

what is the after hours?
4pm to 8pm?
4am to 9:30 and 4pm to 8pm?
i used both time periods and added up the volume in each period.
has labels to list the accumulated volume in all 3 time periods

Code:
#after_hours_vol

#https://usethinkscript.com/threads/how-to-get-after-hours-trading-volume.20098/
#How to get after hours trading volume

def na = double.nan;
def bn = barnumber();
def v = volume;

input prestart = 0400;
input daystart = 0930;
input dayend = 1600;
input afterend = 2000;

def pre =  (SecondsFromTime(prestart) >= 0 and SecondsTillTime(daystart) > 0);
def daytime = (SecondsFromTime(daystart) >= 0 and SecondsTillTime(dayend) > 0);
def after = (SecondsFromTime(dayend) >= 0 and SecondsTillTime(afterend) > 0);

def prevol = if !pre[1] and pre then v else if pre then prevol[1] + v else prevol[1];
def dayvol = if !daytime[1] and daytime then v else if daytime then dayvol[1] + v else dayvol[1];
def aftervol = if !after[1] and after then v else if after then aftervol[1] + v else aftervol[1];


addlabel(1, "  ", color.black);
addlabel(1, "PRE volume: " + prevol, color.cyan);
addlabel(1, "DAY volume: " + dayvol, color.green);
addlabel(1, "AFTER volume: " + aftervol, color.magenta);
addlabel(1, "  ", color.black);


input test_time_lines = no;
addverticalline(test_time_lines and pre, "PRE", color.cyan);
addverticalline(test_time_lines and daytime, "DAY", color.green);
addverticalline(test_time_lines and after, "AFTER", color.magenta);

input test_volume_lines = no;
plot z1 = if test_volume_lines then prevol else na;
plot z2 = if test_volume_lines then dayvol else na;
plot z3 = if test_volume_lines then aftervol else na;
#
 
Solution
what is the after hours?
4pm to 8pm?
4am to 9:30 and 4pm to 8pm?
i used both time periods and added up the volume in each period.
has labels to list the accumulated volume in all 3 time periods

Code:
#after_hours_vol

#https://usethinkscript.com/threads/how-to-get-after-hours-trading-volume.20098/
#How to get after hours trading volume

def na = double.nan;
def bn = barnumber();
def v = volume;

input prestart = 0400;
input daystart = 0930;
input dayend = 1600;
input afterend = 2000;

def pre =  (SecondsFromTime(prestart) >= 0 and SecondsTillTime(daystart) > 0);
def daytime = (SecondsFromTime(daystart) >= 0 and SecondsTillTime(dayend) > 0);
def after = (SecondsFromTime(dayend) >= 0 and SecondsTillTime(afterend) > 0);

def prevol = if !pre[1] and pre then v else if pre then prevol[1] + v else prevol[1];
def dayvol = if !daytime[1] and daytime then v else if daytime then dayvol[1] + v else dayvol[1];
def aftervol = if !after[1] and after then v else if after then aftervol[1] + v else aftervol[1];


addlabel(1, "  ", color.black);
addlabel(1, "PRE volume: " + prevol, color.cyan);
addlabel(1, "DAY volume: " + dayvol, color.green);
addlabel(1, "AFTER volume: " + aftervol, color.magenta);
addlabel(1, "  ", color.black);


input test_time_lines = no;
addverticalline(test_time_lines and pre, "PRE", color.cyan);
addverticalline(test_time_lines and daytime, "DAY", color.green);
addverticalline(test_time_lines and after, "AFTER", color.magenta);

input test_volume_lines = no;
plot z1 = if test_volume_lines then prevol else na;
plot z2 = if test_volume_lines then dayvol else na;
plot z3 = if test_volume_lines then aftervol else na;
#
after_hours_vol #https://usethinkscript.com/threads/how-to-get-after-hours-trading-volume.20098/ #How to get after hours trading volume def na = double.nan; def bn = barnumber(); def v = volume; input prestart = 0400; input daystart = 0930; input dayend = 1600; input afterend = 2000; def pre = (SecondsFromTime(prestart) >= 0 and SecondsTillTime(daystart) > 0); def daytime = (SecondsFromTime(daystart) >= 0 and SecondsTillTime(dayend) > 0); def after = (SecondsFromTime(dayend) >= 0 and SecondsTillTime(afterend) > 0); def prevol = if !pre[1] and pre then v else if pre then prevol[1] + v else prevol[1]; def dayvol = if !daytime[1] and daytime then v else if daytime then dayvol[1] + v else dayvol[1]; def aftervol = if !after[1] and after then v else if after then aftervol[1] + v else aftervol[1]; addlabel(1, " ", color.black); addlabel(1, "PRE volume: " + prevol, color.cyan); addlabel(1, "DAY volume: " + dayvol, color.green); addlabel(1, "AFTER volume: " + aftervol, color.magenta); addlabel(1, " ", color.black); input test_time_lines = no; addverticalline(test_time_lines and pre, "PRE", color.cyan); addverticalline(test_time_lines and daytime, "DAY", color.green); addverticalline(test_time_lines and after, "AFTER", color.magenta); input test_volume_lines = no; plot z1 = if test_volume_lines then prevol else na; plot z2 = if test_volume_lines then dayvol else na; plot z3 = if test_volume_lines then aftervol else na; #

Thanks very much for the code! It is a good example for me to learn from.

Also, I want to add a watchlist column so I will be outputting aftervol after it is calculated and don't need the rest of the code for the column. But aftervol is printed without commas and in scientific notation if it is too large.

Can you tell me how to output as a comma separated number?
 
Thanks to halcyonguy for the excellent code. I did notice that there was a value for the after hours volume before after hours had started, so I made the small change to the aftervol assignment below.

def aftervol = if !after then 0 else if !after[1] and after then v else if after then aftervol[1] + v else aftervol[1];
 
Thanks very much for the code! It is a good example for me to learn from.

Also, I want to add a watchlist column so I will be outputting aftervol after it is calculated and don't need the rest of the code for the column. But aftervol is printed without commas and in scientific notation if it is too large.

Can you tell me how to output as a comma separated number?
for volume numbers (millions), i usually divide by a million, then round it to 2 or 3 places
if i am comparing numbers in the millions, i don't care about 1s,10s,100s, or 1000s places

i kept volume values after the last bar in a period, so those values were usable and would show up in a label (data from last bar). on the first bar in a period, it resets the volume with volume just on that bar. then during a period, it adds up.
 

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