# How to COUNT CUMULATIVE or CONSECUTIVE bars on ENTIRE CHART or SPECIFIED TIME

#### XeoNoX

##### Well-known member
VIP
How to count the cumulative total of bars or the consecutive number of bars on the entire chart and/or the same day
To count your study you simply just have to alter ONE line. You alter the "def = VAR" line.
For this example we will use the number of bars that he close is greater than the open (number of green bars)
the default is:
Code:
``def var = close > open;``
NOTE: "VAR" aka the variable can be changed to the defined study/pattern you want counted.

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

COUNT OF THE TOTAL NUMBER OF CONSECUTIVE GREEN BARS (CLOSE>OPEN)
Code:
``````declare lower;
# COUNT OF THE TOTAL NUMBER OF CONSECUTIVE GREEN BARS (CLOSE>OPEN)
# By XeoNoX via Usethinkscript.com
def var = close > open;
def barUpCount = CompoundValue(1, if var then barUpCount + 1 else 0, 0);
AddLabel (yes, "COUNT " + barUpCount );``````

CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE ENTIRE CHART
Code:
``````declare lower;
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART
# By XeoNoX via Usethinkscript.com
def var =close>open;
def count = totalsum(var);
plot scan  = count;
AddLabel (yes, "COUNT " +  (count)  );``````

CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE ENTIRE CHART WITHIN SPECIFIED TIMEFRAME
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>open;
def cumulative = if Active and !Active then var else if Active then compoundValue(1,  (cumulative + var), 0) else cumulative;
plot scan  = cumulative;
addLabel(1, "Count = " + scan, color.dark_green);
scan.SetPaintingStrategy(PaintingStrategy.Values_Above);``````

CUMULATIVE COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) ON THE THE CURRENT DAY ONLY ( TODAYS ) CHART
Code:
``````declare lower;
#  CUMULATIVE  COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE THE CURRENT DAY ONLY ( TODAYS ) CHART
# By XeoNoX via Usethinkscript.com
def Active = GetDay() == GetLastDay(); #Today
def var = close>open;
def cumulative = if Active and !Active then var else if Active then cumulative + var else cumulative;
plot scan  = cumulative;
AddLabel (yes, "COUNT " +  (scan), color.dark_green  );``````

COUNT OF THE CUMULATIVE OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) FROM XYZ BARS AGO
(COUNT CUMULATIVE TOTAL OF GREEN BARS FROM THE LAST / PAST XYZ BARS)
Code:
``````declare lower;
#COUNT OF THE CUMULATIVE OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN) FROM XYZ BARS AGO
#(COUNT CUMULATIVE TOTAL OF GREEN BARS FROM THE LAST / PAST XYZ BARS)
# By XeoNoX via Usethinkscript.com
input barsago = 15;
def var =close>open;
def count = Sum(var, barsago);
AddLabel (yes, "COUNT " +  (count)  );``````

Last edited:
• • Christopher84, viswanarahari, Pelonsax and 5 others

#### Khahuy12

##### New member
VIP
Can we get this plotted on watchlist?

#### XeoNoX

##### Well-known member
VIP
@Khahuy12 to change to scan simply change the addlabel

example:

Code:
``AddLabel (yes, "COUNT " +  (count)  );``
to
Code:
``plot scan = count;``

• BenTen

#### FoxEatsGekko

##### New member
VIP
I'm trying to figure out what function is needed to count how many times a certain condition was true in the last x number of bars

For example, If I want to find out how many times a stock hit 1 million in volume in the last 21 days.

#### rad14733

##### Well-known member
VIP
I'm trying to figure out what function is needed to count how many times a certain condition was true in the last x number of bars

For example, If I want to find out how many times a stock hit 1 million in volume in the last 21 days.

Several counter scripts have been posted by @XeoNoX in recent weeks that you can extract the need code from... It's more complex than simply using a function... Search for "counter" and @XeoNoX as the member name and they should show up in the results...

#### Prison Mike

##### Member
@XeoNoX How can I make it count only once?

I'm trying to use it to figure the probability if a stock moves .05 up, what are the chances it will move .5 up?
This idea comes from a forex trader, "TheRumpledOne." Check him out if you don't know of him.

As you can see the numbers should not be more than "barsago" length.

EDIT: figured it out. Added "crosses above" to the code.

EDIT 2: Something else is off. The numbers don't seem right.
Code:
``def var = close crosses above buyzone;``
Does "close" in this portion of code account for the last price on the daily? or is it the close of the daily? Might be why its off.

Code:
``````# (Probabilty of XYZ FROM THE LAST / PAST XYZ BARS )
# Original/base code By XeoNoX via Usethinkscript.com
input barsago = 100;
input buy= .05;
def buyzone= (open + buy);
def var = close crosses above buyzone;
def count = Sum(var, barsago);
AddLabel (yes, "COUNT " +  (count)  );
def pct= round(count/barsago)*100;
AddLabel (yes, "BuyZone " +  (pct)  );

input Sell= .50;
def sellzone= (open + sell);
def var1 =close crosses above  sellzone;
def countsells = Sum(var1, barsago);
AddLabel (yes, "COUNT " +  (countsells)  );
def pct2=round (countsells/barsago)*100;
AddLabel (yes, "SellZone " +  (pct2)  );``````

Last edited:

#### XeoNoX

##### Well-known member
VIP
@Prison Mike close is the value of the close of the last bar on the chart

#### XeoNoX

##### Well-known member
VIP
I'm trying to figure out what function is needed to count how many times a certain condition was true in the last x number of bars

For example, If I want to find out how many times a stock hit 1 million in volume in the last 21 days.

here is the code for what you requested
stock hit 1 million in volume in the last 21 days

Code:
``````# By XeoNoX via Usethinkscript.com
input barsago = 21;
def var =volume>1000000;
def count = Sum(var, barsago);
AddLabel (yes, "COUNT " +  (count)  );``````

#### floydddd

##### Active member
Re the second bit of code in the first post -- any way to add to the label the total number of bars under consideration, so that instead of it saying Count = 16, for example, it'd say Count = 16 out of whatevernumberofbars are on the screen? thanks!!!

well i guess i did it on my own for a change. just added: AddLabel(yes, "bars: " + BarNumber()); seems to do it.

Last edited:
• XeoNoX

#### floydddd

##### Active member
re the second study in the first post: one way i'm using this is on mutual funds, which don't have an open price, so i changed def var = close>open to def var = close>close. so far so good. but the funds i'm looking at can sometimes go for a few days with no price change and i'd like to include those days in the cumulative count of green days. i tried close>=close but that didn't work. got any ideas? thanks!

#### XeoNoX

##### Well-known member
VIP
if there is no bar you cant code against it

#### floydddd

##### Active member
i'm not sure i understand. in any event, i do believe there is a bar in the instances i'm talking about, but it's the same as the bar before it. is what you're saying, that if two bars are the same and right next to each other, they can only be counted as 1 bar and not two?

#### XeoNoX

##### Well-known member
VIP
i'm not sure i understand. in any event, i do believe there is a bar in the instances i'm talking about, but it's the same as the bar before it. is what you're saying, that if two bars are the same and right next to each other, they can only be counted as 1 bar and not two?
maybe a screenshot with arrows or circles pointing to what you are trying to explain.

#### floydddd

##### Active member
okay, i'll try. below is a screenshot with the cumulative count as the last study. here's the code i've been using as well. chart is of a mutual fund JASVX, set for one month. i'm trying to get the label to show the number of bars on the screen that are either green or no change, along w/ the total number of bars. the current example should show 18 bars either up or the same, out of 22 total. Instead, it's showing 20 out of 22.

i think the problem is that, for some reason, the count is always starting at 3, which i have circled. that throws everything off. it should start at one. Got any idea how I can sort this out? thanks!

Code:
``````declare lower;
#( 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>=close;

#close>open;

def cumulative = if Active and !Active then var else if Active then cumulative + var else cumulative;
plot scan  = cumulative;
addLabel(1, "Count = " + scan, color.dark_green);

AddLabel(yes, "total bars: " + BarNumber());``````

edited picture to show desired count: Last edited:

#### XeoNoX

##### Well-known member
VIP
you are correct, since there is not prior bar to count on the first bar it assumes the first bar is higher because its theoretically higher than 0. you can offset it by subtracting 1.

okay. thanks!

#### XeoNoX

##### Well-known member
VIP
@floydddd try this let me know how it works, i didnt thoroughly check this, im in a rush. i assume you just wanted the total on the entire chart.

Code:
``````declare lower;
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART
# By XeoNoX via Usethinkscript.com
def var =close>open;
def count = totalsum(var);
AddLabel (yes, "COUNT " +  (count)  );
plot scan  =count;``````

#### floydddd

##### Active member
that didn't work for me, even after i changed close>open to close>close, 'cause i'm dealing w/ mutual funds. otoh, your suggestion to subtract the extra days seems to have done the trick:

Code:
``````declare lower;
#( 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>=close;

#close>open;

def cumulative = if Active and !Active then var else if Active then cumulative + var else cumulative;
plot scan  = cumulative - 2;

# the -2 is the fix

addLabel(1, "Count = " + scan, color.dark_green);

AddLabel(yes, "total bars: " + BarNumber());``````

thanks a bunch!

#### XeoNoX

##### Well-known member
VIP
it should have worked...
did you try close>=close?
this seemed to have worked on JASVX when i tried it
Code:
``````def var =close>=close;
def count = totalsum(var);
plot scan  =count;
AddLabel (yes, "COUNT " +  (count)  );``````

Last edited:

#### floydddd

##### Active member
beautiful, perfect. i changed the color and added the total sum and yup thanks again.

Code:
``````declare lower;
#( CUMULATIVE ) COUNT OF THE TOTAL NUMBER OF GREEN BARS (CLOSE>OPEN)
# ON THE ENTIRE CHART
# By XeoNoX via Usethinkscript.com
def var =close>=close;
def count = totalsum(var);
AddLabel (yes, "COUNT " +  (count), color.dark_green   );
plot scan  =count;

AddLabel(yes, "total bars: " + BarNumber());``````

• XeoNoX