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 witharray.mode()so you can summarise signals, votes, or histogram data.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 5-10 min |
| Category | Arrays |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("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)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- Creating and populating arrays
- Retrieving a mode using
array.mode() - Handling ties and empty arrays safely
- Applying the mode in practical indicator logic
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
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(). |
Implementation Blueprint
Section titled “Implementation Blueprint”-
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) -
Guard against empty arrays
array.mode()returnsnawhen the array is empty. Check size first.modeVal = array.size(arr) > 0 ? array.mode(arr) : na -
React to ties
When two or more values share the top frequency, the mode isna. Use alternative logic (e.g., lowest value wins) if needed.modeVal := na(modeVal) ? math.min(array.max(arr), array.min(arr)) : modeVal
Example Playbook
Section titled “Example Playbook”//@version=5indicator("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)- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”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.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Why do I keep getting na?
Section titled “Why do I keep getting na?”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().
Can I get all modes when there are ties?
Section titled “Can I get all modes when there are ties?”array.mode() returns only one value or na. For multi-mode detection, loop through the array and build your own frequency map.
Key Takeaways
Section titled “Key Takeaways”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.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.