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)
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.