Median (median)

[[median]] returns the middle value of source over a window of length values. Once the values are sorted, half are at or below the median and half are at or above it. When the window contains an even number of values, the block returns the average of the two middle values.

The median depends on the rank of each value, not its numerical distance from the center. It is therefore less affected than an average by a single value far away from the rest. In a window containing the closes 100, 101, 102, 103, and 130, the average is 107.2 while the median remains 102: the spike to 130 does not change which value sits in the middle of the sorted series.

Block declaration

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

Examples

Minimal setup

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

[[median]]
id     = "median"
length = 20

Specific source

This block computes the median on the hl2 source (the average of the high and the low) rather than on close.

[[median]]
id     = "median"
source = "hl2"
length = 20

Source from another indicator

This block takes the median of the series produced by the rsi block over 10 candles, smoothing out its sharpest spikes.

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

[[median]]
id     = "rsi_median"
source = "rsi"
length = 10

Finding the optimal length

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

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

Dedicated symbol and timeframe

This block computes the median 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.

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

Parameters

ParameterDescription
id
 String
 Required
Unique name identifying the median series; the block exposes one numeric series with this identifier.
source
 String
 or Array
 Optional
Input series used for the calculation.
Accepted forms: source = "hl2" or source = ["close", "hl2"].
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 candles in the window over which the median is computed; 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 median 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:

[[median]]
id     = "median"
length = 20

Then:

VariableDescription
median or median[0]
Decimal
Current median value.
median[n]
Decimal
Median value from n candles ago.
median.length
Decimal
Window length in use.
median.source
String
Input series name (for example close, hl2, or another indicator’s id).
median.symbol
String
Symbol the input data is read from.
median.timeframe
String
Timeframe the median 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.
  • Numeric variables support arithmetic, comparisons, and logical operators.
  • Text variables are strings intended for equality/inequality checks only.