DONCHIAN (Donchian Channel)
The Donchian Channel plots the highest high and lowest low over a lookback period, with a midline that serves as a trend baseline.
Declaration
function init(ctx) { ctx.indicator('channel', 'DONCHIAN', { period: 50 }); }
Options
| Option | Type | Default | Description |
|---|---|---|---|
period | number | 20 | Lookback period for high/low |
Output
Returns an object with three series:
| Property | Description |
|---|---|
upper | Highest high over the period |
middle | Midpoint: (upper + lower) / 2 |
lower | Lowest low over the period |
Accessing Values
function onBar(ctx, i) { // Multi-output indicators use underscore naming const upper = ctx.ind.channel_upper[i]; const middle = ctx.ind.channel_middle[i]; const lower = ctx.ind.channel_lower[i]; }
Use Cases
Trend Direction (Baseline Strategy)
The middle line serves as a dynamic baseline for trend detection:
function init(ctx) { ctx.indicator('ema', 'EMA', { period: 21, source: 'close' }); ctx.indicator('baseline', 'DONCHIAN', { period: 50 }); } function onBar(ctx, i) { const ema = ctx.ind.ema; const baseline = ctx.ind.baseline_middle; // EMA above baseline = bullish trend if (q.crossOver(ema, baseline, i)) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'trend_bullish' }); } // EMA below baseline = bearish trend if (q.crossUnder(ema, baseline, i)) { ctx.order.close('ASSET', { signal: 'sell', reason: 'trend_bearish' }); } }
Breakout Detection
Trade breakouts above the upper channel or below the lower channel:
function onBar(ctx, i) { const close = ctx.series.close[i]; const prevClose = ctx.series.close[i - 1]; const upper = ctx.ind.channel_upper[i]; const lower = ctx.ind.channel_lower[i]; // Bullish breakout if (prevClose <= upper && close > upper) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'breakout_high' }); } }
Multi-Timeframe Baseline
Use different periods for entry and exit signals:
function init(ctx) { ctx.indicator('slowBaseline', 'DONCHIAN', { period: 50 }); // Trend ctx.indicator('fastBaseline', 'DONCHIAN', { period: 26 }); // Partial signals }
Calculation
Upper = Highest(High, period) Lower = Lowest(Low, period) Middle = (Upper + Lower) / 2
Related
- PPO Indicator - Risk oscillator often used with Donchian
- TDR Strategy - Full example using Donchian baseline
- init() - Indicator declaration
indicatordonchianchanneltrendbreakoutbaselineqsl