OPEN-SOURCE SCRIPT
Basic Supply & Demand Zones

This indicator draws horizontal supply and demand zones based on swing highs/lows (pivots), using a very simplified version of classic supply/demand zone logic:
Supply zones (red) → areas where strong selling supposedly happened
Demand zones (green) → areas where strong buying supposedly happened
Core logic breakdown
Pivot detection
pineph = ta.pivothigh(leftBars, rightBars) // looks for swing highs
pl = ta.pivotlow (leftBars, rightBars) // looks for swing lows
Typical default settings: leftBars = 5, rightBars = 3
→ A high must be higher than 5 bars before and 3 bars after to be considered a pivot high (and vice versa for lows).
How zones are created
When a pivot high is confirmed:
It looks at the candle body (open/close) of the bar rightBars positions back (usually the 3rd bar before current)
Creates a very thin box using only the body high/low of that candle
Box color: semi-transparent red
Extends rightward 50 bars (configurable)
→ Label "S" appears above it
When a pivot low is confirmed: same logic, but green box + "D" label below
Very important — zone height is extremely narrow
The zone height = |open - close| of the pivot candle body only
→ most real supply/demand traders would consider this too narrow (many prefer wick-to-wick or 50–100% extension of the move)
Mitigation (deletion) logic — the most useful part
Supply zone (red) → gets deleted as soon as price makes a high above the top of the box
Demand zone (green) → gets deleted as soon as price makes a low below the bottom of the box
This is a classic "mitigation" or "invalidated zone" rule — once price returns into the zone and breaks the opposite side → the zone is considered "used up".
Summary – What you actually see on chart
Red horizontal boxes appearing around recent swing highs (very thin = body only)
Green horizontal boxes appearing around recent swing lows (very thin)
Boxes extend ~50 bars to the right
When price later strongly breaks through a zone in the aggressive direction → box disappears
Tiny "S" and "D" labels (can be turned off)
Supply zones (red) → areas where strong selling supposedly happened
Demand zones (green) → areas where strong buying supposedly happened
Core logic breakdown
Pivot detection
pineph = ta.pivothigh(leftBars, rightBars) // looks for swing highs
pl = ta.pivotlow (leftBars, rightBars) // looks for swing lows
Typical default settings: leftBars = 5, rightBars = 3
→ A high must be higher than 5 bars before and 3 bars after to be considered a pivot high (and vice versa for lows).
How zones are created
When a pivot high is confirmed:
It looks at the candle body (open/close) of the bar rightBars positions back (usually the 3rd bar before current)
Creates a very thin box using only the body high/low of that candle
Box color: semi-transparent red
Extends rightward 50 bars (configurable)
→ Label "S" appears above it
When a pivot low is confirmed: same logic, but green box + "D" label below
Very important — zone height is extremely narrow
The zone height = |open - close| of the pivot candle body only
→ most real supply/demand traders would consider this too narrow (many prefer wick-to-wick or 50–100% extension of the move)
Mitigation (deletion) logic — the most useful part
Supply zone (red) → gets deleted as soon as price makes a high above the top of the box
Demand zone (green) → gets deleted as soon as price makes a low below the bottom of the box
This is a classic "mitigation" or "invalidated zone" rule — once price returns into the zone and breaks the opposite side → the zone is considered "used up".
Summary – What you actually see on chart
Red horizontal boxes appearing around recent swing highs (very thin = body only)
Green horizontal boxes appearing around recent swing lows (very thin)
Boxes extend ~50 bars to the right
When price later strongly breaks through a zone in the aggressive direction → box disappears
Tiny "S" and "D" labels (can be turned off)
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.