Sidechair

ta.median()

Browse the reference

Returns the median of source over the last length bars: the middle value of the sorted window.

Try it in the editor

ta.median sorts the window and returns the middle value. With an even window length, it averages the two middle values.

A single extreme bar cannot drag the median the way it moves a mean. A value is produced from the point the window holds length bars.

Syntax

Pine Script
ta.median(source: series int/float, length: series int) -> series float
ta.median(source: series int, length: series int) -> series int

Arguments

NameTypeDefaultDescription
sourceseries int/float or series intrequiredSeries to take the median of.
lengthseries intrequiredNumber of bars in the window. A whole number of 1 or more.

Returns

series float or series int: the median of the window, or na while it is not full.

Remarks

  • Use it as a spike-resistant alternative to ta.sma on noisy data.

Example

Pine ScriptMedian against meanOpen in editor
//@version=6
indicator("Median vs SMA", overlay = true)
plot(ta.median(close, 15), "Median", color = color.blue, linewidth = 2)
plot(ta.sma(close, 15), "SMA", color = color.orange)

See also

Runs on Sidechair