New function LoopStreak for counting consecutive occurrences

New function is now available

LoopStreak ("formula(CTR)", iterations, start=0, increment=1, streak=#Positive, recent=TRUE )

Counts the number of consecutive times the formula evaluates to the streak type specified

formula: the formula to be executed in the loop.
iterations: the number of times to execute the formula (up to 502)
start: the starting value of CTR (default 0)
increment: how much CTR is incremented for each iteration (default 1)
streak: #Positive, #NotPositive, #Increasing, #NotIncreasing
recent: set this to TRUE to stop when streak fails. Use FALSE to evaluate all iterations and find the longest streak.

Example

Find companies that have closed higher than previous day for 10 trading days in a row

LoopStreak("close(CTR)", 21, 0, 1, #Increasing, TRUE) > 10

6 Likes

Thanks- excited to try it out with custom formulas

Excellent. Makes a good sell rule:
LoopStreak("close(CTR)", 21, 0, 1, #Increasing, TRUE) > 5

Thanks Marco. Very nice

@geov That improved mine as well, with a small modification.

Thank you!

I don't understand the difference between Positive and Increasing.

Thanks Marco!

Streak reading the series from left to right (0 is not a positive number)

Series: 5,3,4,0,-2
#Positive: 3
#Increasing: 2

I'll add some clarifications to the doc. Thanks

I added this to the doc:

streak:

  • #Positive: streak of numbers > 0
  • #NotPositive: streak of numbers ≤ 0
  • #Increasing: streak of increasing numbers, as in a(i) > a(i+1)
  • #NotIncreasing: streak of decreasing or equal numbers, as in a(i) ≤ a(i+1)

Oh I get it, that's great. This way it can be used on an indicator as well as a security!

When a streak ends does the tally reset and begin counting again or does it end without starting over?

For example if a company has had positive earnings for 3 years out of ten and on the fourth it has negative earnings before resuming being positive for the rest of the time period what is the streak number using recent="TRUE"?

year1:positive, y2:positive,y3:positive, y4:negative, y5,6,7,8,9,10:positive

would the count be 6? or 3? I would be interested in a count of the "recent/current" streak (6). Is that with recent=true?

Is there a document I could reference?

LoopStreak by default is the most recent. Set the "recent" paramter to FALSE to calculate the longest streak.

So for example if the Bears lose 5 games in a row, then they win one, then lose 3:

Then the current loss streak is 3. The longest loss streak is 5

PS. the above assumes we use a function like LoopStreak("BearsLost(CTR)",9) which returns 1 if they lost, 0 otherwise. With CTR=0 being the last game they played.

Thanks

Sorry, if recent=FALSE, then in the example above the losing streak is 5. did you see the "Full description" documentation? Perhaps it can be clearer


Thank you Marco. Your first reply makes a lot of sense to me and I also get the second one since you mention the "false" setting, but the recent: "true" description threw me off a bit when I first read it. I think what threw me off is the first line in the "recent" description. I was expecting that the count would reset once a streak was interrupted but was not sure if stop here meant a literal stop or a reset.

I guess one could interpret a streak of 0 as the recent positive streak of Bear games but also a streak of one as the most recent positive streak. If it stops, then even if that streak got subsequently interrupted and is no longer positive the formula would yield a positive streak. If it resets it goes to zero. A reset to me would be more timely in practice and further differentiate it from loopsum.

In the Bears example, a "stop" would mean the most recent positive streak is one despite multiple recent loses. If it "resets" then it is zero as of the most recent period. I would expect the default setting to "reset" and give me a streak of zero since the recent losses would have reset the streak. Maybe I can do some tests to clarify this in my mind.

Looks to me like it resets :slight_smile: I will be trying this out for the coming weeks hehe

Sorry, not sure what you mean by "reset".

recent=true: function returns the most recent streak length.
recent=false: returns the length of the longest streak by evaluating all the values in the loop.

The example with da Bears was confusing since it's not clear what formula is being evaluated. In the example I provided it would be "BearsLose(CTR)" which returns 1 if they lost, 0 otherwise. With CTR=0 being the last game they played.

1 Like

Yes, zero otherwise makes perfect sense. Thanks Marco! With reset I just meant the count/streak. I get it now. I have started creating formulas using the function and they look quite promising. Thanks for the explanation.