Skip to main content
When several values belong together, such as a price and the bar it was seen on, declare a type and keep one instance of it. The instance holds the values; you read a field with a dot and change it with :=.
  • type Peak { ... } is declared at the top level, one field per line, before it is used. A field may have a default that is a plain value (a number, a string, true, false, a color constant, na or a built-in variable such as close).
  • Peak.new(high, bar_index) creates an instance; arguments follow the field order or may be named. A field left out takes its default, or na when it has none.
  • static creates the instance once, on the first bar, and keeps it from bar to bar. A field changed with := keeps its new value on the next bar.

The mistake to avoid

Changing a field with =. A field is changed with :=, like any existing variable. The compiler says: To assign a new value to an object’s property, use := instead of =.
An instance is shared, not copied: b = top gives a second name for the same object. Use top.copy() for an independent one. See Custom types and enums.