Home » Array Functions » array.mode Function In Pinescript

array.mode Function In Pinescript

Photo of author
Published on

Welcome to this tutorial on the array.mode function in Pine Script. Today, we will explore the ins and outs of this function, learn how to use it, and delve into two unique use case examples to illustrate its practical applications.

The Array.mode Function

The array.mode function is a built-in function in Pine Script that returns the most frequently occurring value (mode) in a given array. It is particularly useful for analyzing recurring patterns or identifying the most common value in a data set.

Syntax of the Array.mode Function

The syntax for the array.mode function is as follows:

array.mode(arr)

Where arr is the input array for which you want to find the mode.

Use Case Example 1: Detecting the Most Frequent Price Range

In this example, we will create a script that detects the most frequent price range in a given period. This can be useful for identifying support and resistance levels, as well as determining the most common trading range.

//@version=5
indicator('Most Common Trading Volume', shorttitle='MCTV', overlay=false)

length = input.int(14, minval=1, title='Lookback Period')
volumeArray = array.new_float(0)

for i = 0 to length - 1 by 1
    array.push(volumeArray, volume[i])

modeVolume = array.mode(volumeArray)
label newLable = na
if barstate.islast
    newLable := label.new(x=bar_index, y=close, style=label.style_label_left, color=#E6E6FA, textcolor=color.black, size=size.large, text='Most Common \n Trading Volume =  ' + str.tostring(modeVolume))
    newLable
label.delete(newLable[1])
array.mode Function

Explanation of the Code

  1. We start by defining the script settings, including the title and overlay option.
  2. We define an input variable length to represent the lookback period.
  3. We calculate the priceRange as the absolute difference between the close and open prices.
  4. We create an empty float array called rangeArray.
  5. Using a for loop, we iterate through the length of the lookback period and push the price range values into the rangeArray.
  6. We calculate the mode of the price range using the array.mode function.
  7. Finally, we plot the mode of the price range on the chart.

Key Takeaways

  • The array.mode function is a powerful tool for identifying the most frequently occurring value in an array.
  • By using this function, you can analyze recurring patterns or identify common values in a data set.
  • Two practical use cases for the array.mode function include detecting the most frequent price range and identifying the most common trading volume.

Conclusion

In this tutorial, we have covered the array.mode function in Pine Script, its syntax, and two unique use case examples. With a solid understanding of this function, you can incorporate it into your trading scripts to analyze recurring patterns or identify common values in your data. Keep experimenting with this powerful function to unlock its full potential and improve your trading strategies. Happy coding!

Leave a Comment