Skip to main content
A type groups values that belong together, such as a price and the bar it was seen on, into one object you can store, pass to a function and keep in an array. An enum names a fixed set of choices. Both are declared at the top level of the script.

Declare a type

Write type, a name, and a braced block with one field per line. Each field is a data type and a name, and may have a default.
A type needs at least one field. A default must be a plain value: a number, a string, true or false, a color constant, na or a built-in variable such as close. A calculation or an ordinary variable is refused, even -1 or 2 * 3:
The compiler says: Default value of property start of object Counter cannot be a function, variable or calculation. A field may hold any type: a fundamental type, a drawing such as box or label, an array<...>, chartPoint, or another type of your own.

Create an instance and use its fields

<Type>.new(...) creates an instance. Pass the fields in declaration order, or by name. A field you leave out takes its default, or na when it has none.
  • Read a field with a dot: lastHigh.price.
  • Change a field with :=, or with a compound assignment: lastHigh.price := high, counter.hits += 1.
  • static keeps the instance from one bar to the next, like any other value (see Values that persist across bars).
  • Named arguments work too: Swing.new(price=high) leaves barIndex at its default.

Instances are shared, not copied

Assigning an instance to another name does not copy it: both names refer to the same object, so a change through one is seen through the other. To get an independent object, call copy, which copies the fields (a field that holds an array or a drawing still refers to the same array or drawing).
After this, a.price is 1.0, because b is the same object, and c.price is still the close.

Keep instances in an array

An array can hold a type of your own. Declare it as array<Swing> and create it with array.new<Swing>(0). To use a field of an element, take the element into a variable first: a dot straight after a call is not accepted.
Writing array.get(swings, 0).price is refused (messages in Names and conventions); assign the element first, as above.

Types in functions

A function may take an instance as a parameter and change its fields; the caller sees the change, because the instance is shared. A function may also change the fields of an instance declared outside it. What a function may not do is reassign a variable declared outside it (see Your own functions). func countIf(Tally t, bool rising) with t.count += 1 in its body is such a function.

Enums

An enum lists named members, one per line. A member may carry a title after =; the title is the text the settings show for it in input.enum, not a value. A member without a title shows its own name.
  • Refer to a member as Speed.Fast. Members compare with == and !=, and work as case values in a switch.
  • A name that is not a member is refused: Enum Speed has no member Medium.
  • A member is not a number or a string: pass it to functions that take its enum type, and compare it, but do not give it to str.tostring.