Print Page  
Advanced Functions / Loop Functions
LoopRelStdDev("formula(CTR)", iterations[, start=0, increment=1, noNAs=FALSE, break=FALSE])
Full Description

These “Loop” functions return a summary statistic from a series of historical values evaluated using a Loop Function. Loop functions contain a CTR variable that is iterated through with specific increments to create the desired offset for the formula. 

LoopAvg(formula(CTR)", iterations[, start, increment, noNAs, break])
Average of the values

LoopSum(formula(CTR)", iterations[, start, increment, noNAs, break])
Sum of the values

LoopProd(formula(CTR)", iterations[, start, increment, noNAs, break])
Product of the values

LoopMedian(formula(CTR)", iterations[, start, increment, noNAs, break])
Median of the values

LoopMax(formula(CTR)", iterations[, start, increment, noNAs, break])
Maximum value

LoopMin(formula(CTR)", iterations[, start, increment, noNAs, break])
Minimum value

LoopStdDev(formula(CTR)", iterations[, start, increment, noNAs, break])
Standard deviation of the values

LoopRelStdDev(formula(CTR)", iterations[, start, increment, noNAs, break])
Relative standard deviation is calculated as follows: RSD = 100 * (SD/Abs(mean)) 

Parameters

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) 
noNAs: when set to 1 any NAs will return NA, when set to 0 (the default) NAs are skipped 
break: when set to 1 the loop will break when it encounters an NA and will return the result to that point, when set to 0 (the default) the function will return NA if it encounters any NA in the loop.

Example 1

Recreating the simple moving average SMA(5) three different ways: 

LoopSum("Close(CTR)", 5) / 5
 or
LoopAvg("Close(CTR)", 5)
 or
(Close(0) + Close(1) + Close(2) + Close(3) + Close(4)) / 5

Example 2

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

LoopSum("Close(CTR) > Close(CTR + 1)", 10)

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

Find companies that have increased quarterly income after tax at least 8 of the last 10 quarters 

LoopSum("IncAftTax(CTR, QTR) > IncAftTax(CTR + 1, QTR)", 10) >= 8