Guides
Normalization
A threshold such as "volume above 500" means different things on different instruments, at different hours, and on quiet or busy days. The norm module turns a raw measure into a unit that compares it with its own recent values: a rank, a z-score, or a multiple of its median. You keep full control: the measure, the window, the population and the unit are all yours.
How it works#
Each norm.* call keeps its own population of values, separately for each place in the code where it is called. At each call it:
- drops the values older than the window;
- adds the current value
x(unlesspush:is false, orxisna); - returns
xexpressed against the population.
The population is kept from one event to the next and from one bar to the next. Time is measured on the event being processed (in a handler or pattern) or on the bar.
Functions#
| Function | Returns |
|---|---|
norm.rank(x, window) | Percentile rank of x among the values, from 0 to 100 (the current value included). 99 means "in the top 1%". |
norm.zscore(x, window) | (x − mean) / standard deviation; 0 when the deviation is 0. |
norm.ratio(x, window, "median" \| "mean") | x divided by the median (default) or the mean; na if that is 0. |
norm.percentile(x, window, pct) | The pct percentile (0 to 100) of the values: a threshold in the measure's own unit. |
norm.mean(x, window), norm.median(x, window) | Mean and median of the values. |
norm.count(x, window) | Number of values held (returned even below min_count). |
norm.unit(x, unit, window) | One function for all units: "raw" (x unchanged, even below min_count), "rank" (or "percentile"), "zscore", "x_median", "x_mean". Handy with an input.choice setting. |
Window. A duration (30m: the values of the last 30 minutes) or a number of values (500: the last 500 values, at most 1,000,000). Anything else gives E933.
Options
| Option | Effect | Default |
|---|---|---|
min_count: n | Returns na while fewer than n values are held (minimum 1). | 20 |
by: key | Keeps a separate population per key: a number, text, bool or time. More than 200,000 populations gives E933. | one population |
push: condition | When false, compares x without adding it to the population. | true |
Errors (E933): an unknown unit in norm.unit, pct outside 0…100, a third argument of norm.ratio other than "median" or "mean", a bad window.
Rules for unbiased populations#
- **Call
norm.*at every event**, before the other conditions. If the call sits behindandor inside anif, the population only contains the cases where the other conditions were true, and a rank of 99 no longer means the top 1% of the measure. - In a pattern, compute the normalization in the statements before
when, not insidewhen. - Since each call place has its own population, a call that always uses
push: falsenever has any value and returnsna. Usepush:with a condition, for example to compare every bar with the population of RTH bars only.
Examples#
indicator "Volume rank" { overlay: false, min: 0, max: 100 }
unit = input.choice("Volume in", ["rank", "zscore", "x_median", "x_mean", "raw"], "rank")
win = input.int("Normalize over (bars)", 200, min: 20)
v = norm.unit(volume, unit, win)
r = norm.rank(volume, 2h, min_count: 30)
plot(r, #2962ff, title: "Rank over 2h")
plot(norm.rank(volume, 200, by: close >= open), #e8c15a, title: "Rank by bar direction")
hline(90, style: "dotted")A trade-size rank computed for every trade, then used as a filter:
indicator "Unusual prints" { overlay: true }
minRank = input.float("Min size rank", 99, min: 0, max: 100)
on trade(t) {
rk = norm.rank(t.size, 30m, min_count: 200)
if rk >= minRank {
label.new(t.time, t.price, str(t.size), color: t.is_buy ? #26a69a : #ef5350)
}
}
plot(close, title: "Close")Comparing each bar's volume only with bars of the same hour (a population per hour):
indicator "Volume vs same hour" { overlay: false }
x = norm.ratio(volume, 5000, "median", by: bar.hour, min_count: 10)
histogram(x, x > 2 ? #e8c15a : #2962ff60, title: "Volume / median of this hour")
hline(1)