level
A price level or zone: the built-in type level and helpers for sets of levels. Fields (read-only): price, top and bottom (na for a line), name, kind, side (1, -1 or 0), strength, since (the time of the bar it became known, ms) and source (the short title of the script it comes from).
#Functions
#level.clusterfunction
Finds zones where levels agree: groups of at least min_count levels, from at least min_sources different sources, whose prices lie within tolerance of the lowest one of the group. Each zone is a level: top and bottom are the outer levels, price their mean, strength the number of different sources, name the names joined with "+". Sorted by price.
level.cluster(levels: array<T>, tolerance: float, { min_count?: int = 2, min_sources?: int = 1 }): array<T>| Parameter | Type | Description |
|---|---|---|
levels | array<T> | The levels to look through (for example level.merge of two links' levels). |
tolerance | float | How far apart, in price, levels of one zone may be. Use a market-sized value such as ta.atr(14) * 0.25. |
min_count | intdefault 2 | Levels a zone needs (default 2). |
min_sources | intdefault 1 | Different sources a zone needs (default 1). 2 = two scripts must agree, not two lines of one script. |
Returns array<T>.
Example
const mine = array.new<level>()
mine.push(level.new(ta.highest(high, 50), { name: "High 50", source: "range" }))
mine.push(level.new(ta.ema(close, 200), { name: "EMA 200", source: "trend" }))
const zones = level.cluster(mine, ta.atr(14) * 0.5, { min_sources: 2 })
plot(zones.size(), "Zones")Since ChartScript 1
#level.count_nearfunction
How many levels lie within a distance of a price. A zone counts when the price is inside it or within the distance of its edge.
level.count_near(levels: array<T>, price: float, distance: float): int| Parameter | Type | Description |
|---|---|---|
levels | array<T> | The levels to count. |
price | float | The price to measure from. |
distance | float | The distance in price. |
Returns int.
Example
const mine = array.new<level>()
mine.push(level.new(ta.highest(high, 50)))
mine.push(level.new(ta.highest(high, 20)))
plot(level.count_near(mine, close, ta.atr(14)), "Levels near the price")Since ChartScript 1
#level.mergefunction
Two sets of levels as one, sorted by price (low to high).
level.merge(a: array<T>, b: array<T>): array<T>| Parameter | Type | Description |
|---|---|---|
a | array<T> | The first set. |
b | array<T> | The second set. |
Returns array<T>.
Example
const highs = array.new<level>()
highs.push(level.new(ta.highest(high, 50)))
const lows = array.new<level>()
lows.push(level.new(ta.lowest(low, 50)))
const all = level.merge(highs, lows)
plot(all.size(), "Levels")Since ChartScript 1
#level.nearestfunction
The level closest to a price, or na when there is none.
level.nearest(levels: array<T>, price: float, { side?: int = 0 }): T| Parameter | Type | Description |
|---|---|---|
levels | array<T> | The levels to look through. |
price | float | The price to measure from. |
side | intdefault 0 | 1 = only levels at or above the price, -1 = only at or below, 0 = either (default). |
Returns T.
Example
const mine = array.new<level>()
mine.push(level.new(ta.lowest(low, 50), { name: "Low 50" }))
const below = level.nearest(mine, close, { side: -1 })
plot(na(below) ? na : below.price, "Nearest level below")Since ChartScript 1
#level.newfunction
Makes a level of your own, for the level helpers (for example to give levels from several links one family name in source).
level.new(price: float, { top?: float = na, bottom?: float = na, name?: string = "", kind?: string = "", side?: int = 0, strength?: float = na, source?: string = "" }): T| Parameter | Type | Description |
|---|---|---|
price | float | The price (the middle of a zone). |
top | floatdefault na | The top of a zone (na for a line). |
bottom | floatdefault na | The bottom of a zone (na for a line). |
name | stringdefault "" | A short name. |
kind | stringdefault "" | Your own word for the kind of level. |
side | intdefault 0 | 1 = support, -1 = resistance, 0 = neither. |
strength | floatdefault na | How strong, on your own scale. |
source | stringdefault "" | Where the level comes from (level.cluster counts distinct sources). Default: this script's short title. |
Returns T.
Example
const lv = level.new(ta.highest(high, 50), { name: "50-bar high", kind: "R", side: -1 })
plot(lv.price, "Level")Since ChartScript 1