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

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.