Besides the arithmetic operators in TradingView Pine there’s also another operator for performing calculations: the modulus operator (%). What does this operator do and how can we use it?

In this article:

The modulus operator in TradingView Pine

Practically all TradingView scripts contain operators. An operator is a code element that performs an operation on one or multiples values, and those values are called operands (Sharp, 2013; Stephens, 2014). TradingView Pine has four basic arithmetic operators for addition (+), subtraction (-), division (/), and multiplication (*).

When we perform integer division in TradingView any fractional remainder is lost (see Pine Script Language Tutorial, n.d.). That means 10 / 7 returns 1 instead of 1.4286. But sometimes we actually want to calculate the remainder of integer division instead of throwing it away. For that we use the modulus operator (%). This operator takes two operands and returns the remainder of integer division (Pine Script Language Tutorial, n.d.; TradingView, n.d.). As such, this operator is occasionally called the remainder operator in other programming languages (Sharp, 2013).

For example, 5 % 3 returns 2 (5 / 3 = 1 with a remainder of 2) while 25 % 3 results in 1 (25 / 3 = 8, remainder 1). Such expressions are read as “twenty-five modulo three equals one”, or, for short, “twenty-five mod three” (Liberty & MacDonald, 2009).

With the modulus operator we can easily find the nth occurrence of something. In other programming languages it’s primarily used with loops (e.g., see Liberty & Cadenhead, 2011; Liberty & MacDonald, 2009). Since TradingView Pine doesn’t have any loops, the use of the modulus operator is limited. But as we’ll see in the examples below, there are still situations where this is a useful operator to know.

A basic example of TradingView’s modulus operator

To get a better idea of how the modulus operator works, let’s look at the following example:

study(title="Modulus - example 1")

plot(series=n % 6)

We start with the mandatory study() function to define the script’s properties. Then with plot() we draw a line with the result of 6 modulo n. That latter n variable returns the current bar number (TradingView, n.d.). When we add this script to the chart, it looks like:

TradingView example of modulus

Here we see that the result of the modulus operator ranges between 0 and 5. If we look more closely, we’ll see that the modulus operator returns 0 for every bar number that divides evenly into 6:

Bar number Expression Result
0 0 % 6 0
1 1 % 6 1
2 2 % 6 2
3 3 % 6 3
4 4 % 6 4
5 5 % 6 5
6 6 % 6 0
7 7 % 6 1
8 8 % 6 2
9 9 % 6 3
10 10 % 6 4
11 11 % 6 5
12 12 % 6 0
13 13 % 6 1
14 14 % 6 2
15 15 % 6 3
16 16 % 6 4
17 17 % 6 5
18 18 % 6 0

Highlighting every nth bar with TradingView’s modulus operator

To expand on the previous example, we can use the modulus operator to highlight every 7th price bar like this:

study(title="Modulus - example 2", overlay=true)

highlightColour = (n % 7 == 0) ? yellow : na

barcolor(color=highlightColour)
bgcolor(color=highlightColour, transp=70)

After using study() to set the script’s properties we create a variable named highlightColour. This variable’s value is set by the conditional ternary operator (?:). That operator assigns our variable a colour of yellow when n modulo 7 equals (==) 0. Should that modulus expression return something else than 0, we set the variable to na. When it comes to colours, na acts as the default colour (Pine Script Language Tutorial, n.d.), meaning that it won’t have an effect on the chart’s visual appearance.

The next statement uses barcolor(), a function that sets the colour of price bars (TradingView, n.d.). Its color argument is set to the highlightColour variable. With that all price bars either have their default colour (due to na) or are coloured yellow. Then we set the chart’s background colour to the same variable with bgcolor() (Pine Script Language Tutorial, n.d.). This latter function has a transp argument that defines the background transparency, ranging from 0 (not transparent) to 100 for fully transparent (TradingView, n.d.). We set that option to 70 here for a light-yellow background.

This example has the following effect when added to a chart:

Highlighting every nth bar in TradingView

Finding even and odd numbers with TradingView’s modulus operator

When we perform modulo 2 on a number, the % operator returns 0 when the number is even (Sempf, Sphar, & Davis, 2010). With that we can use the modulus operator to find even and odd numbers. For instance:

study(title="Modulus - example 3", overlay=true)

evenBar = (n % 2 == 0) ? 1 : -1

plotarrow(series=evenBar, colorup=blue, colordown=yellow,
     maxheight=15)

We first specify the indicator’s settings with study() here. Then we create the evenBar variable and give it one of two values with the conditional ternary operator (?:). This operator evaluates whether the current bar number (n) modulo 2 equals (==) 0. When that’s the case, evenBar is given a value of 1; otherwise it’s assigned -1.

The positive and negative values in evenBar are then used with plotarrow(). That function draws up arrows when its series argument is positive and down arrows when it’s negative (TradingView, n.d.). So based on the values that we put into evenBar, up arrows are shown on even bar numbers and down arrows on odd bars. And with colorup the up arrows are coloured blue with colordown sets the down arrows to yellow. By setting maxheight to 15 pixels, all arrows have the same modest size.

Added to a chart, this example looks like:

Highlighting even and odd bars in TradingView

See the arithmetic operators in TradingView for more operators. These mathematical operators are, just like other operators, governed by the order of operations in Pine.

Summary

The modulus operator (%) returns the remainder of integer division, a value that’s thrown away when dividing two integers with the division operator (/). With modulus we can easily find the nth occurrence of something by checking whether number % n equals 0. Another use of the modulus operator is checking for even (or odd) numbers, in which case number % 2 equals 0.


References

Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. Indianapolis, 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.

Pine Script Language Tutorial (n.d.). Retrieved on August 13, 2015, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

TradingView (n.d.). Script Language Reference Manual. Retrieved on September 2, 2015, from https://www.tradingview.com/study-script-reference/