> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom types and enums

> Declaring your own object type, creating instances with new, reading and changing their fields, keeping them in arrays, and declaring enums.

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.

```lipi-fragment theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
type Swing {
    float price
    int barIndex = 0
}
```

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`:

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Defaults")
type Counter {
    int start = 2 * 3
}
plot(close)
```

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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last swing high", overlay=true)
type Swing {
    float price
    int barIndex = 0
}
static Swing lastHigh = Swing.new(high, bar_index)
if high > lastHigh.price {
    lastHigh := Swing.new(high, bar_index)
}
plot(lastHigh.price, title="Swing high")
```

* 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](https://gocharting.com/docs/scripting/evaluation/persistence.md)).
* 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).

```lipi-fragment theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
type Level {
    float price
}
a = Level.new(close)
b = a
c = a.copy()
b.price := 1.0
```

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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Recent swing highs", overlay=true)
type Swing {
    float price
    int barIndex
}
static array<Swing> swings = array.new<Swing>(0)
if high == talib.highest(high, 10) {
    array.push(swings, Swing.new(high, bar_index))
    if array.length(swings) > 5 {
        array.shift(swings)
    }
}
if barstate.islast and array.length(swings) > 0 {
    Swing newest = array.get(swings, array.length(swings) - 1)
    label.new(newest.barIndex, newest.price, text="Last swing")
}
```

Writing `array.get(swings, 0).price` is refused (messages in [Names and conventions](https://gocharting.com/docs/scripting/language/names-and-conventions.md)); 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](https://gocharting.com/docs/scripting/language/custom-functions.md)). `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.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Moving average by mode", overlay=true)
enum Speed {
    Fast = "Fast (10)"
    Slow = "Slow (50)"
}
speed = input.enum(Speed.Fast, "Speed")
length = speed == Speed.Fast ? 10 : 50
plot(talib.sma(close, length), title="Average")
```

* 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`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.