Donchian Channels

[[donchian]] produces two bounds that describe the recent price range. On each candle, the upper band is the highest high over the last length candles, while the lower band is the lowest low over the same window. The current candle is included in the calculation, so a new extreme immediately moves the corresponding band.

The channel widens as its two bounds move apart and narrows as they move closer together. A band remains flat until its extreme changes, even while price continues to move inside the range. Unlike a moving average, the Donchian Channel does not smooth a series: it retains its upper and lower limits directly over the observed period.

Block declaration

A strategy can contain multiple [[donchian]] blocks. Each block generates two distinct numeric series: the upper band and the lower band of the Donchian Channel. These two series are identified with the upper_id and lower_id fields.

The [[donchian]] block does not have a source parameter because the Donchian Channel is always computed from the candle high and low prices.

Examples

Minimal setup

This block computes both Donchian Channel bands with the default length 20.

[[donchian]]
upper_id = "donchian_up"
lower_id = "donchian_lo"

Custom fixed setup

This block uses a custom length value.

[[donchian]]
upper_id = "donchian_up"
lower_id = "donchian_lo"
length   = 22

Search for the optimal length

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

[[donchian]]
upper_id     = "donchian_up"
lower_id     = "donchian_lo"
length.start = 15
length.stop  = 25

Dedicated symbol/timeframe

This block computes the Donchian channel on BINANCE:BTCUSDT and timeframe 60. Because length is omitted, the block uses the default length 20.

[[donchian]]
upper_id  = "donchian_up"
lower_id  = "donchian_lo"
symbol    = "BINANCE:BTCUSDT"
timeframe = "60"

Parameters

ParameterDescription
upper_id
 String
 Required
Identifier for the upper band series.
lower_id
 String
 Required
Identifier for the lower band series.
symbol
 String
 or Array
 Optional
Market symbol(s) from which this block reads its candles (high, low). If 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.
length
 Integer
 Optional
Lookback window; must be ≥ 1.
Default value: 20.

Usage:
• Fixed: length = value
• Grid search:
 – length.start = min_value
 – length.stop = max_value
 – length.step = value (optional, default 1)

Available variables

You can use the identifiers below directly in your expressions. The Donchian Channel block exposes two numeric series (upper band and lower band), along with their parameters and calculation context.

Assume the block is configured as follows:

[[donchian]]
upper_id = "donchian_up"
lower_id = "donchian_lo"

Then:

VariableDescription
donchian_up or
donchian_up[0]
Decimal
Current upper band value.
donchian_up[n]
Decimal
Upper band value from n candles ago.
donchian_lo or
donchian_lo[0]
Decimal
Current lower band value.
donchian_lo[n]
Decimal
Lower band value from n candles ago.
donchian_up.length
Decimal
Donchian lookback length used (exposed on the upper id).
donchian_up.high_source
donchian_up.low_source
donchian_lo.high_source
donchian_lo.low_source
String
Names of the input sources used (high and low) on each output.
donchian_up.symbol
donchian_lo.symbol
String
Symbol used.
donchian_up.timeframe
donchian_lo.timeframe
String
Timeframe used.

Notes

  • Numeric variables support arithmetic, comparisons, and logical operators.
  • Text variables are intended for equality/inequality checks.