Introduction to TradingView's operators
Introduction to TradingView’s operators
Section titled “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.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Language Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Operator sampler", overlay=false)
priceChange = close - close[1] // arithmeticaboveEMA = close > ta.ema(close, 20) // comparisonlongSignal = aboveEMA and ta.rsi(close, 14) < 70 // logical
plot(priceChange, "ΔClose")plotshape(longSignal ? 1 : na, title="Signal", location=location.top)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- 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.*).
Quick Reference
Section titled “Quick Reference”| Operator | Purpose | Notes |
|---|---|---|
+, -, *, /, % | Arithmetic | % returns the remainder of division. |
>, <, >=, <=, ==, != | Comparison | Return true/false series. |
and, or, not | Logical | Operate on booleans; short-circuit evaluation. |
:= | Reassignment (mutable) | Works with var variables. |
? : | Ternary operator | condition ? valueIfTrue : valueIfFalse. |
+=, -=, *=, /= | Compound assignment | Modify variables in place. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Start with arithmetic building blocks
Use standard arithmetic to derive values from series.spread = high - lowmidpoint = (high + low) / 2percentageMove = (close / close[1] - 1) * 100 -
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) -
Chain logical operators and ternaries
Combine multiple booleans to define entries, exits, and filter criteria.allowLong = isBreakout and not isPullbacklabelText = allowLong ? "Go" : "Wait"
Example Playbook
Section titled “Example Playbook”//@version=5indicator("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 > slowEMAvolatility = atr / close
volFilterLow = volatility < 0.015volFilterHigh = 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))- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- 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.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My logical condition never returns true
Section titled “My logical condition never returns true”Print intermediate booleans (e.g., with plotshape or the Console) to ensure each comparison behaves as expected before combining them.
:= gives an error
Section titled “:= gives an error”Only var variables can be reassigned. Declare the variable with var type name = initialValue before using :=.
% returns negative numbers
Section titled “% returns negative numbers”The remainder carries the sign of the dividend. Wrap with math.abs if you need a positive result.
Key Takeaways
Section titled “Key Takeaways”- 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.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.