How can I find the number of consecutive days in the past a condition was true

Anarkyn

New member
Hey all, I have a simple Stacked EMA script in which, I'm looking to add a label that displays number of periods (in this case period is days) that the StackedEMA condition was true.

Any ideas on how to accomplish that? I tried something like

sumBullish = Sum(bullish,365)

but that looked back and counted the total number of days in the last 365 where the EMAs were stacked. I only want to look back and count the total number of recent days where the stack was bullish... as soon as it finds a bearish day the count should stop.

Code:
``````def EMA8 = ExpAverage(close, 8);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);
def EMA55 = ExpAverage(close, 55);

def bullish = EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55;

def bearish = EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55;

## Replace label value with the number of consecutive days
## that the bullish/bearish stack has been in effect
## eg if the EMAs have been stacked for the last 10
## consecutive days than the label should show "10"

AssignBackgroundColor(if bullish then color.green else if bearish then color.red else color.black);``````

You can do something like this...

Ruby:
``def bullCount = if bullish then bullCount[1] + 1 else 0;``

The current value of bullCount is the length of the current streak.

If you want to know what the count was for a streak that just ended you could do something like Highest(bullCount, 5).

You can also use HighestAll(bullCount) to see what the longest streak was and other such functions for other stats if they interest you.

It may also be worth noting there are 220 trading days in a year, in case that's what you meant to capture with 365.

Last edited:
I can see that code being potentially useful for scans to find symbols where the averages are stacked for multiple periods but not a lot of periods. That would be less noisy than a crossover scan yet still filter out results that aren't near the beginning of the move.

You can do something like this...

Ruby:
``def bullCount = if bullish then bullCount[1] + 1 else 0;``

The current value of bullCount is the length of the current streak.

If the you want to know what the count was for a streak that just ended you could do something like Highest(bullCount, 5).

You can also use HighestAll(bullCount) to see what the longest streak was and other such functions for other stats if they interest you.

It may also be worth noting there are 220 trading days in a year, in case that's what you meant to capture with 365.

Well, I was making that a lot harder than I needed to. Thanks!

Not the exact question you're looking for?

87k+ Posts
395 Online

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?