Appearance
Keywords
What does each keyword do? Each one starts a declaration or a block, and this page gives its rule in a few lines. and, or and not are on Operators, and const, simple and series on Qualifiers.
var
var declares a variable that keeps its value from bar to bar. Its initial value is set the first time the line runs, and each later bar starts from what the last bar left. Without var, a declaration runs again on every bar.
pine
//@version=6
indicator("Bars that closed up")
var int ups = 0 // set once, then kept
if close > open
ups += 1
plot(ups)In a function, each place the function is called from keeps its own var. See var, and every call keeps its own state.
varip
Like var, and it also keeps what the updates of a live bar change, which var sets back before each update. On history each bar runs once, so the two read the same there; see on a live bar. A leak-safe AmenoScript run refuses varip. See leak-safe AmenoScript.
if
Runs its block when the condition is true, else the first else if whose condition is, else the else block.
As a value, if gives the last expression of the branch that ran, and na where none ran, or false for a bool. See a branch as a value. A ta.* call inside a branch runs only on the bars that branch runs, so it sees only those bars: see a call in a branch.
pine
//@version=6
indicator("Trend word", overlay = true)
fast = ta.ema(close, 10) // computed on every bar, outside the if
slow = ta.ema(close, 30)
word = if fast > slow
"up"
else
"down"
if barstate.islast
label.new(bar_index, high, word)switch
Picks one arm. With an expression after switch, it runs the first arm whose value equals it. Without one, it runs the first arm whose condition is true. An arm with nothing before => is the default.
As a value, switch gives the arm's value, and na where no arm matched. See switch.
pine
//@version=6
indicator("Average by name", overlay = true)
kind = input.string("EMA", "Average", options = ["SMA", "EMA", "WMA"])
sma = ta.sma(close, 20) // each runs on every bar, so none misses bars
ema = ta.ema(close, 20)
wma = ta.wma(close, 20)
avg = switch kind
"SMA" => sma
"EMA" => ema
=> wma
plot(avg)for
for i = from to end runs its block once for each i from from to end, both included. It counts down when end is below from. by step sets the step's size, and the ends set the direction. Keep step above 0: here a negative step counts as its size and a step of 0 runs no pass, but TradingView stops a script whose step is 0 or less. See counting with for.
for x in a runs once for each element of an array, for [i, x] in a gives each index too, and for [k, v] in m gives each key and value of a map, in the order they were put. break leaves the loop and continue goes on to the next pass. See walking a collection.
A loop stops after 100,000 passes, without an error, on every engine. See how long a loop may run.
A loop can't be a value here
Pine lets you assign a loop, x = for i = 1 to 3, to get its body's last value. This engine refuses that as a syntax error. Keep the value in a variable the body assigns.
while
Runs its block again and again while its condition is true, testing before each pass. break and continue work as in for, and it too stops after 100,000 passes. Like for, it can't be assigned as a value here. See looping on a condition.
import
Brings a library's exported functions and types into a script, under an alias: import Me/Kit/1 as kit. See Imports and libraries, and importing a library.
export
Marks a function, a method, a type or an enum in a library as one an importing script can use. See writing a library.
method
Declares a function you call with a dot on its first argument: method double(float x) => x * 2 lets you write close.double(). The first parameter needs a type. See methods.
type
Declares a type of your own, with fields:
pine
//@version=6
indicator("Swing high")
type Swing
float price = na
int bar = na
var last = Swing.new()
if ta.pivothigh(high, 3, 3) > 0
last.price := high[3]
last.bar := bar_index - 3
plot(last.price, "Last swing high")Swing.new() makes one, each field at its default or at what you pass. Fields read and change with a dot. A variable holds a reference to the object, so two names can share one object: copy() makes a separate one. See shared or copied. Each field takes a literal default.
enum
Declares a closed list of named values, each with an optional title:
pine
enum Side
buy = "Buy"
sellSide.buy is a value of type Side, and str.tostring of one gives its title, or its name where it has none. input.enum turns an enum into a dropdown. See enums and input.enum.
true and false
The two bool values. A bool is never na: where a condition reads a na, it reads false. See a bool is never na.