Workaround for excluding premarket bars when using Barnumber()? Want to get % of trading session elapsed with 1min chart as "(CurrentBarNumber/390);"

Glefdar

Active member
EDIT 3: The thread I linked below in the second edit had the solution:

Code:
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART WITHIN SPECIFIED TIMEFRAME
# By XeoNoX via Usethinkscript.com
input startTime = 0930;
input endTime = 1600;
def Active = SecondsFromTime(startTime) >= 0 and SecondsTillTime(endTime) >= 0;
def var = close>0;
def cumulative = if Active and !Active[1] then var else if Active then compoundValue(1,  (cumulative[1] + var), 0) else cumulative[1];

Then it was simply a matter of making this replacement for the commented out plot:

Code:
#plot scan  = cumulative;

plot barpercent = if active is true then (cumulative/390)*100 else double.nan;

Hi all,

At first I was trying to use SecondsFromTime and SecondsTillTime to do this, but I think it would be less CPU intensive to use BarNumber() for the purposes of calculating what % of the trading session has elapsed on a 1min chart.

For example, if CurrentBar() == 5, then I want that to ideally be the fifth minute into the trading session on a 1min chart. (I realize that using BarNumber() will return false values on highly illiquid stocks that have less than 1 tick occurring per minute, but I think that's an acceptable tradeoff.) Then I want to be able to calculate "CurrentBar()/390" to get the percentage of the trading session (390 minutes) that has elapsed (i.e. how far into the trading day is it, percentage-wise).

However, I can't figure out how to exclude premarket bars without entirely disabling extended hours from appearing on my chart (which I don't want to do). So if there were 15 candles from 4am to 9:29am then BarNumber is returning 16 at 9:30am instead of returning 1.

Is there any workaround? If there was a way to count a running tally of the bars between specified times that would work, for example, but I can't see how that would be possible. Maybe it would be possible in a really convoluted way? For example:

"if secondsfromtime(0400)>0 and secondstilltime(0930)>0 and BarNumber()==15 then MarketOpenBarCount = BarNumber()-15"

But I think that doesn't work because the secondsfromtime and secondstilltime functions only register based on the current time (not making a record of what was conditionally true in hours before the current time)? Since the "seconds" counting functions don't seem to work for historical plotting on intraday charts.

That is, it seems impossible to just plot a line of the seconds that have passed starting from a particular time. For example, this code:

Code:
plot data = secondsfromtime(0930);

It plots this (!?):

N3ZqhEI.jpg


That's kind of a side tangent but it's the reason why I am trying to use BarNumber() as a workaround for solving this instead of using SecondsFromTime.

Is there any solution for plotting how much time has passed from "X" point or how much time has transpired as a percentage of the total time in a trading session?

EDIT: I think I found something that might give an answer https://usethinkscript.com/threads/consecutive-bar-count-indicator-for-thinkorswim.324/ I'm not sure if this will solve my question, it's hard for me to wrap my head around but I'll play around with it.

EDIT 2: I just found this thread which I think will be more helpful: https://usethinkscript.com/threads/...-bars-on-entire-chart-or-specified-time.5496/
 
Last edited:
Have you found an answer for this yet?

You can reduce the cpu load using your original seconds from/till calculation by adding this:
Code:
declare once_per_bar;
at the top of your script.
You can also create a bar counter manually along these lines:
Code:
declare lower;
def today_bar_count = if secondsTILLTime(0930) crosses below 0 then 1 else today_bar_count[1] + 1;
plot data = today_bar_count;

Hope that helps,
mashume
 
Have you found an answer for this yet?

You can reduce the cpu load using your original seconds from/till calculation by adding this:
Code:
declare once_per_bar;
at the top of your script.
You can also create a bar counter manually along these lines:
Code:
declare lower;
def today_bar_count = if secondsTILLTime(0930) crosses below 0 then 1 else today_bar_count[1] + 1;
plot data = today_bar_count;

Hope that helps,
mashume
This is quite interesting thanks. I am actually using this as part of a larger study integration. The other components of the study would be affected by using declare once per bar. But that raises the question for me of when the study calculates during the bar. The description of the function is:

"Changes the recalculation mode of a study. By default, last study values are recalculated after each tick. If this declaration is applied, the study is forced to recalculate the last values only once per bar. This declaration can be used to reduce CPU usage for studies which do not need to be recalculated per each tick."

For example, for the first 1min bar of market hours, will the value of the study get calculated (a) at 1 millisecond after 9:30am or (b) does it calculate at 1 millisecond before 9:31am? One interesting advantage of using once per bar is that the bar value will not repaint... From that standpoint maybe it would be ideal if the value of the study calculated (c) at 500 milliseconds after 9:30am so that it is more representative of the value that occurs during the bar period, but I imagine that (a) or (b) is more likely what is happening. Do you know?
 
This is quite interesting thanks. I am actually using this as part of a larger study integration. The other components of the study would be affected by using declare once per bar. But that raises the question for me of when the study calculates during the bar. The description of the function is:

"Changes the recalculation mode of a study. By default, last study values are recalculated after each tick. If this declaration is applied, the study is forced to recalculate the last values only once per bar. This declaration can be used to reduce CPU usage for studies which do not need to be recalculated per each tick."

For example, for the first 1min bar of market hours, will the value of the study get calculated (a) at 1 millisecond after 9:30am or (b) does it calculate at 1 millisecond before 9:31am? One interesting advantage of using once per bar is that the bar value will not repaint... From that standpoint maybe it would be ideal if the value of the study calculated (c) at 500 milliseconds after 9:30am so that it is more representative of the value that occurs during the bar period, but I imagine that (a) or (b) is more likely what is happening. Do you know?
While I don't actually know the answer, I would bet that it is not based on time past the start of a bar but rather on the first tick (trade) of a bar. Remember that bars aren't drawn, even on time charts, if there are no trades that took place. A bar is only drawn when there is a trade. So imagine an illiquid asset which trades in very small volumes. If you're looking at 1 minute candles, there may only be 10 candles drawn for the day (for example look at BRK/A -- 10 is an exaggeration, but the volumes are REALLY SMALL). The calculation would be made after the first trade that falls within a new candle. Don't count on ToS to be time based (ms after event etc...) as it isn't really.

happy trading,
mashume
 
While I don't actually know the answer, I would bet that it is not based on time past the start of a bar but rather on the first tick (trade) of a bar. Remember that bars aren't drawn, even on time charts, if there are no trades that took place. A bar is only drawn when there is a trade. So imagine an illiquid asset which trades in very small volumes. If you're looking at 1 minute candles, there may only be 10 candles drawn for the day (for example look at BRK/A -- 10 is an exaggeration, but the volumes are REALLY SMALL). The calculation would be made after the first trade that falls within a new candle. Don't count on ToS to be time based (ms after event etc...) as it isn't really.

happy trading,
mashume

Very interesting, that makes sense. So for a high liquidity stock that has at least 1 tick per second, a study using declare once_per_bar; would paint and stop painting one second after the previous bar closed. Thanks for your insight.
 

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