# Number of bars since market opened

#### TNTOutburst

##### New member
I'm trying to make a study that tells me what percent of the time since the market last opened that a stock was increasing.
Code:
``````def pos = (Average(close, 8) / Average(close, 8)[6]) > 1;
def neg = (Average(close, 8) / Average(close, 8)[6]) < 1;
def percent = Sum(pos, 300) / (Sum(pos, 300) + Sum(neg, 300));``````
I made this code, but it just uses the last 300 bars.
Code:
``````def condition = if SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0 then barNumber() else 0;
def StartBar = condition;
def StartBarOpen = if barNumber() == StartBar then 1 else StartBarOpen[1]+1;
plot count = StartBarOpen;``````
I found this code which tells me the number of bars since the market opened but I get a "Only constants expected here" when I put it in the Sum().

Is there a way to get the number of bars since the market opened another way?

Solution
with so many count questions, i added a tutorial in the tutorial section here:

as for your specific study you requested here it is:

Remember to leave a thumbs up if you found this post useful

PERCENTAGE OF THE CUMULATIVE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE THE CURRENT DAYS ( TODAYS ) SELECTED TIME FRAME
Code:
``````# PERCENTAGE OF THE CUMULATIVE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE THE CURRENT DAYS ( TODAYS ) SELECTED TIME FRAME
# By XeoNoX via Usethinkscript.com
declare lower;
input start_time = 0930;
input endtime = 1600;
def Active = if GetLastDay() == GetDay() and SecondsFromTime(start_time) >= 0 and...``````
Counts number of Bars since open
Code:
``````# Counts number of Bars since open
def barsfromopen = (RegularTradingstart(GetYYYYMMDD())- GetTime() )/ GetAggregationPeriod();
plot scan = barsfromopen ;``````

I still get an "Only constants expected here: scan CL constant function parameter 'length' at ___" with that code when I put it in Sum()

to word it differently... it sounds like you are attmepting to get the number of green candles (closed higher) since market open compared to the number of red candles (closed lower) since market open? ... as a percent?

I'm using a 1 Day 2 minute chart. Yeah 195 bars might be better in my case. My thinking was that knowing the ratio of candles that are increasing vs decreasing so far in the trading day would be a useful way of seeing the trend and volatility of a stock, like if the day so far was 80% green bars it would have a very strong/stable upward trend, or 50% might have more ups and downs. I wanted the percent to only be since the market opened so that yesterday's/extended hours' data wouldn't add irrelevant data if I was looking at a stock blowing up today.

with so many count questions, i added a tutorial in the tutorial section here:

as for your specific study you requested here it is:

Remember to leave a thumbs up if you found this post useful

PERCENTAGE OF THE CUMULATIVE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE THE CURRENT DAYS ( TODAYS ) SELECTED TIME FRAME
Code:
``````# PERCENTAGE OF THE CUMULATIVE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE THE CURRENT DAYS ( TODAYS ) SELECTED TIME FRAME
# By XeoNoX via Usethinkscript.com
declare lower;
input start_time = 0930;
input endtime = 1600;
def Active = if GetLastDay() == GetDay() and SecondsFromTime(start_time) >= 0 and SecondsFromTime(endtime) <
0 then 1 else 0;
def var = close>open;
def cumulative_green = if Active and !Active[1] then var else if Active then cumulative_green[1] + var else cumulative_green[1];
def var2 = close<open;
def cumulative_red = if Active and !Active[1] then var2 else if Active then cumulative_red[1] + var2 else cumulative_red[1];
def Total_red_green = (cumulative_red + cumulative_green);
def percentage_of_green = cumulative_green/Total_red_green;
plot scan  = percentage_of_green;
AddLabel (yes, "COUNT " +  (scan), color.dark_green  );``````

87k+ Posts
399 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?