PowerLanguage loops like the for-to loop and down-to loop repeat programming code with each loop cycle. But what if you only wanted to executed a part of the loop every *n*th loop cycle?

#### In this article:

## Modulus and loops in MultiCharts PowerLanguage

The `Mod()`

keyword returns the remainder from dividing one number by another (MultiCharts Wiki, 2012), which is called the **modulus** (Liberty & Cadenhead, 2011). For example, `Mod(20, 2)`

returns 0 because 20 divided by 2 is 10 with no remainder. Such a statement is read as “twenty modulo two equals zero” or, for short, “twenty mod two” (see Liberty & Cadenhead, 2011; Liberty & MacDonald, 2009).

Modulus can control PowerLanguage loops because, when you perform modulus *n* on a number that is a multiple of *n*, the result is zero (Liberty & MacDonald, 2009). This means we can take an action every *n*th time through a loop by testing to see if `Mod(counter, n)`

equals zero.

The basic pattern of `Mod()`

, when applied in a for-to loop, is:

```
for counter = beginValue to endValue begin
if (Mod(counter, 5) = 0) then
Print("This happens on every fifth loop cycle.");
end;
```

This if statement evaluates whether the `counter`

variable divides evenly into 5, which happens when `Mod(counter, 5)`

returns 0. As the `Print()`

statement shows, this is the case every fifth loop cycle.

## Finding every *n*th loop cycle in PowerLanguage

A basic example of using `Mod()`

when looping is the following:

```
Variables:
x(0);
for x = 200 downto 0 begin
if (Mod(x, 25) = 0) then
Print("x = ", NumToStr(x, 0));
end;
//> x = 200
//> x = 175
//> x = 150
//> x = 125
//> x = 100
//> x = 75
//> x = 50
//> x = 25
//> x = 0
```

This down-to loop begins at 200 and continues till 0 with the `x`

counter variable automatically decreased with 1 after each loop cycle.

The `Mod()`

keyword inside the loop is used to check if `x`

divides evenly into 25, followed by printing the value of `x`

when this is the case.

## Calculating the average in a PowerLanguage loop

Another example of `Mod()`

is calculating the average price every fifth price bar:

```
Variables:
y(0),
outputStr(""),
avgClose(0);
outputStr = "";
avgClose = 0;
for y = 1 to 50 begin
outputStr = outputStr + NumToStr(Close[y], 5) + Spaces(2);
avgClose = avgClose + Close[y];
if (Mod(y, 5) = 0) then begin
avgClose = avgClose / 5;
outputStr = outputStr + Spaces(7) +
NumToStr(avgClose, 6) + NewLine;
avgClose = 0;
end;
end;
Print(outputStr);
//> 1.28043 1.28016 1.27989 1.27978 1.27958 1.279968
//> 1.27973 1.27951 1.27986 1.27953 1.27995 1.279716
//> 1.27985 1.28033 1.28053 1.28044 1.28023 1.280276
//> 1.28085 1.28093 1.28113 1.28116 1.28016 1.280846
//> 1.28019 1.28076 1.28046 1.28065 1.28115 1.280642
//> 1.28115 1.28101 1.28040 1.27986 1.27995 1.280474
//> 1.27999 1.28027 1.28047 1.28055 1.28199 1.280654
//> 1.28242 1.28250 1.28262 1.28281 1.28265 1.282600
//> 1.28270 1.28288 1.28340 1.28330 1.28432 1.283320
//> 1.28352 1.28320 1.28341 1.28293 1.28142 1.282896
```

The closing price of the last 50 price bars is outputted here in the first five columns, while the five bar average is displayed in the last column.

Inside the loop, the `outputStr`

variable generates a string that holds all prices. The `avgClose`

variable contains initially the sum of closing prices. When the `Mod(y, 5)`

statement signals that `y`

is an even multiple of 5, the average close is calculated, added to the `outputStr`

variable, and reset to zero for the next five bars.

This `outputStr`

variable is outputted when the for-to loop has ended.

### Summary

`Mod()`

returns the remainder from integer division. It can be used in loops to take an action every *n*th loop cycle by evaluating if `Mod(counter, n)`

equals zero.

**References**

Liberty, J. & Cadenhead, R. (2011). *Sams Teach Yourself C++ in 24 Hours*. IN (USA): Sams/Pearson Education.

Liberty, J. & MacDonald, B. (2009). *Learning C# 3.0: Master the Fundamentals of C# 3.0*. Sebastopol, CA: O’Reilly Media.

MultiCharts Wiki (2012, February 13). *Mod*. Retrieved on November 7, 2014, from http://www.multicharts.com/trading-software/index.php/Mod

Visit Kodify.net for more helpful coding articles.