documentation/cookbook/sql/finance/donchian-channels.md
Donchian Channels plot the highest high and lowest low over a period, creating a channel that tracks price range. Breakouts above the upper channel or below the lower channel often signal trend continuation.
You want to identify breakout levels and trading ranges. Moving averages smooth price but don't show clear breakout levels. Donchian Channels show exactly where price needs to go to break out of its recent range.
DECLARE
@symbol := 'EURUSD',
@lookback := '$now - 1M..$now'
WITH channels AS (
SELECT
timestamp,
symbol,
close,
max(high) OVER (
PARTITION BY symbol
ORDER BY timestamp
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW
) AS upper_channel,
min(low) OVER (
PARTITION BY symbol
ORDER BY timestamp
ROWS BETWEEN 19 PRECEDING AND CURRENT ROW
) AS lower_channel
FROM market_data_ohlc_15m
WHERE symbol = @symbol
AND timestamp IN @lookback
)
SELECT
timestamp,
symbol,
round(close, 5) AS close,
round(upper_channel, 5) AS upper_channel,
round(lower_channel, 5) AS lower_channel,
round((upper_channel + lower_channel) / 2, 5) AS middle_channel
FROM channels
ORDER BY timestamp;
The query calculates:
:::note Turtle Trading Donchian Channels were famously used by the Turtle Traders. Their system entered on 20-day breakouts and exited on 10-day breakouts in the opposite direction. :::
:::info Related documentation