Skip to content

Find the mode of a TradingView Pine Script array

Find the mode of a TradingView Pine Script array

Section titled “Find the mode of a TradingView Pine Script array”

TL;DR
Count the most common element in a numeric array with array.mode() so you can summarise signals, votes, or histogram data.

DifficultyBeginner
Time to implement5-10 min
CategoryArrays
//@version=5
indicator("Array mode demo", overlay=false)
arr = array.new_int()
array.push(arr, 3)
array.push(arr, 3)
array.push(arr, 1)
array.push(arr, 5)
modeValue = array.mode(arr)
plot(modeValue, "Mode", color=color.orange)
Note. If multiple values share the highest frequency, `array.mode()` returns `na`. Handle that case before using the result.

The mode highlights which value appears most often in a dataset—helpful for majority voting systems, indicator histograms, or cluster detection. Pine Script’s array helpers make it straightforward to calculate the mode without writing your own frequency map.

  • Creating and populating arrays
  • Retrieving a mode using array.mode()
  • Handling ties and empty arrays safely
  • Applying the mode in practical indicator logic
CallPurpose
array.new_int() / array.new_float()Creates a numeric array that supports array.mode().
array.push(id, value)Adds elements to the end of the array.
array.unshift(id, value)Adds elements to the front (useful for weighting early values).
array.mode(id)Returns the most frequent element or na on ties/empty arrays.
array.size(id)Lets you guard against empty arrays before calling mode().
  1. Create an array and add values
    The array must be integer or float based—mode isn’t supported for other types.

    arr = array.new_int()
    array.push(arr, 4)
    array.push(arr, 2)
    array.push(arr, 4)
  2. Guard against empty arrays
    array.mode() returns na when the array is empty. Check size first.

    modeVal = array.size(arr) > 0 ? array.mode(arr) : na
  3. React to ties
    When two or more values share the top frequency, the mode is na. Use alternative logic (e.g., lowest value wins) if needed.

    modeVal := na(modeVal) ? math.min(array.max(arr), array.min(arr)) : modeVal
//@version=5
indicator("Majority vote mode", overlay=false)
votes = array.new_int()
if barstate.isnew
// Simulated votes from different signals
array.clear(votes)
array.push(votes, close > open ? 1 : 0)
array.push(votes, ta.rsi(close, 14) > 70 ? 1 : 0)
array.push(votes, ta.crossover(ta.ema(close, 10), ta.ema(close, 30)) ? 1 : 0)
modeVote = array.size(votes) > 0 ? array.mode(votes) : na
plot(modeVote, "Mode vote", color=color.new(color.green, 0), style=plot.style_columns)
bgcolor(modeVote == 1 ? color.new(color.green, 85) : na)
Why this works.
  • Votes are binary (0 or 1), so ties resolve cleanly—`na` means a split.
  • Clearing and reusing the array prevents size creep and stale values.
  • The background tint highlights when the majority vote is bullish.
  • array.mode() can be expensive on very large arrays. Clear or reuse arrays thoughtfully.
  • For tie-breaking, consider counting frequencies manually or sorting the array.
  • Use array.new_float() when working with decimal-based modes (e.g., price clusters).
  • Remember to array.clear() before repopulating arrays each bar to avoid accumulating historical values unless intentional.

Likely either the array is empty or there is a tie for the highest frequency. Inspect array contents (e.g., via array.get()) before calling array.mode().

array.mode() returns only one value or na. For multi-mode detection, loop through the array and build your own frequency map.

  • array.mode() offers a quick way to find the most common array element.
  • Always guard against empty arrays and handle ties gracefully.
  • Combine the mode with chart visuals or trading logic to summarise majority signals.
  • Clear arrays when sampling new data to keep memory use predictable.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.