New function to be released tomorrow, Friday April 30: LoopSum()

By tomorrow the following new function will be available:


Executes the formula a number of times and returns the sum. A special variable ‘CTR’ (short for counter) is available for the formula that is incremented each time.

formula: the formula to be executed in the loop. The special CTR variable must be used here.
iterations: the number of times to execute the formula (2-50)
start: the starting value of CTR(default 0)
increment: how much CTR is incremented for each iteration (default 1)

Example 1 Recreating the simple moving average. You can recreate SMA(5) like this:

LoopSum(“Close(CTR)”,5) / 5

This is also the same as:


Example 2 Count the number of times the close price is greater than the previous day for the past 10 bars:


The “>” test is executed 10 times and returns either 1 (TRUE) or 0 (FALSE). The values are added together. The sum returns from 0-10, with 10 being stocks that have had higher closing prices for the past 10 bars vs the previous day.

Example 3 Number of years the annual revenues have increased for the past 5 years:

LoopSum(“ItemA(RTLR,CTR) > ItemA(RTLR,CTR+1)”,5)

The condition is evaluated 5 times and the sum returns from 0-5. When 5 all annual sales have increased from previous year.

This is a very powerful function. I’ve seen many examples of very long formulas that can be recreated with LoopSum in much fewer characters and less error prone. Le us know of great examples for using this function.

Once again, it will be made available tomorrow.

I believe this function takes care of three Feature requests:

Feature Request: Count the number of times an expression is true

Thread: Feature Request: Need StockCount(expr, bars, offset) and StockBarsSince(expr, bars, offset) to calculate stock stats for Ranking

Feature Request: Summation or Count Function and BarsSince Function

Thank you!

Small change. We’ll use CTR (counter) instead of IDX which can be confused with stock indexes

Thanks for the feature.

Thank you for this. I would like to point out also that aside from finding stocks that meet a high count threshold, this function can also do the converse and find stocks that meet a low count threshold. For example one can find stocks that haven’t fallen below their 50-day MA in the past 50 days 2 ways: Loopsum(Close(0)>=SMA(50),50) = 50 or Loopsum(Close(0)<SMA(50),50) = 0. In this case not much of a difference but there may be cases where the more intuitive way of expressing the problem is to count the number of times a condition does NOT occur.


I think you mean

Loopsum(“Close(CTR)>=SMA(50,CTR)”,50) = 50

One more thing, do not use LoopSum(xxx) = 0 when using a boolean test like ‘>’ etc. The reason is NAs. If there’s an NA in either side of the boolean test you get a 0

Awesome! Much needed, thanks…

Is it possible to calculate the standard deviation of volume using this function?

Would this work…




Just verify it using

SetVar(@stdvol , … )

Then run a screen with report set to ScreenFactors. You’ll see the value it calculates in the report colum ‘@stdvol

To make that clearer I’m using…


…where ### is the Loopsum calculation, to get the square root, since there isn’t a square root function. Wondering if this will work as intended. Thanks.

Umm, didn’t know you could do that. Is there a tutorial on verifying? I don’t know exactly how to “set” it to @stdvol

Never mind, I found the original New Feature post on SetVar… thanks again.

Never mind figured it out.

One of my requests from long ago and one I have made periodically since is a function that returns the number of bars from a given condition. I was wondering why one hasn’t been released yet and I stumbled upon this old thread.

Marco in this thread you seem to indicate that maybe the LoopSum() functions solves both cases and maybe that’s the reason it has fallen off your radar. It’s close enough in function to maybe give the impression that it does, but it doesn’t. I made a mistake in my initial feature request by combining two distinct functions one for summation and one for returning number of bars. LoopSum() makes summation of a given condition possible but it does not return the number of bars from a given condition at least not precisely and without introducing plenty of opportunity for error.

A dedicated function that returns number of bars would still be helpful. The BarsSince() function that has since been introduced using a date input is a weak substitute. I read that the team is busy so tackling this issue may be low on the priority list if it is at all. But the impression I get is that development of the LoopSum() function actually did most of the coding heavy lifting and only a minor modification that allows for results like LoopMedian(), LoopAvg(), LoopMax(), LoopMin() could be applied to create something like a LoopBars().

You could probably come up with a workaround using LoopSum. For example, let’s say you want the number of bars that the stock’s price has been higher than its 120-day SMA. The formula would be IsNA(LoopSum ("eval(close(ctr) > sma(120,ctr),1,NA)",100,0,1,1,1),0)

Yes, I use LoopSum with a break condition to calculate the number of bars since some condition. Here’s an example of it in a screen that have gone at least 10 bars without a higher close than the day before.

I’m not exactly sure with the formulas suggested but the downside from using LoopSum for the purpose I have in mind is that you have to have a more definite idea of the time period you are working with and within that period it’s unclear when in that look back time period the condition may be true.

For example LoopSum can return that the close > than the moving average for 90 of the last 100 days but there is a material difference if those 10 days it was below the MA was 90 days ago and the close is now currently above the MA or in the last 10 days and is now below the MA. One also cannot easily discern if the price was zig-zagging up and down through the moving average with choppy price action or behaving orderly. One can add on other rules to account for these things but not precisely and one can easily overlook needing to. A function that returns bars can more clearly delineate between regimes which can be very useful.

The formula I offered is the number of bars that the stock’s price has been higher than its 120-day SMA starting right now. If the stock is currently below its 120-day SMA it returns 0. If it was higher than its 120-day SMA 3 days ago but is lower now, it’s still 0. If it was higher for 99 days and then fell below it, it still returns 0. You can use a 200-day lookback or longer if you want.

In any case, LoopSum is flexible enough to answer almost any kind of question you might have about the number of bars. If you can’t figure out how to get the answer you need, I’ll be glad to help.

I’ll play around with the suggested formula. Thanks.