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
Writetype, 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.
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:
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. statickeeps 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)leavesbarIndexat 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, callcopy, which copies the fields (a field that holds an array or a drawing
still refers to the same array or drawing).
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 asarray<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.
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
Anenum 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 ascasevalues in aswitch. - A name that is not a member is refused: Enum
Speedhas no memberMedium. - 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.