Declaring an enum
enum Trend
up = "Bullish"
down = "Bearish"
flat = "Neutral"
- Each member has an identifier (used in code) and an optional title (shown to users in dropdowns).
- Omitting the title defaults it to the identifier:
enum Signal
buy
sell
hold
Reading a member
t = Trend.up
if t == Trend.up
log.info("bull")
Enum values are compared with ==/!=. They are not strings — you cannot concatenate them with + "foo". Call str.tostring(t) to get the display title, or str.tostring(t, format.id) to get the identifier.
As inputs
Pass an enum as an input to get a dropdown automatically:
mode = input.enum(Trend.up, "Trend mode")
The dialog renders one row per member, labelled with the title.
Exporting from a library
An enum used in an exported function's signature must itself be exported:
//@version=1
library("Signals")
export enum Signal
buy = "Buy"
sell = "Sell"
hold = "Hold"
export decide(float fast, float slow) =>
fast > slow ? Signal.buy : fast < slow ? Signal.sell : Signal.hold
Iteration
Enum members can be collected into an array via enumname.all:
for s in Trend.all
log.info(str.tostring(s))
enumname.all yields members in declaration order.