The rolling-extreme formula
For bar t, let HH(N) be the highest high and LL(N) the lowest low across the latest N bars, including t. With ATR length A and positive multiplier k, rawLong = HH(N) - k × ATR(A), and rawShort = LL(N) + k × ATR(A). TradingView exposes separate extreme and ATR lengths. StockCharts documents 22 bars and 3 ATR as its default convention; those values are a starting specification, not an optimum.
- True range = max(high - low, abs(high - previous close), abs(low - previous close)). The previous close captures gaps that a high-low range misses.
- The Pine example uses ta.atr(A), the RMA of true range. After its initial average, the Wilder update is ATR[t] = ((A - 1) × ATR[t-1] + TR[t]) / A. An SMA or EMA choice changes the result.
- With previous close 100, high 108, and low 105, true range is max(3, 8, 5) = 8. If A = 4 and previous ATR = 2, the updated ATR is (3 × 2 + 8) / 4 = 3.5.
| Input | Meaning | Comparison trap |
|---|---|---|
| Extreme length N | Rolling window in chart bars | 22 hourly bars are not 22 trading days. |
| ATR length A | Smoothing window for true range | Matching N does not establish matching A or smoothing. |
| Multiplier k | Distance in ATR units from the extreme | 3 ATR below the high is not 3 ATR below entry. |
| Extreme source | High and low in this example | Highest close and lowest close define a different variant. |