# Consecutive Signals

#### zackgray88

##### New member
Hi all! I've lurked for quite awhile and have gleaned a lot of useful information a bits of code here and there to build different indicators. I'm a little stuck on trying to finish the one below.

Purpose of Study: Looks at the average daily ATR and divides it by a user input to define what a tight intraday range is for a given stock. This maps out areas of tight consolidations where price may move aggressively away from (load this indicator on \$PFE with a 3 minute chart for the past few days). That part of the indicator is working fine for my needs.

Problem: I'm really only interested when I get 5 or more bars consecutive signals in a row (i.e. one bar with a tight range in the midst of a bunch of volatility isn't what I'm looking for). Is there anyway to get this to only plot a signal when there are consecutive number of true signals? I took a stab at the bottom of the code that I just have blanked out.

Any help would be super appreciated!

Code:
input aggPeriod = AggregationPeriod.DAY;

input length = 20;

input multiplier = 2;

input barperiod = aggregationperiod.three_min;

input atrrange = 40;

def studyPrice = close(period = aggPeriod);

def hi = high(period = aggPeriod);

def lo = low(period = aggPeriod);

def r = hi - lo;

def avgRange = Average(r, length);

def avg = Average(studyPrice, length);

def range = high(period = barperiod) - low(period = barperiod);

def avgbodyLength = ((high(period = barperiod)[1] - low(period = barperiod)[1]) + (High(period = barperiod)[2] - low(period = barperiod)[2]) + (High(period = barperiod)[3] - low(period = barperiod)[3]) + (High(period = barperiod)[4] - low(period = barperiod)[4]) + (High(period = barperiod)[5] - low(period = barperiod)[5]) + (High(period = barperiod)[6] - low(period = barperiod)[6]) + (High(period = barperiod)[7] - low(period = barperiod)[7]) + (High(period = barperiod)[8] - low(period = barperiod)[8])+ (High(period = barperiod)[9] - low(period = barperiod)[9]) + (High(period = barperiod)[10] - low(period = barperiod)[10]) + (High(period = barperiod)[11] - low(period = barperiod)[11]) + (High(period = barperiod)[12] - low(period = barperiod)[12]) + (High(period = barperiod)[13] - low(period = barperiod)[13]) + (High(period = barperiod)[14] - low(period = barperiod)[14]) + (High(period = barperiod)[15] - low(period = barperiod)[15]) + (High(period = barperiod)[16] - low(period = barperiod)[16]) + (High(period = barperiod)[17] - low(period = barperiod)[17]) + (High(period = barperiod)[18] - low(period = barperiod)[18]) + (High(period = barperiod)[19] - low(period = barperiod)[19])+ (High(period = barperiod)[20] - low(period = barperiod)[20])+ (High(period = barperiod)[21] - low(period = barperiod)[21])+ (High(period = barperiod)[22] - low(period = barperiod)[22])+ (High(period = barperiod)[23] - low(period = barperiod)[23])+ (High(period = barperiod)[24] - low(period = barperiod)[24])+ (High(period = barperiod)[25] - low(period = barperiod)[25])+ (High(period = barperiod)[26] - low(period = barperiod)[26])+ (High(period = barperiod)[27] - low(period = barperiod)[27])+ (High(period = barperiod)[28] - low(period = barperiod)[28])+ (High(period = barperiod)[29] - low(period = barperiod)[29])+ (High(period = barperiod)[30] - low(period = barperiod)[30]))/length;

def nan = Double.NaN;

tightrange.setpaintingstrategy(paintingstrategy.booleAN_POINTS);

#def consecutive = if tightrange then tightrange[1] + 1 else 0;

#plot consecutivebars = consecutive > 5;

You can set up a T/F series with 1 and 0 (this is one of my favourite tricks these days) like this:
Code:
def tight_range_tf = if tightrange then 1 else 0;
then you can do a sum over the length you want to find consecutive true signals and plot it if it is true:
Code:
input consecutive_length = 5;
plot consecutive_true = if sum(tight_range_tf, consecutive_length) == consecutive_length then 1 else double.nan;
consecutive_true.SetPaintingStrategy(PaintingStrategy.BooleanArrowUp;

Perhaps that gets you going in the right direction.

-mashume

N.B. I didn't check my syntax on the painting strategy. I can't remember what the correct notation for a boolean arrow up is off the top of my head.

EDIT: I think I had the sum function called incorrectly. It should be fixed now.

Last edited:

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