Mode (mode)

[[mode]] returns the most frequent value in its source among the last length valid observations. Its numeric output is the value that dominates the window at each candle.

In a discrete, rounded, or bucketed series, this captures the recurring level rather than the window’s midpoint or average. When several values share the highest frequency, the block always returns the smallest one.

Block declaration

A strategy can contain multiple [[mode]] blocks. Each block produces one numeric series: the mode of the source over the configured window.

Examples

Minimal setup

This block computes the mode of close over 20 values. Because source is omitted, the block uses the default value "close".

[[mode]]
id     = "mode"
length = 20

Specific source

This block computes the mode on the hlc3 source rather than on close.

[[mode]]
id     = "mode"
source = "hlc3"
length = 20

Source from another indicator

This block computes the most frequent value of the series produced by the rsi block over 10 values.

[[rsi]]
id     = "rsi"
length = 14

[[mode]]
id     = "rsi_mode"
source = "rsi"
length = 10

Finding the optimal length

This block explores a range of values for length to identify the best-performing window.

[[mode]]
id           = "mode"
length.start = 10
length.stop  = 30

Dedicated symbol and timeframe

This block computes the mode from the daily series of BINANCE:ETHUSDT, independently of the primary symbol and timeframe defined in [backtest]. See Exchanges, Symbols and Timeframes for alignment rules.

[[mode]]
id        = "mode"
length    = 20
symbol    = "BINANCE:ETHUSDT"
timeframe = "D"

Parameters

ParameterDescription
id
 String
 Required
Unique name identifying the mode series; the block exposes one numeric series with this identifier.
source
 String
 or Array
 Optional
Input series used for the calculation.
Accepted forms: source = "hlc3" or source = ["close", "hlc3"].
Each value can be either a standard price source (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume) or the id of another indicator.
Default value: "close"
length
 Integer
 Required
Number of values in the window used to compute the mode; must be ≥ 1.

Usage:
• Fixed: length = value
• Grid search:
length.start = min_value
length.stop = max_value
length.step = value (optional, default 1)
symbol
 String
 or Array
 Optional
Market symbol(s) providing the source series when source holds standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).
If source mixes standard prices and indicator ids, symbol applies only to combinations based on standard prices.
If source contains only indicator ids, symbol is ignored.
For symbol format and alignment rules, see Exchanges, Symbols and Timeframes.
timeframe
 String
 or Array
 Optional
Timeframe on which this indicator is computed.
If timeframe is omitted, the computation uses the grid’s main timeframe defined in [backtest].
For accepted formats and timeframe alignment rules, see Exchanges, Symbols and Timeframes.

Available variables

The mode block exposes one numeric series, its calculation length, and metadata about the input source, symbol, and timeframe. Use these identifiers directly in your expressions.

Assume the block is configured as:

[[mode]]
id     = "mode"
length = 20

Then:

VariableDescription
mode or mode[0]
Decimal
Current mode value.
mode[n]
Decimal
Mode value from n candles ago.
mode.length
Decimal
Window length in use.
mode.source
String
Input series name (for example close, hlc3, or another indicator’s id).
mode.symbol
String
Symbol the input data is read from.
mode.timeframe
String
Timeframe the mode is computed on.

Notes

  • During the warm-up phase, while the window does not yet hold length values, the series is NaN.
  • Candles whose source is NaN are not counted: the window keeps the last length valid values.
  • When several values share the highest frequency, the smallest value is returned.
  • Numeric variables support arithmetic, comparisons, and logical operators.
  • Text variables are strings intended for equality/inequality checks only.