Home » Pinescript Syntax » Understanding the Priority of Operators in TradingView Pine Script

Understanding the Priority of Operators in TradingView Pine Script

Photo of author
Published on

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:

10( )Parentheses (overrides priority)((value1 – value2) + (value3 – close[2])) / 5
9[ ]History referencing operatorclose[2], variableX[9]
8+ (unary)Unary addition (no change)+ta.mom(close, 10), +volumeDelta
– (unary)Unary negation-ta.ema(high, 3), -maxDrawdown
notLogical NOTnot (high > high[1]), not enterPosition
7*MultiplicationvalueMedian * 2, 10 * volumeDelta
/Divisionlow / high, 9 / 2
%Modulus (remainder division)9 % 3, bar_index % 20 == 0
6+ (binary)Binary addition10 + 6, (close + close[1]) / 2
– (binary)Binary subtractionpeak – trough, ta.ema(close, 10) – ta.ema(close, 5)
5>Greater than10 > 9, peak > peak[1]
<Less than9 < 10, ta.mom(close, 10) < ta.mom(close, 5)[1]
>=Greater than or equal toclose <= ta.sma(close, 10), open <= close
<=Less than or equal topeak <= peak[5], 19 <= 20
4==Equalitypeak = ta.highest(high, 20), trough = low[2]
!=Inequalityclose != close[4], variableY != 100
3andLogical ANDnewPeak and volumeRise, 10 > 2 and 9 != 8
2orLogical ORenterPosition or stopHit, not (9 < 3 or 500 > 8)
1?:Conditional ternaryhighestPeak == true ? 200 : 3, close < open ? close : close
Operator Priority in Pinescript

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

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:

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.

Leave a Comment