Variable declarations

Variable Usage in Pine Script

Overview

Variables in Pine Script are placeholders that store certain values. They need to be declared in your code prior to being used. Variable declaration could involve an optional type specification and an identifier with an assigned expression or structure.

Here's the general syntax for variable declarations:

[<declaration_mode>] [<type>] <identifier> = <expression> | <structure>

or

<tuple_declaration> = <function_call> | <structure>

In this context:

  • The pipe symbol (|) represents "or", and any part enclosed in square brackets ([]) can occur either once or not at all.

  • <declaration_mode> determines the mode of variable declaration, which can be 'var', 'varip', or none.

  • <type> is optional, as Pine Script automatically determines the type for most variable declarations.

  • <identifier> is the name of the variable.

  • <expression> can be a literal, variable, another expression, or a function call.

  • <structure> can be an if, for, while, or switch structure.

  • <tuple_declaration> is a list of variable names, separated by commas, and enclosed in square brackets ([]), e.g., [ma, upperBand, lowerBand].

Initializing with na

In most cases, explicitly stating the type is not necessary as Pine Script determines the type at compile time based on the value on the right side of the equals (=) sign. For instance:

baseLine0 = na         // This will cause a compile time error!
float baseLine1 = na   // This is correct
baseLine2 = float(na)  // This is also correct

Tuple Declarations

Function calls or structures in Pine Script can return multiple values. To store these values, a tuple declaration is used, allowing multiple variables to be declared at once. For example, the ta.bb() function returns three values:

[bbMiddle, bbUpper, bbLower] = ta.bb(close, 5, 4)

Variable Reassignment

A variable can be reassigned using the := operator, but only after it has been declared and given an initial value. Variable reassignment is a common requirement for calculations, especially when a global scope variable needs to be reassigned a new value from within a local block.

Declaration Modes

Variables can be declared with three modes: 'On each bar', 'var', and 'varip'.

On each bar

In this mode, a variable is declared and initialized on each bar if there is no explicit declaration mode, i.e., no 'var' or 'varip' keyword is used.

var

By using the 'var' keyword, a variable is initialized only once, on the first bar or the first execution of a local block, and retains its value across successive bars unless reassigned.

Note

Please be aware that the above explanations on variable declarations and reassignments in Pine Script require a good understanding of the distinction between the two, especially for those new to Pine Script. When you see lines using the := reassignment operator, the code is reassigning a value to an already declared variable. Be sure to understand this distinction as it is often a point of confusion.

Last updated