Topics like health, travel, finance, home, living, education and careers | Media Search Home

The modulus operator (%) in TradingView Pine

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
  • A basic example of TradingView"s modulus operator
  • Highlighting every nth bar with TradingView"s modulus operator
  • Finding even and odd numbers with TradingView"s modulus operator
  • Summary
# 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 the plot() function we draw a solid 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 modulusTradingView 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 numberExpressionResult
00 % 60
11 % 61
22 % 62
33 % 63
44 % 64
55 % 65
66 % 60
77 % 61
88 % 62
99 % 63
1010 % 64
1111 % 65
1212 % 60
1313 % 61
1414 % 62
1515 % 63
1616 % 64
1717 % 65
1818 % 60
nth bar with TradingView"s modulus operator""># 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 script has the following effect when added to a chart:

Highlighting every nth bar in TradingViewHighlighting 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 TradingViewHighlighting 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/

Published October 31, 2015.
# Related TradingView tutorials
  • TradingView"s conditional ternary operator explained

    In this TradingView programming article we discuss the conditional ternary operator (?:), which acts as an if/else statement in Pine.

  • The arithmetic operators (+, -, *, /) in TradingView Pine

    In this TradingView programming article we discuss the arithmetic operators and how they work, including several code examples.

  • Comparing values for true or false with TradingView"s comparison operators

    In this TradingView programming article we look at the comparison operators, including several script examples that show how these operators work.

  • Introduction to TradingView"s operators

    This article provides an overview of all programming articles and examples that explain how operators work in TradingView Pine.

  • Combining strings with TradingView"s addition operator

    In this TradingView programming article, we discuss how the addition operator (+) can also combine strings together.

« All TradingView operators articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved