OPEN-SOURCE SCRIPT

Market Structure + Swing Levels [AFD]

1 231
Market Structure + Swing Levels [AFD] organizes confirmed swing highs and
swing lows into configurable price Zones. Its default ATR mode sizes each
individual Zone from volatility at the Swing origin Bar; Tick, Point, and
Currency modes provide fixed-distance alternatives. It then describes
confirmed continuation Breaks, direction-changing Shifts, and the initial
direction assignment without trade instructions or predictive claims.

snapshot

## Publication and license

- Publication type: Open-source.
- Pine version: 6.
- License: Mozilla Public License 2.0 (MPL 2.0).
- The open-source publication setting, this description, and the MPL 2.0
notice in the Pine source header must remain aligned.

The source is available for inspection and modification under MPL 2.0. This
summary identifies the applicable license; the notice in the Pine source
header remains the authoritative license reference.

## Capabilities

- Confirms Swing Highs and Swing Lows using the same selected strength on both
sides of the candidate Bar.
- Shows classifications by default: `H`, `HH`, `EH`, and `LH` for highs, and
`L`, `HL`, `EL`, and `LL` for lows.
- Builds ATR-, Tick-, Point-, or Currency-width Zones around confirmed Swings.
- Merges transitively overlapping same-side Zones into exact-union clusters.
- Evaluates Structure on confirmed Bars using selectable Close or Body
clearance.
- Lets Zone breaks use that Structure rule or an inclusive High/Low Touch of
the far edge.
- Retains broken Zones by default with configurable memory and color.
- Reports current direction, latest Structure event, active Zones, and
nearest-Zone distance in the Dashboard.
- Provides four fixed Structure alert conditions plus an optional combined
dynamic alert that can include Zone breaks.

## How to use it

1. Select a Preset Profile or choose Custom and set Swing Strength. The default
Swing profile uses a strength of 7 Bars on each side.
2. Choose the Zone Width Mode and width appropriate for the chart.
3. Choose how Structure confirms a clearance. Close is the default; Body
requires both the Open and Close to clear the reference.
4. Zone Break Basis follows the Structure rule by default. Touch (High/Low)
instead breaks a Zone when the wick reaches or passes its far edge.
5. Swing High and Swing Low Zone labels start at the oldest Swing Origin
represented by each Zone. Select Right Edge when current-Bar alignment is
preferred.
6. Swing High and Swing Low text have separate color controls. Both default to
white and also color the matching Swing classification labels. Zone Age,
Break, Shift, and Dashboard text also start white.
7. The Dashboard starts with all five rows visible. Disable nearest-Zone
distance when only Direction, Last Event, and Zone counts are needed.
8. Show Broken Zones starts on with a 20-Bar memory. Turn it off for an
active-Zones-only chart.
9. Zone Age starts off. When enabled, its unit starts in Days.

## How to interpret Zone cluster counts

The `×N` suffix is a cluster membership count. `Swing High ×3` means three
confirmed Swing High Zones are represented by that merged cluster.

It does not mean price tested one exact price three times. Zones merge when
their price bands overlap or meet at an edge. Merging is transitive, so one
connected Zone can bridge two other Zones even when those outer Zones do not
directly overlap each other. The displayed cluster preserves the full union of
those connected bands, and `×N` reports how many confirmed Swing Zones
contributed to it.

snapshot

## Broken Zones

Show Broken Zones is the explicit on/off control:

- On by default — the Zone freezes at its break-confirmation Bar, changes to
Broken Zone Color, and remains visible for the selected Broken Zone Memory.
- Off — a Zone is deleted as soon as it breaks.
- Up to 20 broken Zones are retained. If more accumulate before their memory
expires, the oldest retained Zone is removed first.

This display choice does not change how Zone breaks are detected and does not
change alert behavior.

## Limitations and evidence

- Swings require the selected number of Bars on both sides, so confirmation
occurs after the origin Bar.
- ATR Zone width uses volatility from the Swing origin Bar.
- Currency-width mode creates no Zone when the symbol does not provide a valid
Point Value.
- Active Zones are capped at 40 per side. When the cap is reached, the oldest
active Zone on that side is removed first.
- Hours and Days shown for Zone Age are timeframe-based approximations and do
not correct for session gaps.
- Cluster membership describes connected Zone bands, not repeated reactions at
one exact price.
- Repository checks pass for the working source. TradingView compilation and
runtime verification of the latest working source have not yet been
recorded.
- This indicator is educational chart context and does not provide trade
instructions.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.