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.
Chart
Chart metadata i
Outputs
outSwingHigh:display_shift·pattern_bool·zero·positiveoutSwingLow:display_shift·pattern_bool·zero·positive
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
The function's flags declare which values it can return: rW7.
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 |
| ✅ Display Shift i |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double high[252]; /* your data, oldest first */
double low[252]; /* your data, oldest first */
int swingHigh[252], swingLow[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_FRACTAL( 0, n - 1, high, low, 2, 2, &outBegIdx, &outNBElement, swingHigh, swingLow );
printf( "%d values from bar %d:\n", outNBElement, outBegIdx );
printf( "swingHigh = [%d, ..., %d]\n", swingHigh[0], swingHigh[outNBElement - 1] );
printf( "swingLow = [%d, ..., %d]\n", swingLow[0], swingLow[outNBElement - 1] );use ta_lib::Core;
let high = vec![0.0; 252]; // your data, oldest first
let low = vec![0.0; 252]; // your data, oldest first
let mut swing_high = vec![0i32; 252];
let mut swing_low = vec![0i32; 252];
let core = Core::new();
let range = core.fractal(0, high.len() - 1, &high, &low, 2, 2,
&mut swing_high, &mut swing_low)?;
println!("{} values from bar {}:", range.count, range.beg_idx);
println!("swing_high = [{}, ..., {}]", swing_high[0], swing_high[range.count - 1]);
println!("swing_low = [{}, ..., {}]", swing_low[0], swing_low[range.count - 1]);import io.github.talib.Core;
import io.github.talib.OutRange;
import java.util.Locale;
double[] high = new double[252]; // your data, oldest first
double[] low = new double[252]; // your data, oldest first
int[] swingHigh = new int[252];
int[] swingLow = new int[252];
Core core = Core.DEFAULT;
OutRange r = core.fractal(0, high.length - 1, high, low, 2, 2, swingHigh, swingLow);
System.out.printf(Locale.ROOT, "%d values from bar %d:%n", r.count(), r.begIdx());
System.out.printf(Locale.ROOT, "swingHigh = [%d, ..., %d]%n", swingHigh[0], swingHigh[r.count() - 1]);
System.out.printf(Locale.ROOT, "swingLow = [%d, ..., %d]%n", swingLow[0], swingLow[r.count() - 1]);using System;
using TALib;
var high = new double[252]; // your data, oldest first
var low = new double[252]; // your data, oldest first
var swingHigh = new int[252];
var swingLow = new int[252];
var core = Core.Default;
OutRange r = core.Fractal(0, high.Length - 1, high, low, 2, 2, swingHigh, swingLow);
Console.WriteLine($"{r.Count} values from bar {r.BegIdx}:");
Console.WriteLine(FormattableString.Invariant($"swingHigh = [{swingHigh[0]}, ..., {swingHigh[r.Count - 1]}]"));
Console.WriteLine(FormattableString.Invariant($"swingLow = [{swingLow[0]}, ..., {swingLow[r.Count - 1]}]"));import { fractal } from 'ta-lib';
const high = new Float64Array(252); // your data, oldest first
const low = new Float64Array(252); // your data, oldest first
const { begIdx, outSwingHigh, outSwingLow } = fractal({ high, low }, {
leftBars: 2,
rightBars: 2,
});
console.log(outSwingHigh.length + ' values from bar ' + begIdx + ':');
console.log('outSwingHigh = [' + outSwingHigh[0] + ', ..., ' + outSwingHigh[outSwingHigh.length - 1] + ']');
console.log('outSwingLow = [' + outSwingLow[0] + ', ..., ' + outSwingLow[outSwingLow.length - 1] + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
high := make([]float64, 252) // your data, oldest first
low := make([]float64, 252) // your data, oldest first
swingHigh := make([]int32, 252)
swingLow := make([]int32, 252)
core := talib.NewCore()
r, err := core.FRACTAL(0, len(high)-1, high, low, 2, 2, swingHigh, swingLow)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d:\n", r.NBElement, r.BegIdx)
fmt.Printf("swingHigh = [%d, ..., %d]\n", swingHigh[0], swingHigh[r.NBElement-1])
fmt.Printf("swingLow = [%d, ..., %d]\n", swingLow[0], swingLow[r.NBElement-1])Implementation
Added in 0.8.1 (ta-lib-python 0.8.1).
TA-Lib Definition: fractal.c · fractal.yaml
| Native | File |
|---|---|
| C | ta_FRACTAL.c |
| Rust | fractal.rs |
| Java | Core_FRACTAL.java |
| C# | Core_FRACTAL.cs |
| TypeScript | fractal.ts |
| Go | fractal.go |
Python: talib.FRACTAL (ta-lib-python 0.8.1 or later), see ta-lib-python.
TA-Lib is also available for 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