Make the indexing operator ( [ ] ) skip extended-hours session bars, or alternative to shift and skip them?

Sabs

New member
I'm having trouble with a chart comparing volume to the average of the past 4 days, for the same bar of the day.

The code below is working without extended hours enabled but I'm trying to make it work with the extended session enabled. I've tried including the extended session bars using SPY as a test. Since SPY displays in ToS 24 hours, I set the length to 288 (24 hours * 12 five min bars/hr) but it's not calculating the average as expected.

I'd rather the extended market bars were ignored completely - different tickers have more/less extended session bars. I'm not sure if there's a way to skip those volume bars with the indexing operator ( [ ] ).

Any ideas would be greatly appreciated.

Code:
declare lower;
declare zerobase;

# each day's 6.5 hour session has (78) 5-minute bars
def length = 78;
input paintBars = no;

def isGreen = volume > ((volume[length]+volume[length*2]+volume[length*3]+volume[length*4])/4);
def isRed = volume < ((volume[length]+volume[length*2]+volume[length*3]+volume[length*4])/4);

plot Vol = volume;

Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(3);
Vol.DefineColor("Green", Color.DARK_GREEN);
Vol.DefineColor("Red", Color.DARK_RED);
Vol.AssignValueColor(if isGreen then Vol.Color("Green")  else if isRed then Vol.Color("Red") else Color.LIGHT_GRAY);

DefineGlobalColor("Green", Color.DARK_GREEN);
DefineGlobalColor("Red", Color.DARK_RED);
AssignPriceColor(if !paintBars then Color.CURRENT else if isGreen then GlobalColor("Green") else if isRed then GlobalColor("Red") else Color.LIGHT_GRAY);
 
I'm having trouble with a chart comparing volume to the average of the past 4 days, for the same bar of the day.

The code below is working without extended hours enabled but I'm trying to make it work with the extended session enabled. I've tried including the extended session bars using SPY as a test. Since SPY displays in ToS 24 hours, I set the length to 288 (24 hours * 12 five min bars/hr) but it's not calculating the average as expected.

I'd rather the extended market bars were ignored completely - different tickers have more/less extended session bars. I'm not sure if there's a way to skip those volume bars with the indexing operator ( [ ] ).

Any ideas would be greatly appreciated.

Code:
declare lower;
declare zerobase;

# each day's 6.5 hour session has (78) 5-minute bars
def length = 78;
input paintBars = no;

def isGreen = volume > ((volume[length]+volume[length*2]+volume[length*3]+volume[length*4])/4);
def isRed = volume < ((volume[length]+volume[length*2]+volume[length*3]+volume[length*4])/4);

plot Vol = volume;

Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(3);
Vol.DefineColor("Green", Color.DARK_GREEN);
Vol.DefineColor("Red", Color.DARK_RED);
Vol.AssignValueColor(if isGreen then Vol.Color("Green")  else if isRed then Vol.Color("Red") else Color.LIGHT_GRAY);

DefineGlobalColor("Green", Color.DARK_GREEN);
DefineGlobalColor("Red", Color.DARK_RED);
AssignPriceColor(if !paintBars then Color.CURRENT else if isGreen then GlobalColor("Green") else if isRed then GlobalColor("Red") else Color.LIGHT_GRAY);

take a look at the study joshua made
https://usethinkscript.com/threads/moving-average-excludes-extended-hours.14061/#post-117144

if a bar is during a desired period, then add up a variable. then divide the total by a number to get an average.
 

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

take a look at the study joshua made
https://usethinkscript.com/threads/moving-average-excludes-extended-hours.14061/#post-117144

if a bar is during a desired period, then add up a variable. then divide the total by a number to get an average.

Thanks for the response. I'm not very strong with code and I can't seem to wrap my head around the use of the Fold and how I could compare a specific bar from opening time. Any chance you'd be able to help with the code for this?

All other relative volume studies I've seen either compare to the previous x bars, or some sort of historical average.

To me, it seems most helpful to compare to the same bar on different days - for instance, the opening and closing bars of the day are often the highest, and comparing to any other bar is irrelevant.
 
Thread starter Similar threads Forum Replies Date
rottentrade How would you write "skip the first 5 opening bars (or 5 bars after the open)"? Questions 19

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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