Enums
An enum is a fixed set of named values. Use one when a script has a few modes, and you want the settings gear to offer them as a dropdown.
//@version=5
indicator("Average type", overlay = true)
enum Kind
ema = "Exponential"
sma = "Simple"
wma
kind = input.enum(Kind.ema, "Average")
len = input.int(20, "Length")
avg = switch kind
Kind.ema => ta.ema(close, len)
Kind.sma => ta.sma(close, len)
=> ta.wma(close, len)
plot(avg, "Average")
Defining one
enum Name is followed by an indented list of values. Each is a name, or a name with a title in quotes: sma = "Simple". The title is what people see in the dropdown and what str.tostring gives. Without a title, the name is used. An enum is defined at the top level of the script.
Using the values
- Write a value as
Kind.ema. - Compare with
==and!=, or choose withswitch, as above. - A value can be a field of a type, a function argument, or a map key.
input.enum(default, title, options)makes the dropdown. Passoptions = [Kind.ema, Kind.sma]to offer only some values. The default has to be one of them.
The saved setting is the value's name, so renaming a value in the script resets that setting to the default.