Skip to content

Introduction to TradingView's operators

TL;DR
Pine Script ships with arithmetic (+, -, *, /, %), comparison (>, ==, etc.), logical (and, or, not), and assignment/ternary operators—combine them to calculate values and control flow in a single expression.

DifficultyBeginner
Time to implement10-15 min
CategoryLanguage Basics
//@version=5
indicator("Operator sampler", overlay=false)
priceChange = close - close[1] // arithmetic
aboveEMA = close > ta.ema(close, 20) // comparison
longSignal = aboveEMA and ta.rsi(close, 14) < 70 // logical
plot(priceChange, "ΔClose")
plotshape(longSignal ? 1 : na, title="Signal", location=location.top)
Tip. Pine uses the keywords `and`, `or`, and `not` for logical operations—there is no `&&`/`||` shorthand.

Operators are the glue of Pine Script: every condition, calculation, and assignment runs through them. Knowing which operator to reach for keeps your code concise, readable, and free from off-by-one or precedence mistakes.

  • Arithmetic, comparison, logical, and ternary operators with Pine-specific syntax.
  • Operator precedence and how parentheses help you control evaluation order.
  • Practical patterns for chaining operators in trading conditions.
  • Ways to combine operators with built-in functions (e.g., ta.*).
OperatorPurposeNotes
+, -, *, /, %Arithmetic% returns the remainder of division.
>, <, >=, <=, ==, !=ComparisonReturn true/false series.
and, or, notLogicalOperate on booleans; short-circuit evaluation.
:=Reassignment (mutable)Works with var variables.
? :Ternary operatorcondition ? valueIfTrue : valueIfFalse.
+=, -=, *=, /=Compound assignmentModify variables in place.
  1. Start with arithmetic building blocks
    Use standard arithmetic to derive values from series.

    spread = high - low
    midpoint = (high + low) / 2
    percentageMove = (close / close[1] - 1) * 100
  2. Add comparisons for decision-making
    Use comparison operators to transform numeric results into booleans.

    isBreakout = close > ta.highest(high, 20)
    isPullback = close <= ta.ema(close, 34)
  3. Chain logical operators and ternaries
    Combine multiple booleans to define entries, exits, and filter criteria.

    allowLong = isBreakout and not isPullback
    labelText = allowLong ? "Go" : "Wait"
//@version=5
indicator("Operator-powered regime filter", overlay=true, max_labels_count=500)
fastEMA = ta.ema(close, 21)
slowEMA = ta.ema(close, 55)
atr = ta.atr(14)
trendUp = fastEMA > slowEMA
volatility = atr / close
volFilterLow = volatility < 0.015
volFilterHigh = volatility > 0.04
tradeState = trendUp and volFilterLow ? "Trending" : not trendUp and volFilterHigh ? "Chop" : "Neutral"
bgcolor(tradeState == "Trending" ? color.new(color.green, 85) : tradeState == "Chop" ? color.new(color.red, 85) : na)
plot(fastEMA, "Fast EMA", color=color.new(color.blue, 0))
plot(slowEMA, "Slow EMA", color=color.new(color.orange, 0))
if barstate.islast
label.new(bar_index, close, tradeState, style=label.style_label_left,
textcolor=color.white,
bgcolor=tradeState == "Trending" ? color.new(color.green, 60) : tradeState == "Chop" ? color.new(color.red, 60) : color.new(color.gray, 60))
Why this works.
  • Arithmetic derives key metrics (EMA spread, ATR percentage).
  • Comparisons and logical operators classify the environment.
  • Ternaries convert boolean decisions into chart-ready labels and colours.
  • Pine evaluates multiplication/division before addition/subtraction—use parentheses when expressions get long.
  • Comparing floats can yield unexpected results due to precision; use tolerances when necessary (math.abs(value) < 1e-6).
  • Compound assignment (+=, -=) only works with mutable (var) variables.
  • Ternary chains are powerful but can hurt readability—consider storing intermediate booleans instead of nesting too deeply.

Print intermediate booleans (e.g., with plotshape or the Console) to ensure each comparison behaves as expected before combining them.

Only var variables can be reassigned. Declare the variable with var type name = initialValue before using :=.

The remainder carries the sign of the dividend. Wrap with math.abs if you need a positive result.

  • Operators are fundamental to every Pine Script calculation and condition.
  • Combine arithmetic and comparisons to generate actionable booleans.
  • Logical operators and ternaries let you express complex trading rules succinctly.
  • Parentheses and helper variables keep multi-operator expressions readable and maintainable.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.