Skip to Main Content

Adaptive Donchian Channel

An indicator designed to show how to build adaptive ranges that only adjust width if other conditions are met.

Adaptive Donchian Channel
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0
// (C) HeWhoMustNotBeNamed
		
describe_indicator('Adaptive Donchian Channel', 'price', { shortName: 'ADC' });
 
const length = input('Length', 20, { min: 2, max: 199 });
 
input('Offset', 0, { min: -50, max: 50 });
 
const donchianTop = highest(high, length);
const donchianBottom = lowest(low, length);
 
const top = series_of(null);
const bottom = series_of(null);
const middle = series_of(null);
 
for (let candleIndex = 1; candleIndex < high.length; candleIndex++) {
	const isBreakout = high[candleIndex] > top[candleIndex - 1] || low[candleIndex] < bottom[candleIndex - 1];
 
	top[candleIndex] = isBreakout ? donchianTop[candleIndex] : top[candleIndex - 1];
	bottom[candleIndex] = isBreakout ? donchianBottom[candleIndex] : bottom[candleIndex - 1];
	middle[candleIndex] = (top[candleIndex] + bottom[candleIndex]) / 2;
}
 
const topLine = paint(top, 'Top', '#2ca599');
const middleLine = paint(middle, { hidden: true });
const bottomLine = paint(bottom, 'Bottom', '#ee5451');
 
fill(topLine, middleLine, '#2ca599', undefined, 'Upper');
 
fill(middleLine, bottomLine, '#ee5451', undefined, 'Lower');