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();

Svanoy

Well-known member
@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();
 
Solution

stormy77

Member
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)
 

Svanoy

Well-known member
@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

Member
@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?
 

stormy77

Member
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


Wadxt8F.png


6DEVDWm.png


3XgQeXp.png
 

Svanoy

Well-known member
@stormy77 Please check your scan setup.
On the same charts I'm showing either 'Dn_Signal is true or neither Up nor DN is true.


PSHwaYm.png


AFR0Czt.png


fFAGt8s.png


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);
 

stormy77

Member
@stormy77 Please check your scan setup.
On the same charts I'm showing either 'Dn_Signal is true or neither Up nor DN is true.


PSHwaYm.png


AFR0Czt.png


fFAGt8s.png


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
 

stormy77

Member
@stormy77 just noticed the share link to your scan.
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
 

SheeRah

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

Member
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
 

stormy77

Member
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


EcQND46.png
 
Last edited:

Similar threads

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.
Top