Defines a user-defined type: a named set of typed fields that you create instances of with TypeName.new().
type Name followed by an indented list of fields declares a new type. Each field has a type, a name and optionally a default, as in float price = na. Create an instance with Name.new(price = close) or positionally Name.new(close), read fields with a dot, and assign them with :=.
Instances are objects held by reference: assigning one to a second variable shares it, and Name.copy() makes an independent shallow copy. Objects can sit in arrays and maps and be passed to functions and methods. Fields may use varip, and the whole type may be exported from a library with export type.
Syntax
[export ]type <UDT_identifier> [varip ]<field_type> <field_name> [= <value>] …Remarks
- A field can hold another object, an array or a map, so a type is the way to group related series.
- Type names are capitalised by convention. A field with no default starts as na.
- On Sidechair: types support defaults, .new, .copy and methods. Field types are not checked.
Example
//@version=6
indicator("Type", overlay = true)
type Level
float price
string name = "level"
int touches = 0
var lvl = Level.new(high)
if high > lvl.price
lvl.price := high
lvl.touches += 1
plot(lvl.price, "Highest high", color.red)
plot(lvl.touches, "Raised", color.gray, style = plot.style_histogram)See also
Runs on Sidechair