Bentrix Docs
HomePricingDashboard

Fractals

Bill Williamsoverlayin every package

Bill Williams' Fractals — a marker on any bar whose high is higher than the two bars on each side, and the mirror of that on lows.

It measures nothing continuous: it labels five-bar local extrema. The reading is the level of the marked high or low, and the fact that a fractal cannot be confirmed until two further bars have printed, so the most recent two bars can never carry one.

01The study, live

Live figureScroll to zoom, drag the bars to pan.
bar —O — H — L — C —first value at —

Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.

02Its parts, named

An up fractala high with two lower highs on eachsideThe bars it readseach bar's open, high, low and closeA down fractala low with two higher lows on eachside
Computed by Bentrix’s own code over the sample bars. It is drawn on once, as you reach the figure.

03What you see

Arrows above and below certain bars: a bar whose high is higher than the two bars on each side (an up fractal), or whose low is lower (a down fractal).

04Where it comes from

Bill Williams described the fractal in Trading Chaos (1995): the smallest pattern that marks a local turning point — a bar whose high stands above the highs of the two bars on each side, or whose low stands below their lows.

05The formula

up fractal at t:Ht>Ht−j  and  Ht>Ht+jfor j=1,…,k
down fractal at t:Lt<Lt−j  and  Lt<Lt+jfor j=1,…,k
Ht, Ltthe high and the low of bar t
kthe bars on each side (2 by default: the five-bar fractal)

That is the five-bar fractal when k=2. An up fractal at bar t when Ht>Ht±1 and Ht>Ht±2; a down fractal likewise on lows. A bar that only equals a neighbour is not a fractal, and the mark can be drawn only once bar t+k has closed.

06A worked example

The sample bars used here are eight: closes 20, 21, 23, 22, 24, 25, 24, 26; highs 21, 22, 24, 24, 25, 26, 26, 27; lows 18, 19, 20, 21, 21, 23, 23, 23; opens 19, 20, 21, 23, 22, 24, 25, 24; volumes 100, 120, 150, 90, 140, 160, 80, 170.

Look at the highs: 21, 22, 24, 24, 25, 26, 26, 27. An up fractal needs a high above the two highs before it and the two after it.

Bar 6 has a high of 26. The two before it are 24 and 25 — lower. The two after are 26 and 27 — not lower. No fractal. In a steady rise like these bars there is none at all: every high is overtaken.

Change bar 8's high to 25 and bar 7's to 25, and bar 6 (26) tops two bars on each side: an up fractal is marked over bar 6 — but only once bar 8 has closed, two bars after the peak.

07Built in Assay

The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.

THE SCRIPT’S GRAPH
//@version=6
indicator("Bill Williams' Fractals", overlay = true)
// A swing high: the high two bars back stands strictly above the two highs on each side of it.
swingHigh = high[2] > high[4] and high[2] > high[3] and high[2] > high[1] and high[2] > high
// A swing low: the mirror, on the lows.
swingLow = low[2] < low[4] and low[2] < low[3] and low[2] < low[1] and low[2] < low
// Known only once the two bars after it have closed, so each mark is drawn two bars back.
plotshape(swingHigh, "Up fractal", shape.triangleup, location.abovebar, offset = -2)
plotshape(swingLow, "Down fractal", shape.triangledown, location.belowbar, offset = -2)

Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.

08Readings — what traders look for

Williams used fractals as the levels a breakout must pass; traders read them as recent swing points.

How a fractal as a level fails. The high of an up fractal is read as a local resistance and a move through it as a breakout. In a range fractals form every few bars on both sides and are passed constantly.

Confirmation delay. A fractal is drawn over a bar that is already two bars old. On a chart of the past it looks as if the turn was marked as it happened; live, the mark arrives after it.

09In different markets and timeframes

Fractals mark local turning points two bars after they happen. In trends they are sparse and their breaks follow the trend; in ranges they form on both sides every few bars.

10With other studies

Read with the Alligator: Williams counted only fractals beyond the Alligator's teeth.

11What it does not tell you

!

A fractal appears two bars late: it needs two bars after it. Until then, the bar cannot be marked — the figure shows the confirmed ones.

12Inputs and defaults

Fractals (2, Chart)settings card
1Bars on each side2
2TimeframeChart
3Wait for the candle to closeOn
  1. 1
    Bars on each side — 2How many bars on each side the middle bar must top (or undercut). 2 is Williams' five-bar fractal; a larger number marks only larger swings, and confirms them that many bars late.
  2. 2
    Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
  3. 3
    Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.

The settings in use

  • In use2 bars each side is Williams' five-bar fractal; a larger number marks only larger swings and confirms later.

13Limits and common misreadings

It appears late, by as many bars as it needs on its right side.

It is drawn back in time, on the peak bar, which flatters it on any historical chart.

Frequent. With two bars a side, roughly one bar in five or six is a fractal of one kind or the other in a sideways market.

14Practise it in the laboratory

Replay a chart bar by bar with Fractals on and watch where each mark appears relative to the newest bar: always two bars back, never under it.

Open the laboratory

16In Bentrix

  • From the menu: a chart's Indicators button › Bill Williams › Fractals, or type Fractals in its search.
  • Its settings: the settings card opens when it is added; its legend's name opens it again.

17From an agent or a program

Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "fractals"}).

The study, as a chart files it

This is a new Fractals’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.

{
  "applied": "close",
  "convention": "talib",
  "kind": "fractals",
  "settings": {
    "bars": 2,
    "shape": "sides"
  }
}

An agent

An agent calls the MCP tool with it:

{
  "name": "study_values",
  "arguments": {
    "bench": "bentrix://lab/\u2026/bench/\u2026",
    "symbol": "EURUSD",
    "timeframe": "H1",
    "limit": 5,
    "study": {
      "applied": "close",
      "convention": "talib",
      "kind": "fractals",
      "settings": {
        "bars": 2,
        "shape": "sides"
      }
    }
  }
}

A program

A program does the same in its language — bench and market as in the built-in indicators guide:

Python

values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "talib", "kind": "fractals", "settings": {"bars": 2, "shape": "sides"}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
    print("line:", line["line"])
    print("  newest values:", line["values"])

JSJavaScript

const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "talib", "kind": "fractals", "settings": { "bars": 2, "shape": "sides" } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
  console.log("line:", line.line);
  console.log("  newest values:", line.values);
}

C#C#

var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "talib", ["kind"] = "fractals", ["settings"] = new Dictionary<string, object?> { ["bars"] = 2, ["shape"] = "sides" } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
    Console.WriteLine($"line: {line!["line"]!}");
    Console.WriteLine($"  newest values: {line!["values"]!}");
}

GOGo

values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "talib", "kind": "fractals", "settings": map[string]any{"bars": 2, "shape": "sides"}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
	fmt.Println("line:", at(line, "line"))
	fmt.Println("  newest values:", at(line, "values"))
}

Java

Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "talib", "kind", "fractals", "settings", obj("bars", 2, "shape", "sides"))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
    System.out.println("line:" + " " + at(line, "line"));
    System.out.println("  newest values:" + " " + at(line, "values"));
}

C++C++

auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "talib"}, {"kind", "fractals"}, {"settings", Json::Object{{"bars", 2}, {"shape", "sides"}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
    std::cout << "line:" << " " << line["line"].dump() << std::endl;
    std::cout << "  newest values:" << " " << line["values"].dump() << std::endl;
}

Julia

values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "talib", "kind" => "fractals", "settings" => Dict{String,Any}("bars" => 2, "shape" => "sides")); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
    println("line:", " ", line["line"])
    println("  newest values:", " ", line["values"])
end

RR

values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "talib", kind = "fractals", settings = list(bars = 2, shape = "sides")), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
  cat("line:", format(line$line), "\n")
  cat("  newest values:", format(line$values), "\n")
}

RRust

let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "talib", "kind": "fractals", "settings": {"bars": 2, "shape": "sides"}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
    println!("line: {}", line["line"]);
    println!("  newest values: {}", line["values"]);
}

Reading the answer

  • times — the candles’ open times, in milliseconds since 1970 (UTC), oldest first.
  • lines — one entry per line, in this order, each with its line name and its values; values[i] belongs to the candle at times[i].
  • The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
  • null is a candle with no value yet — the warm-up, about 1 × the period at the start of the history.
lineWhat it is

In a strategy

A strategy cannot read it: its value at a candle depends on candles after it, which a strategy never sees. The strategy document refuses it.

18Sources

  1. Williams, Bill. Trading Chaos. Wiley, 1995.

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed