Understanding Operator Priority in Pine ScriptOperator Precedence in TradingView PineModifying Operator Precedence Using ParenthesesNesting Parentheses in TradingView PineSummaryCitations
Â
Â
Â
Understanding Operator Priority in Pine Script
TradingView Pine scripts use operators to perform complex operations. An operator is a code element that acts on one or more values, called operands. An expression is a code snippet that produces a value and often contains operators. For example,
close + high
calculates the sum of a bar's close and high prices, while open > open[1]
returns true if the current open price is higher than the previous open.When an expression includes multiple operators, they're evaluated in a specific order based on operator priority. This priority determines which operator is executed first, ensuring consistent expression evaluation. For instance, in
2 + 10 * 5
, multiplication has higher priority than addition, resulting in 52 rather than 60.Operator Precedence in TradingView Pine
The following table outlines the operator priority in TradingView Pine, where higher priority operators are calculated before those with lower priority:
Priority | Operator | Name | Example |
10 | ( ) | Parentheses; overrides operator priority | ((34 - 3) + (8 - close[2])) / 5 |
9 | [ ] | History referencing operator | close[2] , myVariable |
8 | + | Unary addition; leaves operand unchanged | +ta.mom(close, 10) , +volumeChange |
ã…¤ | - | Unary subtraction; returns the opposite of operand | -ta.ema(high, 3) , -maxLoss |
ã…¤ | not | Logical NOT; returns logical opposite | not (high > high[1]) , not enterLong |
7 | * | Multiplication operator | hl2 * 2 , 10 * volumeDifference |
ã…¤ | / | Division operator | low / high , 9 / 2 |
ã…¤ | % | Modulus operator; returns remainder of division | 9 % 3 , bar_index % 20 == 0 |
6 | + | Binary addition | 10 + 6 , (close + close[1]) / 2 |
ã…¤ | - | Binary subtraction | high - low , ta.ema(close, 10) - ta.ema(close, 3) |
5 | > | Greater than operator | 10 > 9 , high > high[1] |
ã…¤ | < | Less than operator | 9 < 1 , ta.mom(close, 10) < ta.mom(close, 10)[1] |
ã…¤ | >= | Greater than or equal to operator | close <= ta.sma(close, 10) , open <= close |
ã…¤ | <= | Less than or equal to operator | high <= high , 19 <= 20 |
4 | == | Equality operator | high == ta.highest(high, 20) , low == low[2] |
ã…¤ | != | Not equal to operator | close != close[4] , myVariable != 100 |
3 | and | Logical AND operator | newHigh and volumeIncrease , 10 > 2 and 9 != 8 |
2 | or | Logical OR operator | enterLong or stopTriggered , not (9 < 3 or 500 > 8) |
1 | ?: | Conditional ternary operator | highestHigh ? 200 : 3 , close < open ? close : close |
Operators can be unary (acting on a single operand) or binary (acting on two operands). For example, unary subtraction is represented as
-close
, while binary subtraction is expressed as high - low
.When operators share the same priority level, they're evaluated from left to right. For instance, in the expression
9 - 3 + 22 - 3
, the calculations proceed as follows: 9 - 3 = 6
, then 6 + 22 = 28
, and finally 28 - 3 = 25
.Â
Modifying Operator Precedence Using Parentheses
Parentheses can be used to alter the default order of operations. Since parentheses have the highest priority, expressions within them are evaluated first. For example:
- In the expression
10 + 9 * close
, the multiplication occurs before addition due to higher priority.
- Conversely,
(10 + 9) * close
first adds10
and9
, then multiplies byclose
.
Â
Consider this example for calculating a bar's midpoint:
//@version=5 indicator(title="[Pine Script Tutorial] Bar Midpoint", overlay=true) plot(high + low / 2, color=color.red, linewidth=1)
Â
This code attempts to plot the midpoint but results in an incorrect calculation because division takes precedence over addition. To correct this:
//@version=5 indicator(title="[Pine Script Tutorial] Bar Midpoint", overlay=true) plot((high + low) / 2, color=color.red, linewidth=1)
Now it accurately computes and plots the bar's midpoint.
Â
Nesting Parentheses in TradingView Pine
Complex expressions may require nested parentheses. TradingView evaluates these from the innermost set outward. For example:
x = (7 % 3) * (4 + (6 / 2))
The evaluation sequence is as follows:
- Calculate 6 / 2 = 3.
- Evaluate 4 + 3 = 7.
- Compute 7 % 3 = 1.
- Finally, multiply: 1 * 7 = 7.
Thus, x results in 7.
Â
Summary
Understanding operator priority in Pine Script is crucial for writing accurate and efficient trading strategies. By mastering the precedence of operators, you can ensure that your expressions are evaluated correctly and produce the intended results. Remember that parentheses can be used to override default priorities when needed, allowing for more complex and precise calculations in your scripts.
Â
Citations
Â