EDIT 3: The thread I linked below in the second edit had the solution:
Then it was simply a matter of making this replacement for the commented out plot:
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:
It plots this (!?):
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/
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 (!?):
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: