Supply and demand zones

Tight bases that price left with a strong candle, kept as supply and demand zones.

Demand: a bull displacement candle (disp_k, close_frac) right after a base of min_base to max_base candles (the longest that fits) whose bodies are at most base_body of their range (a zero-range candle counts as body 0) and whose high-low span is at most max(floor_pct % × close, base_k × ATR), closing above the base. Zone = [base low, highest body top]. Supply is the mirror. A close beyond the far edge by invalid_k ATRs ends a zone; zones expire max_age_bars after birth; at most max_zones are kept. fresh = the nearest zone's current visit is its first (or no visit yet).

Supply and demand zones is a structure detector: it marks price structure on the chart and counts against the plan's detector limit.

Settings

SettingDefaultRangeWhat it does
atr_period141 to 500ATR length (Wilder) for every buffer.
min_base11 to 20Fewest candles in a base.
max_base51 to 20Most candles in a base.
base_body0.50 to 1Base candles: largest body ÷ range.
base_k10.1 to 10Widest base (high to low) in ATRs.
disp_k1.50.1 to 10Displacement: smallest body in ATRs.
close_frac0.250.01 to 0.5Displacement: the close lies in this top (bull) or bottom (bear) fraction of the range.
floor_pct0.050 to 10Smallest buffer of this detector, % of the close (each buffer is the larger of this and its ATR multiple).
invalid_k0.10 to 5A close this many ATRs beyond a zone's far edge ends the zone.
max_zones51 to 20Active zones kept (oldest dropped).
max_age_bars20010 to 2000Candles after its birth until a zone or level expires.
pick_rank11 to 5Pick outputs: 1 = the nearest zone, 2 = the next one out, …
pick_min_visits00 to 50Pick outputs: only zones visited at least this often.
pick_sideanyany, bull, bearnearest_low/high: zones of any side, or only the bull (support) or bear (resistance) side.

What it gives

OutputWhat it is
In a zoneTrue while the close is inside an active supply/demand zone.
In a demand zoneTrue while the close is inside a demand supply/demand zone.
In a supply zoneTrue while the close is inside a supply supply/demand zone.
Low edge of the zone aboveLow edge of the nearest supply/demand zone wholly above the close.
High edge of the zone aboveHigh edge of the nearest supply/demand zone wholly above the close.
Low edge of the zone belowLow edge of the nearest supply/demand zone wholly below the close.
High edge of the zone belowHigh edge of the nearest supply/demand zone wholly below the close.
Active zonesActive supply/demand zones (both sides).
Visits of the nearest zoneVisits of the nearest supply/demand zone (the one holding the close, else the closer of above/below); one continuous stay is one visit.
New demand zoneTrue on the candle a demand supply/demand zone is born.
New supply zoneTrue on the candle a supply supply/demand zone is born.
Demand zone invalidatedTrue on the candle a close goes through a demand supply/demand zone (it ends).
Supply zone invalidatedTrue on the candle a close goes through a supply supply/demand zone (it ends).
Low edge of the zone the close is inLow edge of the supply/demand zone the close is inside (the most recently created if several); none outside every zone. A long's stop below the zone.
High edge of the zone the close is inHigh edge of the supply/demand zone the close is inside (the most recently created if several); none outside every zone. A short's stop above the zone.
First touch of a demand zoneTrue on the candle the first visit of a demand supply/demand zone starts (its visits become 1).
First touch of a supply zoneTrue on the candle the first visit of a supply supply/demand zone starts (its visits become 1).
Fresh zoneTrue while the nearest zone had no visit before the current one.
Low edge of the picked zoneLow edge of the pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits, of the pick_side side; inside it or not.
High edge of the picked zoneHigh edge of the pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits, of the pick_side side; inside it or not.
Low edge of the picked demand zoneLow edge of the demand pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits; inside it or not.
High edge of the picked demand zoneHigh edge of the demand pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits; inside it or not.
Low edge of the picked supply zoneLow edge of the supply pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits; inside it or not.
High edge of the picked supply zoneHigh edge of the supply pick_rank-th supply/demand zone (1 = nearest: one holding the close, else the closest) with at least pick_min_visits visits; inside it or not.
Low edge of the newest demand zoneLow edge of the newest active demand supply/demand zone, from the candle it is born (none before).
High edge of the newest demand zoneHigh edge of the newest active demand supply/demand zone, from the candle it is born (none before).
Low edge of the newest supply zoneLow edge of the newest active supply supply/demand zone, from the candle it is born (none before).
High edge of the newest supply zoneHigh edge of the newest active supply supply/demand zone, from the candle it is born (none before).

What it reads

It reads the whole candle (open, high, low, close and volume).

Warm-up

With its default settings, Supply and demand zones needs 346 closed candles before its first value. A backtest loads them before its start date, so the first trade can already use it.

Test it

Copy this sentence into the Algonney chat to backtest it:

Backtest a strategy on BTC that uses Supply and demand zones with its default settings