Williams Fractal (FRACTAL)
Summary
Williams Fractal: a causal swing-pivot detector. A bar is a swing high when its high strictly exceeds the highs of the optInLeftBars bars before it and the optInRightBars bars after it; a swing low is the mirror on the lows. Bill Williams' original is the symmetric five-candle case; independent left and right arms generalise it.
The right arm cannot be known until it has closed, so the value is written on the bar that completes the pivot, optInRightBars bars after the pivot itself. Each output value therefore describes the bar optInRightBars back, not the bar it is written at: a flag at output index k names input bar outBegIdx + k - optInRightBars, whose price is inHigh[...] / inLow[...] at that index. The display shift both outputs report, -optInRightBars, is that offset: a chart draws each flag that many bars to the left.
The two outputs are independent flags rather than one signed value, because an outside bar can be a swing high and a swing low at once.
Formula
With L = optInLeftBars, R = optInRightBars and pivot c = i - R:
swingHigh(i) = 100 if High[c] > High[j] for every j in [c-L, c+R] other than c, else 0.
swingLow(i) = 100 if Low[c] < Low[j] for every j in [c-L, c+R] other than c, else 0.
Notes
- Strict on both sides: a bar tied with any other bar of its window is not a pivot. TradingView's Pine runtime differs: its
ta.pivothigh/ta.pivotlowlet a tie with an older bar stand and let a tie with a newer bar cancel, i.e. non-strict left and strict right — so a plateau Pine reports as a pivot is not one here. - Each output is decided on its own side: a high tied with any other high in the window forces
outSwingHighto 0 while leavingoutSwingLowfree to fire 100, and the mirror holds. Only a window flat in both series emits 0 on both.
Inputs
inHigh— High price seriesinLow— Low price series
Outputs
outSwingHigh— 100 when the baroptInRightBarsback is a strict swing high, 0 otherwiseoutSwingLow— 100 when the baroptInRightBarsback is a strict swing low, 0 otherwise
Output Values
How to read these values from the output's flags: rW8.
outSwingHigh
| Value | Meaning |
|---|---|
| 0 | The bar optInRightBars back is not a swing high |
| 100 | The bar optInRightBars back is a strict swing high |
outSwingLow
| Value | Meaning |
|---|---|
| 0 | The bar optInRightBars back is not a swing low |
| 100 | The bar optInRightBars back is a strict swing low |
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInLeftBars | integer | 2 | 1–100000 | Bars before the pivot that it must strictly dominate |
optInRightBars | integer | 2 | 1–100000 | Bars after the pivot that it must strictly dominate, and the delay before the verdict is reported |
Properties
Numerical Stability: Start-Independent
| ☐ Overlap Input |
| ✅ Independent Y-Axis i |
| ☐ Candlestick |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ✅ Display Shift i |
| ☐ Uses Transcendental |
Implementation
TA-Lib Definition: fractal.c · fractal.yaml
| Native | File |
|---|---|
| C | ta_FRACTAL.c |
| Rust | fractal.rs |
| Java | Core_FRACTAL.java |
| C# | Core_FRACTAL.cs |
TA-Lib is also available for Python, R and more using a wrapper.
Aliases
Williams Fractal, Fractals, Swing High, Swing Low, Pivot High, Pivot Low
See Also
AROON · MAXINDEX · MININDEX · MINMAXINDEX
References
- Bill Williams, Trading Chaos, John Wiley & Sons (1995)
- TradingView: Williams Fractal