> ## 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.

# How to group values in your own type

> Declaring a type, creating one instance in a static variable, reading its fields and updating them with :=.

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

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Highest high so far", overlay=true)
type Peak {
    float price
    int barIndex = 0
}
static Peak top = Peak.new(high, bar_index)
if high > top.price {
    top.price := high
    top.barIndex := bar_index
}
plot(top.price, title="Highest high")
if barstate.islast {
    label.new(top.barIndex, top.price, "Peak " + str.tostring(top.price), style=label.style_label_down)
}
```

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

> [!WARNING]
>
> 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 `=`.
>
> ```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
> //@lipi=2
> indicator("Field assigned with =", overlay=true)
> type Peak {
>     float price
> }
> static Peak top = Peak.new(high)
> top.price = high
> plot(top.price)
> ```

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](https://gocharting.com/docs/scripting/language/custom-types.md).


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