Skip to content
ChartDeck ChartScript docs Account Open the terminal

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>
ParameterTypeDescription
levelsarray<T>The levels to look through (for example level.merge of two links' levels).
tolerancefloatHow far apart, in price, levels of one zone may be. Use a market-sized value such as ta.atr(14) * 0.25.
min_countintdefault 2Levels a zone needs (default 2).
min_sourcesintdefault 1Different sources a zone needs (default 1). 2 = two scripts must agree, not two lines of one script.

Returns array<T>.

Example

ChartScriptFragmentOpen in editor
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
ParameterTypeDescription
levelsarray<T>The levels to count.
pricefloatThe price to measure from.
distancefloatThe distance in price.

Returns int.

Example

ChartScriptFragmentOpen in editor
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>
ParameterTypeDescription
aarray<T>The first set.
barray<T>The second set.

Returns array<T>.

Example

ChartScriptFragmentOpen in editor
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
ParameterTypeDescription
levelsarray<T>The levels to look through.
pricefloatThe price to measure from.
sideintdefault 01 = only levels at or above the price, -1 = only at or below, 0 = either (default).

Returns T.

Example

ChartScriptFragmentOpen in editor
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
ParameterTypeDescription
pricefloatThe price (the middle of a zone).
topfloatdefault naThe top of a zone (na for a line).
bottomfloatdefault naThe bottom of a zone (na for a line).
namestringdefault ""A short name.
kindstringdefault ""Your own word for the kind of level.
sideintdefault 01 = support, -1 = resistance, 0 = neither.
strengthfloatdefault naHow strong, on your own scale.
sourcestringdefault ""Where the level comes from (level.cluster counts distinct sources). Default: this script's short title.

Returns T.

Example

ChartScriptFragmentOpen in editor
const lv = level.new(ta.highest(high, 50), { name: "50-bar high", kind: "R", side: -1 })
plot(lv.price, "Level")

Since ChartScript 1

The ChartScript documentation is in English for now; translations will follow.