Operators that are used in practically any TradingView scripts are the arithmetic operators. What are their features and how do they work?

In this article:

Arithmetic operators in TradingView Pine

TradingView has four arithmetic operators that perform the standard mathematical operations of addition (+), subtraction (-), multiplication (*), and division (/) (Pine Script Language Tutorial, n.d.). An operator is, by the way, a code element that performs an operation on one or more values to create a result (Stephens, 2014). We call the values that an operator works on operands (Sharp, 2013).

The simple arithmetic operators in TradingView work as you’d expect:

Operator Description Example Result
+ Addition 10 + 12.0 22.0
- Subtraction 22 - 9 13
* Multiplication 12 * -0.5 -6
/ Division 4 / 3.3 1.2121...

As the table above shows, all arithmetic operators need two operands (Pine Script Language Tutorial, n.d.). But the addition (+) and subtraction (-) operators can also be applied to one operand (TradingView, n.d.). In that case, - returns the operand’s opposite while + leaves the value unchanged.

So if we assume that x is a variable with a value of 10 and y holds a value of -5, then placing the + and - operators before these variables has the following effect:

Operator Expression Result
Addition (+) +x 10
+y -5
Subtraction (-) -x -10
-y 5

The values returned by TradingView’s arithmetic operators

The value that’s returned by the arithmetic operators depends on the type of operands (Pine Script Language Tutorial, n.d.). There are two situations in which these operators return different values than you’d expect.

First, when dividing two integers with / the fractional value is thrown away and Pine rounds towards zero. This only happens with integer division, though: when we perform division when one of the operands is a floating-point value, then the result will be a decimal answer. For example, dividing 23 by 7 returns 3, but dividing 23 by 7.0 (or 23.0 by 7) gives 3.2857.

Second, when one operand is NaN (“not a number”, a value that’s caused by invalid operations; Albahari & Albahari, 2012), then the arithmetic operators return NaN also (Pine Script Language Tutorial, n.d.).

Using the addition and subtraction operators in TradingView Pine

An example of the addition (+) and subtraction (-) operators is the following:

study(title="Arithmetic operators - example 1")

upVolume = iff(close > open, 
     nz(upVolume[1]) + volume, 

downVolume = iff(close < open,
     nz(downVolume[1]) + volume,

plot(series=upVolume, color=green)
plot(series=downVolume, color=red)

We start here with the study() function to configure the indicator’s settings. Then we create the upVolume variable that’s given a value by the iff() function. This function evaluates a condition and then either returns its second value (when the condition is true) or its third (when the condition is false) (Pine Script Language Tutorial, n.d.).

In our case, we use iff() to check whether the current bar’s close (close) is greater than (>) the bar’s opening price (open). When that’s the case, we add the current bar’s volume (volume) to the variable’s value on the previous bar (upVolume[1]). And when the close isn’t greater than the open, we use the history referencing operator ([ ]) to repeat the previous bar’s value.

Since both of those operations return a NaN value on the first bar (because there’s no bar before that; see Pine Script Language Tutorial, n.d.), we use the nz() function so that any NaN value is replaced with a 0 (TradingView, n.d.). That allows us to calculate with all previous bar values correctly.

The second variable (downVolume) is created similarly, except that here we check whether the current bar’s close is less than (<) its open. After we’ve calculated both variables, we plot them as green and red lines on the chart with the plot() function.

When this example script is added to a TradingView chart it looks like:

Example of TradingView's arithmetic operators

Performing multiplication and division in TradingView Pine

Using the / and * operators in TradingView goes like this:

study(title="Arithmetic operators - example 2")

volumeEMA = ema(volume, 30)
volumeRatio = volume / volumeEMA

highVolume = 1.75 * volumeEMA

plotColour = volume > highVolume ? orange : navy

plot(series=volumeRatio, style=histogram, color=plotColour,

We begin this example by defining the indicator’s settings with study(). The next statement calculates the exponential moving average (EMA) based on the volume data of the last 30 bars, and puts the value returned by ema() into the volumeEMA variable. We then divide the current bar’s volume (volume) by volumeEMA and assign this outcome to the volumeRatio variable.

Then we calculate the value of highVolume by multiplying volumeEMA with 1.75 (so 75% above the average volume). Now with all variables defined, we determine the plot colour with the conditional ternary operator (?:). That operator evaluates if the current bar’s volume is greater than (>) the value of highVolume. When that’s the case, the orange colour is put into the plotColour variable; otherwise, this variable is given the navy colour.

The last statement in the example calls plot() to create a histogram plot based on the volumeRatio variable. We set this plot’s colour to the plotColour variable that we’ve defined just a moment earlier.

Added to a chart, the above example looks like:

Example of TradingView's arithmetic operators

Other examples of arithmetic operators are combining strings with the addition operator (+) and using the modulus operator (%) to find the remainder of division.


The four arithmetic operators in TradingView are addition (+), subtraction (-), multiplication (*), and division (/). Each of these can work with two operands while addition and subtraction can also be applied to just one operand. The value returned by the arithmetic operators depends on the type of operands: when one operand is NaN, then the result is NaN also. And when dividing two integers any fractional remainder is thrown away; something that doesn’t happen when at least one operand is a floating-point value.


Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). 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/

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 1, 2015, from https://www.tradingview.com/study-script-reference/