Donchian Channels (DONCHIAN)
Summary
Donchian Channels: three overlap lines built from rolling price extrema. The upper band is the highest high and the lower band the lowest low over the period; the middle line is their midpoint. Richard Donchian's original four-week rule — generally credited as the first published systematic trend-following system — buys a break above the high of the preceding weeks and sells a break below their low.
Formula
Window = the optInTimePeriod bars ending at the current bar
Upper = Highest High of Window Lower = Lowest Low of Window Middle = (Upper + Lower) / 2
Notes
- The window includes the current bar, matching TradingView (
ta.highest/ta.lowest), NinjaTrader, ta4j, pandas-ta and every other library that ships Donchian Channels. - A breakout rule compares the current bar against the previous bar's band —
High[t] > Upper[t-1]— which is where the one-bar offset belongs. ReadingUpper[t]againstHigh[t]can never signal, becauseHigh[t]is inside the window that produced it. - Upper, Middle and Lower are bit-identical to
MAX(high, N),MIDPRICE(N)andMIN(low, N). DONCHIAN computes all three in one pass under the name users look for. - The middle line is the channel midpoint, not a moving average of price.
- No smoothing or recursion is involved, so there is no unstable period: outputs are exact from the first bar.
Inputs
inHigh— High price of each barinLow— Low price of each bar
Outputs
outRealUpperBand— Highest high of the windowoutRealMiddleBand— Midpoint of the upper and lower bandsoutRealLowerBand— Lowest low of the window
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 2–100000 | Number of bars in the extrema window |
Properties
Numerical Stability: Start-Independent
| ✅ Overlap Input i |
| ☐ Independent Y-Axis |
| ☐ Candlestick |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
Implementation
TA-Lib Definition: donchian.c · donchian.yaml
| Native | File |
|---|---|
| C | ta_DONCHIAN.c |
| Rust | donchian.rs |
| Java | Core_DONCHIAN.java |
TA-Lib is also available for Python, R and more using a wrapper.