StatisticalPine Script v6

ta.median()Median

Returns the median (middle value) of a series over a given number of bars.

Syntax

Syntax
ta.median(source, length) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars in the rolling window.

Returns

Median of source for length bars back.

Remarks

na values in the source series are ignored.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.median", overlay=true)
plot(ta.median(close, 20))
plot(ta.sma(close, 20), color=color.new(color.orange, 0))

Trading Applications

Calculate median price for robust trend analysis

Build outlier-resistant moving averages

Create median-based channel indicators

Use for non-parametric price analysis

Related Functions

Frequently Asked Questions

ta.median() returns the middle value of the last length non-na observations when sorted, making it a robust central tendency measure.

SMA weights all points equally in the average; median is resistant to a few huge spikes because outliers do not pull the middle order statistic as strongly.

Prefer median when the series has fat tails or bad ticks, or when you want a smoother central line that ignores isolated extremes.

Generate ta.median() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.median() and other built-in functions instantly.