Overview of Arithmetic Operators in TradingView PineReturned Values from Arithmetic OperatorsUsing Addition and Subtraction in TradingView PinePerforming Multiplication and Division in TradingView PineAdditional Arithmetic OperationsString ConcatenationModulus OperationSummaryCitations
Â
Â
Â
Arithmetic operators are essential components of TradingView scripts. They perform basic mathematical operations: addition (
+
), subtraction (-
), multiplication (*
), and division (/
).Â
Overview of Arithmetic Operators in TradingView Pine
TradingView supports four primary arithmetic operators:
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... |
These operators typically require two operands. However, addition and subtraction can also be applied to a single operand: the unary
+
returns the operand unchanged, while unary -
negates the operand.For example, if x = 10 and y = -5:
Operator | Expression | Result |
Addition ( + ) | +x | 10 |
ã…¤ | +y | -5 |
Subtraction ( - ) | -x | -10 |
ã…¤ | -y | 5 |
Â
Returned Values from Arithmetic Operators
The returned value from arithmetic operations depends on the operand types. Two notable cases affect the expected outcome:
- Integer Division: When dividing two integers using
/
, the result is rounded towards zero, discarding any fractional part. For instance, 23 / 7 yields 3, while 23 / 7.0 gives 3.2857.
- NaN Values: If any operand is NaN (not a number), the result will also be NaN.
Â
Using Addition and Subtraction in TradingView Pine
Here’s an example demonstrating the use of addition and subtraction:
//@version=5 indicator(title="[https://pinescripttutorial.com/] Arithmetic Operators - Example 1") var upVolume = 0.0 var downVolume = 0.0 if close > open upVolume := upVolume[1] + volume else if close < open downVolume := downVolume[1] + volume plot(upVolume, color=color.green) plot(downVolume, color=color.red)
In this script, we define two persistent variables to track volume for upward and downward price movements based on bar closes.
Â
Here's a detailed explanation of each part:
//@version=5
: This line specifies that the script uses Pine Script version 5.
indicator(title="[https://pinescripttutorial.com/] Arithmetic Operators - Example 1")
: This function declares the script as an indicator and sets its title.
var upVolume = 0.0
andvar downVolume = 0.0
: These lines initialize two variables,upVolume
anddownVolume
, with initial values of 0.0. Thevar
keyword makes these variables persistent, meaning they retain their values across bar updates.
- The
if
statement block:if close > open upVolume := upVolume[1] + volume else if close < open downVolume := downVolume[1] + volume
This block checks if the current bar's close price is higher than its open price. If true, it adds the current bar's volume toupVolume
. If the close is lower than the open, it adds the volume todownVolume
. The[1]
suffix refers to the previous bar's value.
plot(upVolume, color=color.green)
andplot(downVolume, color=color.red)
: These lines plot the accumulated up and down volumes on the chart. Up volume is displayed in green, and down volume in red.
This script essentially tracks and visualizes the cumulative volume for bullish (green) and bearish (red) price movements over time, providing a visual representation of volume distribution based on price action.
Â
Â
Performing Multiplication and Division in TradingView Pine
Here’s how multiplication and division are implemented:
//@version=5 indicator(title="[https://pinescripttutorial.com/] Arithmetic Operators - Example 2") volumeEMA = ta.ema(volume, 30) volumeRatio = volume / volumeEMA highVolume = 1.75 * volumeEMA plotColour = volume > highVolume ? color.orange : color.navy plot(volumeRatio, style=plot.style_histogram, color=plotColour, linewidth=4)
This example calculates the exponential moving average of volume over the last 30 bars and determines whether the current volume exceeds a defined threshold.
Â
Here's a detailed explanation of each part:
//@version=5
: This line specifies that the script uses Pine Script version 5.
indicator(title="[https://pinescripttutorial.com/] Arithmetic Operators - Example 2")
: This function declares the script as an indicator and sets its title.
volumeEMA = ta.ema(volume, 30)
: This line calculates the exponential moving average (EMA) of the volume over the last 30 bars using theta.ema()
function.
volumeRatio = volume / volumeEMA
: This calculates the ratio of the current volume to the EMA of volume. This is an example of division.
highVolume = 1.75 * volumeEMA
: This sets a threshold for high volume at 1.75 times the EMA of volume. This is an example of multiplication.
plotColour = volume > highVolume ? color.orange : color.navy
: This is a ternary operator. It checks if the current volume is greater than the high volume threshold. If true, it sets the plot color to orange; otherwise, it sets it to navy.
plot(volumeRatio, style=plot.style_histogram, color=plotColour, linewidth=4)
: This plots the volume ratio as a histogram. The color of the histogram is determined by theplotColour
variable, and the line width is set to 4.
This script essentially visualizes the relationship between current volume and its recent trend (represented by the EMA). It highlights periods of unusually high volume with a different color, providing a quick visual indicator of volume spikes relative to recent activity.
Â
Additional Arithmetic Operations
Besides basic arithmetic, you can concatenate strings using the addition operator (
+
) or compute remainders with the modulus operator (%
).Â
Let's explore some examples of string concatenation and modulus operations in TradingView Pine:
Â
String Concatenation
In Pine, you can use the addition operator (
+
) to concatenate strings://@version=5 indicator("[https://pinescripttutorial.com/] String Concatenation Example") firstName = "John" lastName = "Doe" fullName = firstName + " " + lastName label.new(bar_index, high, text=fullName)
This script creates a label with the concatenated full name "John Doe".
Â
Modulus Operation
The modulus operator (
%
) is used to find the remainder after division://@version=5 indicator("[https://pinescripttutorial.com/] Modulus Example") barNumber = bar_index % 10 isMultipleOf10 = barNumber == 0 bgColor = isMultipleOf10 ? color.new(color.blue, 70) : na bgcolor(bgColor)
Â
This script colors every 10th bar with a light blue background, demonstrating the use of the modulus operator to identify specific bars based on their position.
These additional arithmetic operations expand the capabilities of TradingView Pine scripts, allowing for more complex string manipulations and cyclic calculations.
Â
Summary
In TradingView Pine, the four arithmetic operators—addition (
+
), subtraction (-
), multiplication (*
), and division (/
)—are fundamental for performing calculations. While addition and subtraction can operate on a single operand, the results depend on operand types, particularly regarding NaN values and integer division behavior. Understanding these operators is crucial for effective scripting in TradingView Pine.Â