top of page

Pine Script Library

Public·1 member

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 circle at every confirmed pivot bar so the structure is unmissable at a glance

- Uses Pine's `offset` and `label.new` with `bar_index - rightBars` so dots and labels land on the true pivot bar

- Built-in `alertcondition` hooks for every new pivot and every HH / LL — ready to wire into TradingView alerts


## 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.


## Pine Script v5 code

```

//@version=5

// ─────────────────────────────────────────────

// │ 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.

// Pivots are confirmed rightBars after the actual swing bar; dots and

// labels are placed at the true pivot bar via `offset` / `bar_index - rightBars`.

// Free for non-commercial use. Attribution appreciated.


indicator("MF Pivot High/Low Fractals", overlay=true, max_labels_count=500)


leftBars = input.int(5, "Left Bars", minval=1)

rightBars = input.int(5, "Right Bars", minval=1)

showDots = input.bool(true, "Show Pivot Dots")

showLabels = input.bool(true, "Show HH/HL/LH/LL Labels")


// --- Pivot detection (returns the pivot price on the confirmation bar) ---

phVal = ta.pivothigh(high, leftBars, rightBars)

plVal = ta.pivotlow(low, leftBars, rightBars)


// --- Track last + previous pivot highs / lows ---

var float lastPH = na

var float prevPH = na

var float lastPL = na

var float prevPL = na


if not na(phVal)

prevPH := lastPH

lastPH := phVal


if not na(plVal)

prevPL := lastPL

lastPL := plVal


// --- Classify ---

isHH = not na(phVal) and not na(prevPH) and phVal > prevPH

isLH = not na(phVal) and not na(prevPH) and phVal <= prevPH

isHL = not na(plVal) and not na(prevPL) and plVal > prevPL

isLL = not na(plVal) and not na(prevPL) and plVal <= prevPL


// --- Dots plotted at the true pivot bar via offset = -rightBars ---

plot(showDots ? phVal : na, title="Pivot High Dot", color=color.red,

linewidth=3, style=plot.style_circles, offset=-rightBars)

plot(showDots ? plVal : na, title="Pivot Low Dot", color=color.green,

linewidth=3, style=plot.style_circles, offset=-rightBars)


// --- HH/HL/LH/LL labels placed at the true pivot bar ---

if showLabels and isHH

label.new(bar_index - rightBars, phVal, "HH", color=color.new(color.green, 0),

textcolor=color.white, style=label.style_label_down, size=size.small)

if showLabels and isLH

label.new(bar_index - rightBars, phVal, "LH", color=color.new(color.red, 0),

textcolor=color.white, style=label.style_label_down, size=size.small)

if showLabels and isHL

label.new(bar_index - rightBars, plVal, "HL", color=color.new(color.green, 0),

textcolor=color.white, style=label.style_label_up, size=size.small)

if showLabels and isLL

label.new(bar_index - rightBars, plVal, "LL", color=color.new(color.red, 0),

textcolor=color.white, style=label.style_label_up, size=size.small)


// --- Alerts ---

alertcondition(not na(phVal), title="MF Pivot High", message="MF Pivot High confirmed")

alertcondition(not na(plVal), title="MF Pivot Low", message="MF Pivot Low confirmed")

alertcondition(isHH, title="MF Higher High", message="MF Higher High")

alertcondition(isLL, title="MF Lower Low", message="MF Lower Low")

```


Drop your tweaks, screenshots, and questions below — that's how the library gets better.


1 View
Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page