Concept

Order-statistic Filters

Order-statistic Filters, also known as moving median, moving mode, are Trend concepts. The Library holds 1 implementation, a working definition you can pull into Quant.

Top Order-statistic Filters indicator

The top custom implementation, built on the original standard Order-statistic Filters formula.

1 total

From studying Order-statistic Filters to trading it: take the implementation below into Quant and backtest it instantly.

What are Order-statistic Filters?

Order-statistic filters smooth a series by ranking rather than averaging. Slide a window of the last N values along the chart, sort the contents, and output the value at a chosen rank: the middle value gives the moving median, a chosen percentile gives a percentile filter, the two extremes give running maximum and minimum, and the most frequent value gives a moving mode. Because the output is chosen by rank, always an actual data point or an interpolation between two adjacent ones rather than a weighted blend of the whole window, these are nonlinear filters and behave differently from any moving average.

The family comes from signal processing and robust statistics. John Tukey proposed running-median smoothing in the early 1970s as part of exploratory data analysis, and median filters went on to become a staple of digital image processing because they remove salt-and-pepper noise while preserving edges. Traders inherited the tools directly: a bad tick is the chart's version of a corrupted pixel, and a genuine gap or regime break is an edge worth preserving. On modern platforms the general case is built in; Pine Script® provides median and percentile functions, and array support makes arbitrary ranks straightforward to compute.

The practical appeal is robustness. A single bad tick or one violent bar drags a mean in proportion to its size but barely moves a median, and where linear smoothers round off sharp level shifts, a median tends to preserve them, at the cost of a steppy, plateau-prone output. The family also generalizes familiar tools: Donchian channels are simply the 100th and 0th percentile filters plotted as a channel, and a median crossover system is a moving average crossover setup with means swapped for ranks.

How to calculate a moving median

The moving median is the flagship order-statistic filter, and every other member of the family is the same procedure with a different rank.

  1. 1Choose a window length N and, at each bar, collect the most recent N values of the source series (close, volume, an indicator, anything).
  2. 2Sort the window's values in ascending order.
  3. 3Output the middle value. With an odd N that is the single center element; with an even N, average the two center elements.
  4. 4For the general case, output a different rank instead: the p-th percentile (interpolating between neighboring ranks where needed), the maximum, the minimum, or the most frequent value for a moving mode.

How it's calculated

Filters that output a chosen rank of the last n prices rather than an average; the moving median is the best-known case.

Wt=(Pt,Pt1,,Ptn+1)W_t = (P_t, P_{t-1}, \ldots, P_{t-n+1})
OSFt=r-th smallest value in Wt\operatorname{OSF}_t = r\text{-th smallest value in } W_t
Mediant=value at rank n+12 in Wt, for odd n\operatorname{Median}_t = \text{value at rank } \frac{n + 1}{2} \text{ in } W_t\text{, for odd } n
Mediant=mean of the values at ranks n/2 and n/2+1, for even n\operatorname{Median}_t = \text{mean of the values at ranks } n/2 \text{ and } n/2 + 1\text{, for even } n
Modet=most frequent value in Wt, with prices rounded to bin width b\operatorname{Mode}_t = \text{most frequent value in } W_t\text{, with prices rounded to bin width } b
P_t: source price at bar t (commonly close)
t: bar index
n: window length in bars (no universal default; odd lengths are typical)
W_t: window of the last n prices at bar t
r: selected rank, from 1 = smallest to n = largest
OSF_t: order-statistic filter output at bar t
Median_t: moving median at bar t, the rank (n + 1) / 2 order statistic
Mode_t: moving mode at bar t
b: bin width used to group nearly equal prices for the mode (no universal default)

Ranks r = 1 and r = n give the moving minimum and maximum; applied to the lows and highs these are the lower and upper Donchian channel lines.

A moving median follows clean step changes and ignores isolated spikes better than a mean of the same length, at the cost of a staircase-like output.

Exact price repeats are rare, so the moving mode is only meaningful on binned or rounded prices.

How traders use it

  • To despike raw data: a short moving median strips isolated bad ticks and single-bar anomalies before other indicators are computed, standard outlier handling.
  • As a robust baseline: swapping a moving median in place of an SMA or EMA gives a centerline that one wide bar barely moves, useful in gappy or thinly traded markets.
  • As percentile channels: an upper and lower percentile of price over a lookback frame a range that ignores the most extreme excursions, a softer alternative to pure high-low channels and a rank-based cousin of the MA envelope.
  • Inside classic constructs: median-based variants of tools like Supertrend or MACD replace the mean component so the signal reacts less to single-bar shocks.
  • As a regime condition: price holding above a rising moving median, or the median's own slope, provides a robust trend regime label, the same role an MA slope filter plays but harder for one news bar to flip.

Order-statistic Filters vs linear smoothers

SMA: An SMA is a linear filter: every value in the window contributes proportionally, so one outlier shifts the output. A moving median is rank-based, so an outlier's size never enters the output; at most it shifts which value sits in the middle.

EMA: An EMA weights recent data more heavily but is still linear, so a single extreme bar pulls it immediately and decays out of it slowly. A median never responds in proportion to an outlier's size, though the EMA tracks smooth turns more gracefully.

Ehlers SuperSmoother: The SuperSmoother is a carefully designed linear low-pass filter with little lag for its smoothness, but like all linear filters it rounds off level shifts and passes a scaled version of every spike. A median keeps step changes crisp and drops isolated spikes entirely, at the cost of plateau-shaped output.

VWMA: A VWMA reweights the mean by volume, so it is still an average and still outlier-sensitive, just along a different dimension. Order-statistic filters discard magnitude altogether and keep only rank order.

Concept family

Trend

100 concepts mapped · 100 in the Library

Order-statistic Filters FAQ

Turn Order-statistic Filters into a trading strategy.

Take the implementation from this page into Quant, then build on it, backtest it on real data, and keep refining it in conversation.