# scan for tickers that have been UP for x number of consecutive days

#### stormy77

##### Member
Seems like no one has been able to answer this, so let me see if I can revise my request:

How to create a scan for tickers that have been UP for x number of consecutive days (closing on each day higher than closing on previous day)
Ideally, I'd like to be able to specify number of days as a dropdown, but I can work with a fixed number in the script (and also same for tickers that are down for x number of consecutive days.

They have to be consecutive days... so if it breaks on any day within the past x days, the count has to begin.

Thank you

Solution
@stormy77 Scan for either 'Up_Signal' or 'Dn_Signal' being true.
Ruby:
``````input Days_Up = 5;
input Days_Down = 5;
def Days_Close = close(period=AggregationPeriod.DAY);

def UpDays = if Days_Close < Days_Close[1] then 0 else if Days_Close > Days_Close[1] then UpDays[1]+1 else UpDays[1];
def DnDays = if Days_Close > Days_Close[1] then 0 else if Days_Close < Days_Close[1] then DnDays[1]+1 else DnDays[1];

plot Up_Signal = UpDays >= Days_Up;
Up_Signal.hide();
plot Dn_Signal = DnDays >= Days_Down;
Dn_Signal.hide();``````
@stormy77 Scan for either 'Up_Signal' or 'Dn_Signal' being true.
Ruby:
``````input Days_Up = 5;
input Days_Down = 5;
def Days_Close = close(period=AggregationPeriod.DAY);

def UpDays = if Days_Close < Days_Close[1] then 0 else if Days_Close > Days_Close[1] then UpDays[1]+1 else UpDays[1];
def DnDays = if Days_Close > Days_Close[1] then 0 else if Days_Close < Days_Close[1] then DnDays[1]+1 else DnDays[1];

plot Up_Signal = UpDays >= Days_Up;
Up_Signal.hide();
plot Dn_Signal = DnDays >= Days_Down;
Dn_Signal.hide();``````

I am trying to re-use this for getting consecutive bars for EMA

My current EMA scan has this code:
Code:
``close("priceType" = PriceType.LAST) is greater than MovAvgExponential("length" = 20)."AvgExp" within 2 bars``

I want to change it so it filters for tickers where last price is greater than 20 EMA for at least 4 bars (15 minute timeframe)

@stormy77
Ruby:
``````input Up = 4;
input Down = 4;
def Agg_Close = close("priceType" = PriceType.LAST, period=AggregationPeriod.FIFTEEN_MIN);
def EMACond = MovAvgExponential("length" = 20)."AvgExp";

def AggUp = if Agg_Close < EMACond then -1 else if Agg_Close > EMACond then AggUp[1]+1 else AggUp[1];
def AggDn = if Agg_Close > EMACond then -1 else if Agg_Close < EMACond then AggDn[1]+1 else AggDn[1];

plot Up_Signal = AggUp >= Up;
Up_Signal.hide();
plot Dn_Signal = AggDn >= Down;
Dn_Signal.hide();``````

@stormy77
Ruby:
``````input Up = 4;
input Down = 4;
def Agg_Close = close("priceType" = PriceType.LAST, period=AggregationPeriod.FIFTEEN_MIN);
def EMACond = MovAvgExponential("length" = 20)."AvgExp";

def AggUp = if Agg_Close < EMACond then -1 else if Agg_Close > EMACond then AggUp[1]+1 else AggUp[1];
def AggDn = if Agg_Close > EMACond then -1 else if Agg_Close < EMACond then AggDn[1]+1 else AggDn[1];

plot Up_Signal = AggUp >= Up;
Up_Signal.hide();
plot Dn_Signal = AggDn >= Down;
Dn_Signal.hide();``````
Thanks. you've been a super help...as always!

T I had to remove the aggregationperiod because the scanner warned me about the secondary aggregation period.
I'll test it out when market opens .

Question. I want to do the same scan, but on vwap. I presume, I should be able to replace the EMA related code with VWAP right?

Question. I want to do the same scan, but on vwap. I presume, I should be able to replace the EMA related code with VWAP right?
Yes, just change the definition of 'EMACond' to your definition for VWAP.

OK so I ran the script, looking for upsignal over 4 bars
here's my scan
http://tos.mx/pLgkiI5

However, I'm seeing most results are below or at the 20ema mark

On the same charts I'm showing either 'Dn_Signal is true or neither Up nor DN is true.

I changed the plot settings to show a '1' above the bar if 'Up_Signal' is true and a '1' below the bar if 'Dn_Signal' is true to assist is validating the scan.
Ruby:
``````input Up = 4;
input Down = 4;

def Agg_Close = close("priceType" = PriceType.LAST);
def EMACond = MovAvgExponential("length" = 20)."AvgExp";

def AggUp = if Agg_Close < EMACond then -1 else if Agg_Close > EMACond then AggUp[1]+1 else AggUp[1];
def AggDn = if Agg_Close > EMACond then -1 else if Agg_Close < EMACond then AggDn[1]+1 else AggDn[1];

plot Up_Signal = if AggUp >= Up then 1 else double.nan;
Up_Signal.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot Dn_Signal = if AggDn >= Down then 1 else double.nan;
Dn_Signal.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);``````

On the same charts I'm showing either 'Dn_Signal is true or neither Up nor DN is true.

I changed the plot settings to show a '1' above the bar if 'Up_Signal' is true and a '1' below the bar if 'Dn_Signal' is true to assist is validating the scan.
Ruby:
``````input Up = 4;
input Down = 4;

def Agg_Close = close("priceType" = PriceType.LAST);
def EMACond = MovAvgExponential("length" = 20)."AvgExp";

def AggUp = if Agg_Close < EMACond then -1 else if Agg_Close > EMACond then AggUp[1]+1 else AggUp[1];
def AggDn = if Agg_Close > EMACond then -1 else if Agg_Close < EMACond then AggDn[1]+1 else AggDn[1];

plot Up_Signal = if AggUp >= Up then 1 else double.nan;
Up_Signal.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

plot Dn_Signal = if AggDn >= Down then 1 else double.nan;
Dn_Signal.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);``````
Thank you for taking the time to do this.
So, if I want 4 consecutive bars ABOVE the EMA, I should comment out these two lines. correct?

Code:
``````plot Dn_Signal = if AggDn >= Down then 1 else double.nan;
Dn_Signal.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);``````

so here's what I'm experiencing.
1. If I use a 15 m aggregation, the results are off (i get some below the EMA, even though I have chosen ABOVE)
2. If I choose 20 min aggregation, the results are more consistent
3. If I choose a 30 M aggregation, then the results are even more consistent.
4. If I choose the 5 or 10 minute aggregation, the results are best

Not sure what to make of it, but either 5 or 10 will work for me
Thank you again

So, if I want 4 consecutive bars ABOVE the EMA, I should comment out these two lines. correct?
You don't have to comment out anything, you just change which plot you are testing in the scan setup.

Last edited:
That is the issue, use the built in Condition Wizard to create the scan.
I changed the scan you shared to use the Condition Wizard and the results were correct.

That is the issue, use the built in Condition Wizard to create the scan.
I changed the scan you shared to use the Condition Wizard and the results were correct.
OK, @Svanoy, so I was copying and pasting the entire code into custom study.
I'm a little lost with what you're saying. I go into conditional scan but am not seeing A2.
I went into Tools>Thinkscript and added this code as a new study, so I thought A2 would show up. I'm definitely missing something

OK, @Svanoy, so I was copying and pasting the entire code into custom study.
I'm a little lost with what you're saying. I go into conditional scan but am not seeing A2.
I went into Tools>Thinkscript and added this code as a new study, so I thought A2 would show up. I'm definitely missing something
Hi Stormy, I am new here. I was wondering if you got your up/down trend 4 days or more scan to work. Would you mind sharing the Tos link? Thanks so much!!!

@stormy77 'A2' is just the name I had saved the study as. You will change that to whatever you saved it as.

@stormy77 'A2' is just the name I had saved the study as. You will change that to whatever you saved it as.
OK, this us a duh moment for me. Thanks so much. I did not know you could reference an entire study like this. I was looking for a specific function to call. You have been a super help!

Hi Stormy, I am new here. I was wondering if you got your up/down trend 4 days or more scan to work. Would you mind sharing the Tos link? Thanks so much!!!
Hi there. Just follow the last code snippet and the screenshot earlier in the post. I was messing up earlier, but now that I followed precise instructions, I think I'm good. Let me know if you have any issues

Hi Svanoy. This script works well, except when I choose a daily aggregation period. Then I get an error:
Wrong timestamp for on Sep-27-2022: 20:37
I guess, what I'm trying to accomplish is this:
1. I'd like to choose the time period from the scan settings
2. I want the EMA to daily aggregation

so when we look at 4 bars above EMA in the default example, it should be 4 bars for whatever aggregation period I choose but the EMA would be daily

so I typically choose 3 MINUTE candles
How do we force the EMA to use daily, while keeping bats at 3 min.

I tried changing the EMACond line to this:
Code:
``def EMACond = MovAvgExponential(close (period = AggregationPeriod.DAY), 20);``

But the scan did not like this. It warned me about secondary period... How would I accomplish this?

One possible method would be to use the Previous day Close to calculate the EMA instead of specifying an aggregation period explicitly...unsure How it would work, or how to do this

Last edited:
@stormy77 yes, you have to define the higher aggregation with current aggregation data.

@stormy77 yes, you have to define the higher aggregation with current aggregation data.
I'm not sure I Understand. I can't get around this

The scanner does not accept references to secondary aggregations. You would have to manually calculate the daily EMA from among the intraday data, using the closing price from the final bar of each day. But the scanner has limited access to historical data. You only gain access to roughly three or four days worth of data when scanning from a three minute aggregation.

As per your original question, logical comparisons evaluate to a 1 or a 0, so all you have to do is sum the comparison.

input Length = 5;
def x = Sum(close > close[1], Length) == Length;

87k+ Posts
321 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?