Type system

Pine Script Type System Overview

Pine Script's type system is critical as it regulates what types of values can be used when calling functions. It's possible to write very basic scripts without knowing about the type system, but a thorough understanding is crucial to becoming proficient with the language and to leverage its full capabilities.

The type system employs a form-type pair to specify the type of all values, whether they're literals, a variable, the result of an expression, the value returned by functions, or the arguments used when calling a function.

Forms

Pine Script identifies when a variable’s value is known using forms:

  1. input: Values known at input time (when values are changed in a script’s β€œSettings/Inputs” tab)

  2. simple: Values known at bar zero (when the script begins execution on the chart’s first historical bar)

  3. series: Values known on each bar (any time during the execution of a script on any bar)

Note that of all these forms, only the β€œseries” form allows values to change dynamically, bar to bar, during the script’s execution over each bar of the chart’s history.

Types

Pine Script types identify the nature of a value. They are:

  1. The fundamental types: β€œint”, β€œfloat”, β€œbool” and β€œstring”

  2. The special types: β€œarray”, β€œmatrix” (SOON AVAILABLE !)

  3. User-defined types (UDTs)

  4. β€œvoid” (SOON AVAILABLE !)

Each fundamental type refers to the nature of the value contained in a variable. Variables of special types contain an ID referring to an object of the type’s name. A variable of type β€œlabel” contains an ID referring to a label, and so on. The β€œvoid” type means no value is returned.

The Pine Script compiler can automatically convert some types into others when a value is not of the required type. The auto-casting rules are: int -> float -> bool.

Usage of Forms and Types

Forms are used to define when a value is known. Below are examples of how different forms can be used:

  1. input: These values are known when the values initialized through input.*() functions are determined. These values can be modified by script users in the script’s β€œSettings/Inputs” tab.

  2. simple: These are values known only when a script begins execution on the first bar of a chart’s history, and they never change during the execution of the script.

  3. series: Values of β€œseries” form provide the most flexibility because they can change on any bar, or even multiples times during the same bar, in loops for example.

In all these forms, the usage of types is the same. They are used to specify the kind of value a variable or a function's return value can hold. For example, "int" can hold integer values, "float" can hold decimal numbers, "bool" can hold true or false, "color" can hold color values, and "string" can hold text.

Understanding the type system is key to effectively using Pine Script, as it governs how data is manipulated and processed in your scripts.

Pine Script: Understanding Types

The following guide breaks down the types in Pine Script.

Integer Type (int)

Integer literals must be written in decimal notation. Examples include 1, -1, and 750. Built-in variables like bar_index, time, timenow, time_close, or dayofmonth all return values of the int type.

Floating-point Type (float)

Floating-point literals contain a delimiter (the symbol .) and may also contain the symbol e or E. The symbol represents "multiply by 10 to the power of X", where X is the number after the symbol e. Examples are as follows:

3.14159    // Rounded value of Pi (Ο€)
-3.0
6.02e23    // 6.02 * 10^23 (a very large value)
1.6e-19    // 1.6 * 10^-19 (a very small value)

The internal precision of floats in Pine Scriptβ„’ is 1e-10.

Boolean Type (bool)

There are only two literals representing bool values: true and false. When an expression of type β€œbool” returns na and it is used to test a conditional statement or operator, the false branch is executed.

Color Type

Color literals have the following format: #RRGGBB or #RRGGBBAA. The letter pairs represent 00 to FF hexadecimal values (0 to 255 in decimal). The RR, GG, and BB pairs are the values for the color’s red, green, and blue components respectively. AA is an optional value for the color’s transparency (or alpha component) where 00 is invisible and FF is opaque. When no AA pair is supplied, FF is used.

Pine Scriptβ„’ also has built-in color constants such as color.green, color.red, color.orange, color.blue, etc.

String Type

String literals may be enclosed in single or double quotation marks. Single and double quotation marks are functionally equivalent. You can escape the string’s delimiter in the string by using a backslash. For example:

pinescriptCopy code'It\'s an example'
"The \"Star\" indicator"

You can concatenate strings using the + operator.

Plot and Hline

Pine Scriptℒ’s fill() function fills the space between two lines with a color. Both lines must have been plotted with either plot() or hline() function calls. Each plotted line is referred to in the fill() function using IDs which are of β€œplot” or β€œhline” type.

Drawings: Line, Linefill, Label, Box, and Table

Drawings in Pine Scriptβ„’ have their own type: line, linefill, label, box, table. Each drawing type has its own namespace containing all the built-in functions used to operate on each type of drawing. One of these is a new() constructor used to create an object of that type.

Arrays and Matrices

Arrays and matrices in Pine Scriptβ„’ are identified by an ID, much like drawings such as lines. The type of the ID defines the type of elements contained in the array or matrix. Array and matrix types are specified by appending a type template to the array or matrix keywords.

User-Defined Types

The type keyword allows the creation of user-defined types (UDTs) from which objects can be created. UDTs are composite types; they contain an arbitrary number of fields that can be of any type to: Assistant<|im_sep|>## Types

Int

Integer literals must be written in decimal notation, e.g., 1, -1, 750. Built-in variables like bar_index, time, timenow, time_close, or dayofmonth all return values of type "int".

Float

Floating-point literals contain a decimal point (.) and may contain the symbol e or E (representing "multiply by 10 to the power of X", where X is the number after the symbol e). For example:

pinescriptCopy code3.14159    // Rounded value of Pi (Ο€)
-3.0
6.02e23    // 6.02 * 10^23 (a very large value)
1.6e-19    // 1.6 * 10^-19 (a very small value)

In Pine Script, the internal precision of floats is 1e-10.

Bool

There are only two literals representing bool values: true and false. When an expression of type "bool" returns na and is used to test a conditional statement or operator, the "false" branch is executed.

Color

Color literals follow the format: #RRGGBB or #RRGGBBAA. Each pair of letters represent hexadecimal values (0 to 255 in decimal) for the color’s red (RR), green (GG), blue (BB) components, and an optional alpha (AA) value for transparency (00 is invisible and FF is opaque).

Here are some examples:

pinescriptCopy code#000000      // black color
#FF0000      // red color
#00FF00      // green color
#0000FF      // blue color
#FFFFFF      // white color
#808080      // gray color
#3ff7a0      // some custom color
#FF000080    // 50% transparent red color
#FF0000ff    // same as #FF0000, fully opaque red color
#FF000000    // completely transparent color

Pine Script also has built-in color constants (like color.green, color.red, color.orange, color.blue) which can be used in plot functions and others. Additionally, you can add transparency information using color.new.

String

String literals may be enclosed in single or double quotation marks. A string within double quotes may contain any number of single quotation marks and vice versa. Strings can be concatenated using the + operator.

Arrays and matrices (Soon available !)

Arrays and matrices in Pine Script are identified by an ID, like the drawing types above. The type of the ID defines the type of elements contained in the array or matrix.

User-defined types

The type keyword allows the creation of user-defined types (UDTs) from which objects can be created.

Series[T]

A series is a stream of values over the time series data, where T denotes the type of data the series holds. For example, a series can hold int, float, bool, or color. Notably, every built-in variable and function that operates on the time series returns a value of series type.

Consider an example of a series of floats:

pinescriptCopy codes = sma(close, 14)

Here, s holds a series of floating-point numbers which represent the 14-period simple moving average of the close prices. Each value in the series corresponds to a specific bar on the chart.

Remember that a value of a series variable exists for each bar in the dataset. Because of this, you can refer to the value of the series variable on any bar. Pine Script uses the [] operator for this. For example, close[1] refers to the close price of the previous bar.

Other Types

In addition to the types described above, Pine Script also includes types such as timestamp (used for date and time calculations) and syminfo (used for fetching information about the current symbol).

Last updated