Home » Technical Analysis Functions » Understanding the ta.median() Function in Pine Script

Understanding the ta.median() Function in Pine Script

Photo of author
Published on

In this article, we delve into the ta.median() function in Pine Script. The ta.median() function is designed to calculate the median of a given series of values over a specified length. This function is particularly useful for smoothing out data and reducing the impact of outliers, making it valuable for identifying trends and making trading decisions. We will explore its syntax, overloads, arguments, and practical applications to enhance your Pine Script programming skills.

Syntax and Overloads

The ta.median() function comes with two overloads, allowing it to process both integer and floating-point series. Understanding these overloads is crucial for applying the function effectively in different scenarios. Here’s a closer look at the syntax:

1. Integer Series Overload

ta.median(source, length) → series int

2. Floating-Point Series Overload

ta.median(source, length) → series float


  • source (series int or series float): This is the series of values you wish to process. It can be a series of integer or floating-point values, depending on the overload you are using.
  • length (series int): This represents the number of bars over which to calculate the median. The length specifies the window size of the dataset to consider for each median calculation.


The ta.median() function returns the median of the series over the specified length. Depending on the overload used, the return type can be an integer or a floating-point series. The median is a valuable statistical measure because it is less sensitive to extreme values (outliers) compared to the mean, providing a more robust central tendency measure for a series of values.

Practical Application

To illustrate the practical application of the ta.median() function, let’s consider an example where we calculate the median closing price over the past 10 bars in a trading chart.


indicator("Median Closing Price", overlay=true)
lengthValue = 10
medianClose = ta.median(close, lengthValue)
plot(medianClose, color=color.red, title="Median Close")

In this example:

  • We define an indicator that calculates and plots the median closing price over the last 10 bars.
  • lengthValue is set to 10, specifying the window size for the median calculation.
  • We use the ta.median() function with close as the source argument (the closing prices of the bars) and lengthValue as the length argument.
  • The result, medianClose, is plotted on the chart with a red line, allowing traders to visualize the median closing price trend.

Key Features and Takeaways

  • Function Usability: The ta.median() function can handle both integer and floating-point series, making it versatile for various data types.
  • Syntax and Application: Understanding the syntax and choosing the correct overload are crucial for effectively applying the function in trading strategies.
  • Reducing Outliers’ Impact: The median is less sensitive to outliers, making ta.median() useful for identifying underlying trends in noisy data.

By integrating the ta.median() function into your Pine Script strategies, you can leverage robust statistical measures to enhance your technical analysis and trading decisions. Whether you’re smoothing data series or identifying trend directions, this function is a valuable tool in your Pine Script programming arsenal.

Leave a Comment