OPEN-SOURCE SCRIPT
S/R Zones

### What it does
**Smart S/R Zones** automatically detects and visualizes **support and resistance zones** by:
* Finding **confirmed swing highs/lows** (pivot points)
* **Clustering nearby pivots** into price zones
* Scoring each zone by **how many pivots** it contains (zone “strength”)
* Marking zones as **active** or **broken**, and stopping broken zones at the breakout bar
* Keeping the chart clean by removing pivots and zones outside a configurable lookback window
### Core logic (how it works)
1. **Pivot detection (swing points)**
* Uses `ta.pivothigh()` and `ta.pivotlow()` with:
* `leftBars` bars to the left
* `rightBars` bars to the right
* Important: pivots are **confirmed only after `rightBars` bars**, so signals are delayed by design.
2. **Pivot memory + lookback cleanup**
* Each pivot is stored with:
* price
* type: `1` = resistance pivot high, `-1` = support pivot low
* pivot bar index (pivot’s original bar)
* active/broken flag
* breakout bar index (when it got broken)
* Pivots older than `lookbackPeriod` bars are removed.
3. **Dynamic zone width**
* Computes the recent range:
* `priceRange = highest(high, lookbackPeriod) - lowest(low, lookbackPeriod)`
* Converts it to a maximum zone width:
* `maxZoneWidth = priceRange * zoneWidthPct / 100`
* This makes zones adapt to volatility/regime changes.
4. **Breakout detection (pivot invalidation)**
* A resistance pivot is marked broken when:
* `close > pivotPrice * (1 + breakoutPct/100)`
* A support pivot is marked broken when:
* `close < pivotPrice * (1 - breakoutPct/100)`
* Note: breakout is **close-based**, not intrabar wick-based.
5. **Clustering pivots into zones**
* Pivots are grouped into zones **only with the same type** (support with support, resistance with resistance).
* A pivot joins a zone if it is close to the zone midpoint:
* `abs(price - zoneMid) <= maxZoneWidth/2`
* Zone boundaries expand to include the new pivot, but only if:
* `(newHigh - newLow) <= maxZoneWidth`
* Zone strength increments by 1 for each pivot added.
6. **Active vs broken zones (visual state)**
* Active zones extend to the **current bar**.
* Broken zones stop at their **breakout bar** (the bar index stored when the pivot was broken).
* Strength includes all pivots clustered, including pivots that later broke (as long as they are still in lookback).
### Visuals (what you see)
* **Green zones**: active support
* **Red zones**: active resistance
* **More transparent zones**: broken zones (support/resistance that was invalidated by a close beyond the breakout threshold)
* Optional labels:
* `S2`, `S3`… for support strength
* `R2`, `R3`… for resistance strength
* The number is the **count of clustered pivots** in that zone.
### Inputs (how to tune it)
* **Pivot Left Bars / Pivot Right Bars**
* Higher values = fewer pivots, more “major” swings, stronger zones
* Lower values = more pivots, more zones, more noise
* **Lookback Period**
* Limits how far back pivots are considered and keeps the chart uncluttered
* **Zone Width %**
* Controls how wide zones can get (as a % of recent range)
* Higher = more clustering, fewer broader zones
* Lower = tighter zones, more zones
* **Minimum Zone Strength**
* Filters weak zones. Example: set to 3 to show only zones formed by 3+ pivots
* **Breakout Threshold %**
* Defines how far price must close beyond a level to mark it broken
* Higher = fewer “false break” breaks, slower invalidation
* Lower = more responsive, more break markings
* **Show Strength Labels**
* Toggles S/R strength markers.
### How to use it in trading workflows
* **Zone reaction**: Watch for rejection/acceptance when price revisits a strong zone (`S3+`, `R3+`).
* **Breakout context**: A zone turning “broken” indicates price closed meaningfully beyond it (by threshold).
* **Confluence**: Use with your own context (trend, volatility, session structure, volume tools). This script is strictly price-structure based.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。