StatisticalPine Script v6

ta.median()Median

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

TradingView language reference

Syntax

Syntax
ta.median(source, length) → series int / 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.

A median can reduce the influence of isolated extreme values on the center estimate. It is not necessarily smoother than SMA and does not repair bad source data; verify suspicious ticks separately.

Generate ta.median() Code with AI

Draft Pine Script using ta.median() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.