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:
input: Values known at input time (when values are changed in a scriptβs βSettings/Inputsβ tab)
simple: Values known at bar zero (when the script begins execution on the chartβs first historical bar)
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:
The fundamental types: βintβ, βfloatβ, βboolβ and βstringβ
The special types: βarrayβ, βmatrixβ (SOON AVAILABLE !)
User-defined types (UDTs)
β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:
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.
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.
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:
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:
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:
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:
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:
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