Home » Array Functions » array.median Funtion in Pinescript

array.median Funtion in Pinescript

Photo of author
Published on

can you write a programming tutorial blog on array.median function in pine script? The blog should properly use h2 and h3 headings. Include every thing that you think is needed Also, include 2 unique different use case examples and explain each line of code in the example. The article should be close to 1000 words start straight away with the topic at hand please. Please add key take away before conclusion.

Introduction to Array.Median Function

The array.median function is used to find the median value of the elements in an array. It’s particularly useful for removing noise and outliers from data, as the median is less sensitive to extreme values than the mean.

Syntax

array.median(a)

Where a is the input array.

Return value: The median value of the elements in the input array.

Implementing Array.Median

Now that we have a basic understanding of arrays in Pine Script, let’s implement the array.median function.

//@version=5
indicator('Array Median Example', shorttitle='AME', overlay=true)

length = input.int(14, minval=1, title='Length')
src = close

arraySrc = array.new_float(0)
for i = 0 to length - 1 by 1
    array.push(arraySrc, src[i])

medianValue = array.median(arraySrc)
plot(medianValue, color=color.new(color.red, 0), linewidth=2, title='Median')
label newLable = na
if barstate.islast
    newLable := label.new(x=bar_index, y=medianValue, style=label.style_label_left, 
     color=#E6E6FA, textcolor=color.black, size=size.large, 
     text="Array Median =  " + str.tostring(medianValue))
label.delete(newLable[1])
array.median Funtion

In this example, we’re creating a simple script that plots the median value of the last length close prices. Let’s go through the code step by step:

  1. We define the length input variable, which represents the number of past close prices to consider.
  2. We set the src variable to close, which means we’ll be using the close prices as our data source.
  3. We create an empty array called arraySrc of type float.
  4. We use a for loop to iterate through the past close prices and add each price to the arraySrc array.
  5. We calculate the median value of the elements in the arraySrc array using the array.median function.
  6. Finally, we plot the median value using the plot function.

Use Case 1: Median-Based Moving Average

In this use case, we’ll create a median-based moving average indicator using the array.median function. The median-based moving average smooths the price data while being less sensitive to extreme values, providing a more robust representation of the trend.

//@version=5
indicator('Median-Based Moving Average', shorttitle='MBMA', overlay=true)

length = input.int(14, minval=1, title='Length')
src = close

arraySrc = array.new_float(0)
for i = 0 to length - 1 by 1
    array.push(arraySrc, src[i])

medianValue = array.median(arraySrc)
mbma = ta.sma(medianValue, length)
plot(mbma, color=color.new(color.blue, 0), linewidth=2, title='MBMA')
label newLable = na
if barstate.islast
    newLable := label.new(x=bar_index, y=mbma, style=label.style_label_left, color=#E6E6FA, textcolor=color.black, size=size.large, text='Median-Based Moving Average =  ' + str.tostring(mbma))
    newLable
label.delete(newLable[1])
median based moving average

In this example, we modify the previous script to calculate the simple moving average (SMA) of the median value:

  1. We calculate the median value of the elements in the arraySrc array using the array.median function, just like before.
  2. We calculate the simple moving average (SMA) of the median value using the sma function.
  3. Finally, we plot the median-based moving average using the plot function.

Key Takeaways

  1. The array.median function in Pine Script is useful for finding the median value of elements in an array, which can help reduce the impact of extreme values in data.
  2. Arrays in Pine Script can be created using the array.new function and manipulated using functions such as array.push.
  3. The array.median function can be used in a variety of applications, such as creating median-based moving averages and price channels.

Conclusion

In this tutorial, we explored the array.median function in Pine Script and demonstrated its implementation in two unique use cases. Understanding the array.median function and its potential applications can help you create more robust and reliable indicators and strategies in your trading toolbox.

Leave a Comment