Williams Fractals Dynamic Horizontal Lines

Overview
This script identifies Williams Fractals (support and resistance points) on any chart and automatically draws horizontal lines at those fractal levels.
The lines extend right and disappear when price breaks them — giving a clean and real-time view of key support and resistance levels.
Features
Detects upward fractals (potential resistances).
Detects downward fractals (potential supports).
Draws horizontal lines at each fractal point.
Automatically deletes a line when:
Price closes above a resistance.
Price closes below a support.
Clean and lightweight — minimal performance impact.
Inputs
Setting Description Default
Periods (n) Number of candles on each side required to confirm a fractal (minimum 2). 2
How It Works
Fractal Detection:
An up fractal is confirmed when a candle has the highest high compared to its neighbors.
A down fractal is confirmed when a candle has the lowest low compared to its neighbors.
Custom logic supports different "ties" — not just strict greater-than or less-than comparisons.
Drawing Lines:
When an up fractal is detected → a red horizontal resistance line is drawn.
When a down fractal is detected → a green horizontal support line is drawn.
Lines are drawn starting at the fractal candle and extend right across the chart.
Dynamic Line Deletion:
Every new candle is checked:
If high > resistance line → delete that resistance line.
If low < support line → delete that support line.
Only unbroken lines are kept on the chart, providing live, updated support/resistance zones.
Visual Elements
Up Fractal: 🡅 Triangular marker (teal color) above the fractal candle.
Down Fractal: 🡇 Triangular marker (red color) below the fractal candle.
Resistance Line: Red horizontal line.
Support Line: Green horizontal line.
Technical Details
Version: Pine Script v6
Arrays Used:
To store and manage active lines (line[] arrays).
To track the price associated with each line (float[] arrays).
Error Handling:
Backward iteration is handled using a while loop instead of a for loop to comply with Pine Script restrictions.
Offset Handling:
Fractal markers are plotted with an offset of -n bars for visual alignment.
Possible Enhancements (Future Ideas)
Sensitivity Settings: Allow a small margin (buffer) to define breakouts.
Extend Options: Let users choose between extending "infinite" or "limited" bars.
Custom Alerts: Generate alerts when fractal levels are broken.
Multi-timeframe Support: Detect fractals from higher timeframes on lower timeframe charts.
Example Usage
Swing Trading:
Use the fractal lines to identify and react to key breakouts or breakdowns dynamically.
Intraday Trading:
Spot live support/resistance on smaller timeframes for quick trades.
Trend Reversal Spotting:
Notice when a major fractal level is broken, possibly indicating trend changes.
Example Visual — Williams Fractals Dynamic Lines
Price Chart View:
What This Diagram Shows:
▲ Up Fractals:
Form resistance lines.
Line extends until price breaks above it.
▼ Down Fractals:
Form support lines.
Line extends until price breaks below it.
Lines are removed from the chart as soon as broken.
📈 Real Chart Example
Here's a rough idea how it will look live on your TradingView chart:
Feature Visual
Up Fractal 🔺 Green triangle above bar
Resistance Line ➖ Horizontal red line across the chart
Down Fractal 🔻 Red triangle below bar
Support Line ➖ Horizontal green line across the chart
Line Break 🚫 Line disappears when price crosses
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.
Penafian
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.