Using the CQG Modulus Function

One of the simplest examples for this function is to find out whether a value is an even number.

Modulus (Close(@),2) = 0

In other words, the result of the formula above would give us the remainder of the operation Closing Price/2. If the result is different than 0, the Closing Price of the bar in question is not an even number. If the remainder of the operation is 0, then the Closing Price is an even number.

The best example for this function would be to find out whether a year is a leap year. The definition of a leap year is a year that is divisible by 4, but not by 100, unless it is divisible by 400. This would translate in CQG code as:

(Modulus(LocalYear(@),4)=0 AND  (Modulus(LocalYear(@),100)<>0) OR Modulus(LocalYear(@),400) =0);

We can see the result on an annual chart of the Dow Jones Index back in 1900 where every year is a leap year, except for 1900, which was not (as divisible by 4 and 100 but not 400).

At the bottom of the post is a downloadable PAC to install this study in CQG.


Microsoft Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries.