Skip to content
TradequixDocsOpen the terminal
OverviewStart here, search, progress
  • Introduction
  • Quickstart
  • How a script runs
  • Types and qualifiers
  • Operators, conditionals and loops
  • Functions and methods
  • Objects and enums
  • Arrays, matrices and maps
  • Strings and colours
  • Time and sessions
  • Bar states
  • Plotting
  • Inputs
  • Technical analysis
  • Drawings and tables
  • Multiple timeframes
  • Strategies
  • Alerts in scripts
  • Repainting
  • The Pine editor
  • Indicator settings
  • Coming from TradingView
  • The data
  • Publish and sell
  • Catalogue and community
  • Your dashboard and earnings
  • Errors and debugging
  • Limits
  • Coverage
  • Built-in catalogue
  • Cookbook
  • Write Pine with AI
  • FAQ
  • Glossary
  • Cheat sheet
  • Function reference
  • Will it run here?
  • Error index

Language

Operators, conditionals and loops

Arithmetic and logic operators, the ternary, if and switch as expressions, and for and while loops with their limits.

View as MarkdownOpen in ClaudeOpen in ChatGPT

Operators#

KindOperators
Arithmetic+ - * / %
Comparison== != > < >= <=
Logicand, or, not
Ternarycond ? a : b
Historyx[n]
Assignment= declares, := reassigns, += -= *= /= update

Use := to change a variable after declaring it; = a second time declares a new one and is an error in the same scope.

if and switch return values#

Both can be used as expressions: the last line of the branch is the value.

RunnableTrend state
1//@version=62indicator("Trend state")3fast = ta.ema(close, 12)4slow = ta.ema(close, 26)5state = if fast > slow and close > fast6 27else if fast > slow8 19else10 -111plot(state, "State", style = plot.style_stepline, color = state > 0 ? color.teal : color.red)

switch picks the first matching case; a case with no expression is the default:

pine
1kind = input.string("EMA", options = ["SMA", "EMA"])2avg = switch kind3 "SMA" => ta.sma(close, 20)4 => ta.ema(close, 20)

Loops#

for i = 0 to n counts (inclusive), with an optional by step. for x in array walks an array. while cond repeats. break and continue work as you expect.

RunnableUp closes in the last 20 bars
1//@version=62indicator("Up closes in the last 20 bars")3count = 04for i = 0 to 195 if close[i] > close[i + 1]6 count += 17plot(count, "Up closes", color = color.teal)

Tip

Most loops over history have a built-in that is faster and clearer: this one is math.sum(close > close[1] ? 1 : 0, 20). Reach for a loop when no built-in fits.

Loop limits#

The engine stops a loop that runs past its iteration ceiling with "Loop exceeded maximum iterations", and a whole run must finish in 8 seconds. Bound every loop by a length, never by "all history". See Limits.

PreviousTypes and qualifiersNext Functions and methods

On this page

  • Operators
  • if and switch return values
  • Loops
  • Loop limits

Use with AI

View as MarkdownOpen in ClaudeOpen in ChatGPT