Understanding how operators work and their precedence is crucial when programming in TradingView Pine Script. Operators allow us to perform operations on variables and values, but knowing in what order these operations are carried out is key to writing accurate and functional scripts.
Operator Priority: A Tabular Overview
Operator priority dictates the sequence in which operations are executed in an expression. Here’s a systematic breakdown:
Priority | Operator | Description | Example |
---|---|---|---|
10 | ( ) | Parentheses (overrides priority) | ((value1 – value2) + (value3 – close[2])) / 5 |
9 | [ ] | History referencing operator | close[2], variableX[9] |
8 | + (unary) | Unary addition (no change) | +ta.mom(close, 10), +volumeDelta |
– (unary) | Unary negation | -ta.ema(high, 3), -maxDrawdown | |
not | Logical NOT | not (high > high[1]), not enterPosition | |
7 | * | Multiplication | valueMedian * 2, 10 * volumeDelta |
/ | Division | low / high, 9 / 2 | |
% | Modulus (remainder division) | 9 % 3, bar_index % 20 == 0 | |
6 | + (binary) | Binary addition | 10 + 6, (close + close[1]) / 2 |
– (binary) | Binary subtraction | peak – trough, ta.ema(close, 10) – ta.ema(close, 5) | |
5 | > | Greater than | 10 > 9, peak > peak[1] |
< | Less than | 9 < 10, ta.mom(close, 10) < ta.mom(close, 5)[1] | |
>= | Greater than or equal to | close <= ta.sma(close, 10), open <= close | |
<= | Less than or equal to | peak <= peak[5], 19 <= 20 | |
4 | == | Equality | peak = ta.highest(high, 20), trough = low[2] |
!= | Inequality | close != close[4], variableY != 100 | |
3 | and | Logical AND | newPeak and volumeRise, 10 > 2 and 9 != 8 |
2 | or | Logical OR | enterPosition or stopHit, not (9 < 3 or 500 > 8) |
1 | ?: | Conditional ternary | highestPeak == true ? 200 : 3, close < open ? close : close |
Practical Example: Correcting Operator Priority
Consider the following script designed to plot the midpoint of a bar:
Original Attempt at Calculating a Bar’s Midpoint
//@version=5 indicator(title="Bar midpoint", overlay=true) plot(high + low / 2, color=color.blue, linewidth=2)
At first glance, this script attempts to plot the midpoint by adding the high
to half of the low
value. However, due to the precedence of division over addition, the script doesn’t calculate the midpoint as intended. Instead, it divides the low
by 2 and then adds the high
, leading to inaccurate results.
To rectify this, we enclose high + low
in parentheses, ensuring they are evaluated as a unit before division:
//@version=5 indicator(title="Bar midpoint", overlay=true) plot((high + low) / 2, color=color.blue, linewidth=2)
With this adjustment, the script accurately calculates and plots the midpoint of a bar.
Key Takeaways
- Operator Precedence: Knowing the order in which operations are executed is essential for writing accurate Pine Script code.
- Parentheses for Clarity: Use parentheses to explicitly define the order of operations, especially in complex expressions.
- Correct Application: Applying these principles can prevent common errors and ensure your script behaves as intended.
By familiarizing yourself with the operator precedence and correctly applying it in your TradingView Pine Script programming, you can enhance the accuracy and functionality of your trading strategies and indicators.