Pivot High/Low Fractals — Market Structure Drop
# Pivot High/Low Fractals — Market Structure Drop
Structure is the alphabet of price action — until you can name your swing highs and lows, every chart is a blur. This drop locks in confirmed pivots with Williams-style fractals, classifies each one as a Higher High, Higher Low, Lower High, or Lower Low, and gives you a clean read on whether the trend is still intact or starting to break.
## What it does
- Detects confirmed pivot highs and lows using a configurable left/right lookback (Williams Fractals method)
- Auto-labels each pivot as HH, HL, LH, or LL by comparing to the most recent same-type pivot
- Drops a colored dot at every confirmed pivot bar so the structure is unmissable at a glance
- Plots labels with the right offset so they actually land on the pivot bar, not on the confirmation bar
- Built-in alerts fire the moment a new pivot is confirmed — wire them straight into thinkorswim notifications
## Recommended settings
- Left bars: **5**
- Right bars: **5**
- Show pivot dots: **on**
- Show HH/HL/LH/LL labels: **on**
## Ideal timeframe
Works on any timeframe — use 1m/5m for intraday structure, 1h/4h for swing, and Daily for the bigger map. Smaller left/right values (2-3) make it more sensitive; larger values (8-13) filter for major swings only.
## How to use
A clean uptrend is a sequence of HH and HL labels; a clean downtrend is LH and LL. The moment that sequence breaks — first LH inside an uptrend, first HH inside a downtrend — that's your earliest signal of a possible regime change. Use the labels as a structural backbone for entries, stop placement (just past the last opposite-side pivot), and invalidation. Remember pivots are confirmed `rightBars` after the fact, so don't expect real-time tops or bottoms — this is a structure framework, not a turning-point oracle.
## thinkScript code
```
# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
# Pivot High/Low Fractals — MarketFragments free drop
# Williams Fractals style swing detection with HH/HL/LH/LL classification.
# A pivot is confirmed rightBars after the actual swing bar; labels and
# dots are projected back to the true pivot position via negative offsets.
# Free for non-commercial use. Attribution appreciated.
declare upper;
input leftBars = 5;
input rightBars = 5;
input showDots = yes;
input showLabels = yes;
# --- Pivot detection (confirmed rightBars after the actual swing) ---
def pivotHighVal = high[rightBars];
def isPHigh = pivotHighVal > Highest(high[rightBars + 1], leftBars)
and pivotHighVal > Highest(high, rightBars);
def pivotLowVal = low[rightBars];
def isPLow = pivotLowVal < Lowest(low[rightBars + 1], leftBars)
and pivotLowVal < Lowest(low, rightBars);
# --- Track last + previous pivot highs / lows ---
def lastPH = if isPHigh then pivotHighVal else lastPH[1];
def prevPH = if isPHigh then lastPH[1] else prevPH[1];
def lastPL = if isPLow then pivotLowVal else lastPL[1];
def prevPL = if isPLow then lastPL[1] else prevPL[1];
# --- Classify ---
def isHH = isPHigh and !IsNaN(prevPH) and pivotHighVal > prevPH;
def isLH = isPHigh and !IsNaN(prevPH) and pivotHighVal <= prevPH;
def isHL = isPLow and !IsNaN(prevPL) and pivotLowVal > prevPL;
def isLL = isPLow and !IsNaN(prevPL) and pivotLowVal <= prevPL;
# --- Dots plotted at the actual pivot bar (rightBars back via negative offset) ---
plot phDot = if showDots and isPHigh[-rightBars] then high else Double.NaN;
phDot.SetPaintingStrategy(PaintingStrategy.POINTS);
phDot.SetDefaultColor(Color.RED);
phDot.SetLineWeight(4);
phDot.HideTitle();
plot plDot = if showDots and isPLow[-rightBars] then low else Double.NaN;
plDot.SetPaintingStrategy(PaintingStrategy.POINTS);
plDot.SetDefaultColor(Color.GREEN);
plDot.SetLineWeight(4);
plDot.HideTitle();
# --- HH/HL/LH/LL bubbles placed at the actual pivot bar ---
AddChartBubble(showLabels and isHH[-rightBars], high, "HH", Color.GREEN, yes);
AddChartBubble(showLabels and isLH[-rightBars], high, "LH", Color.RED, yes);
AddChartBubble(showLabels and isHL[-rightBars], low, "HL", Color.GREEN, no);
AddChartBubble(showLabels and isLL[-rightBars], low, "LL", Color.RED, no);
# --- Alerts (fire on confirmation bar) ---
Alert(isPHigh, "MF Pivot High confirmed", Alert.BAR, Sound.Bell);
Alert(isPLow, "MF Pivot Low confirmed", Alert.BAR, Sound.Bell);
Alert(isHH, "MF Higher High", Alert.BAR, Sound.Chimes);
Alert(isLL, "MF Lower Low", Alert.BAR, Sound.Chimes);
```
Drop your tweaks, screenshots, and questions below — that's how the library gets better.

