# 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...``````

#### XeoNoX

##### Well-known member
VIP
Counts number of Bars since open
Code:
``````# Counts number of Bars since open
def barsfromopen = (RegularTradingstart(GetYYYYMMDD())- GetTime() )/ GetAggregationPeriod();
plot scan = barsfromopen ;``````

#### TNTOutburst

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

##### Well-known member
VIP
@TNTOutburst You can't use plot as that tries to plot a line on your chart... Try using AddLabel() instead as that will paint a label in the upper left of your charts... That should eliminate the error...

Ruby:
``````# Counts number of Bars since open
def barsfromopen = (RegularTradingstart(GetYYYYMMDD()) - GetTime() ) / GetAggregationPeriod();
AddLabel(yes, " Bars from open: " + AbsValue(barsfromopen) + " ", Color.WHITE);``````

Last edited:

#### TNTOutburst

##### New member
@rad14733 The code before could get "Bars from open" using plot. The error came from when I tried to plug the bars value into sum() in my percent code. Putting the percent code in a label doesn't work also because sum() never accepts the value cause it's not constant. Not sure this is possible :/

##### Well-known member
VIP
@rad14733 The code before could get "Bars from open" using plot. The error came from when I tried to plug the bars value into sum() in my percent code. Putting the percent code in a label doesn't work also because sum() never accepts the value cause it's not constant. Not sure this is possible :/

No, Sum() probably won't work in the fashion you are attempting... How are you attempting to implement Sum()...??? There may be an alternate method...

#### TNTOutburst

##### New member
No, Sum() probably won't work in the fashion you are attempting... How are you attempting to implement Sum()...??? There may be an alternate method...
Can you not see it in my original post?
This was my code for roughly showing the percent that a stock is increasing in the last 300 bars:
Code:
``````plot pos = (Average(close, 8) / Average(close, 8)[6]) > 1;
def neg = (Average(close, 8) / Average(close, 8)[6]) < 1;
plot percent = Sum(pos, 300) / (Sum(pos, 300) + Sum(neg, 300));``````

#### XeoNoX

##### Well-known member
VIP
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?

##### Well-known member
VIP
@TNTOutburst My apologies... Please note that I participate a lot here in the forums and may not have time to re-read entire topics which is why I was asking what, specifically, you were attempting to accomplish - which you still never answered, specifically... Regardless, have you tried reducing the number of bars from 300 by chance...??? What timeframe are you using this in...??? You see, the more information you provide the better we can assist you... We need to know what the logic is in your head, not just what you attempt to code...

#### TNTOutburst

##### New member
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.

##### Well-known member
VIP
@TNTOutburst Thank you... That is information we can work with... Unfortunately, it is too late to ponder deeper, at least for me... Lets see what tomorrow brings...

#### XeoNoX

##### Well-known member
VIP
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  );``````