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

XeoNoX

Expert
VIP
Lifetime
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] + 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[1] then var else if Active then compoundValue(1,  (cumulative[1] + var), 0) else cumulative[1];
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[1] then var else if Active then cumulative[1] + var else cumulative[1];
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:
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.
 
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...
 
@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:
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)  );
 
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:
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[1]. 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[1] but that didn't work. got any ideas? thanks!
 
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?
 
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.
 
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[1];

#close>open;

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

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

edited picture to show desired count:

sKRM9pB.jpg
 
Last edited:
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.
 
@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;
 
that didn't work for me, even after i changed close>open to close>close[1], '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[1];

#close>open;

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

# the -2 is the fix

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

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

thanks a bunch!
 
it should have worked...
did you try close>=close[1]?
this seemed to have worked on JASVX when i tried it
Code:
def var =close>=close[1];
def count = totalsum(var);
plot scan  =count;
AddLabel (yes, "COUNT " +  (count)  );
 
Last edited:
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[1];
def count = totalsum(var);
AddLabel (yes, "COUNT " +  (count), color.dark_green   );
plot scan  =count;

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

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
331 Online
Create Post

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