Highest Bars (highestbars)

[[highestbars]] reports where the recent maximum of a series occurred, rather than returning the maximum value itself. On each candle, it finds the highest value among the last length candles and outputs its offset from the current candle.

The result is 0 when the maximum is on the current candle. Negative values point into the past: -1 is the previous candle, while -3 is three candles earlier. If several candles share the same maximum, the block returns the oldest occurrence that is still inside the window.

source determines the series being examined. With the default high source, the block locates the candle containing the highest recent high. With close or the output of another indicator, it locates the recent maximum of that series.

Block declaration

A strategy can contain multiple [[highestbars]] blocks. Each block outputs one numeric series: the offset to the highest value in the window.

Examples

Minimal setup

This block uses the implicit "high" source and returns the position of the highest value observed over the last 20 bars.

[[highestbars]]
id     = "highest_pos"
length = 20

Custom source and finding the optimal length

This block computes the position of the maximum close for each length value tested by the grid.

[[highestbars]]
id           = "close_highest_pos"
source       = "close"
length.start = 10
length.stop  = 30

Position checked in a condition

This example checks whether the highest value in the window is on the current candle.

[[highestbars]]
id     = "recent_high_pos"
length = 20

[[condition]]
id            = "current_high_is_window_high"
condition     = "recent_high_pos == 0"
next_block_id = "..."

Dedicated symbol and timeframe

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

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

Parameters

ParameterDescription
id
 String
 Required
Unique identifier for the resulting series.
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: "high"
length
 Integer
 Required
Window size; 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) used when source only consists of standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).
If source mixes standard prices and indicator ids, symbol is applied only to combinations based on standard prices.
If source contains only indicator ids, symbol is ignored.
When omitted, the block inherits the [backtest] symbol.
For symbol format, arrays, 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 highestbars block exposes one numeric series and its parameters (length, source, symbol, timeframe). These variables are available in expressions after the series has been computed.

Assume the block is configured as:

[[highestbars]]
id     = "highest_pos"
length = 20

Then:

VariableDescription
highest_pos or highest_pos[0]
Decimal
Current offset to the maximum of the window. 0 means the maximum is on the current candle; a negative value indicates how many candles separate that maximum from the current candle.
highest_pos[n]
Decimal
Offset computed n candles ago (e.g. highest_pos[1] for the previous candle).
highest_pos.length
Decimal
Effective window size.
highest_pos.source
String
Actual source id (high, close, another indicator).
highest_pos.symbol
String
Display name of the symbol feeding this block.
highest_pos.timeframe
String
Timeframe used for the computation.

Notes

The first length - 1 values are not available because the window is not yet complete. After this warm-up, [[highestbars]] returns 0 when the current source value is NaN.