Returns the most frequent value of source in the last length bars; when values tie, the smallest of them.
ta.mode counts how often each exact value appears in the window and returns the one that appears most. Ties go to the smallest value.
It suits series that repeat, such as rounded prices or counts. For continuous data most values appear once, and the result is the smallest of them. Output is na until a full window exists.
Syntax
ta.mode(source: series int/float, length: series int) -> series float
ta.mode(source: series int, length: series int) -> series intArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source | series int/ | required | The series whose most frequent value over the last length bars is returned. |
| length | series int | required | Number of bars in the window. A whole number of 1 or more. |
Returns
series float or series int: the most common value in the window, or na while it is not full.
Remarks
- Round the source first, for example
math.round_to_mintick(close), so near-identical prices count as equal.
Example
//@version=6
indicator("Mode of closes", overlay = true)
rounded = math.round(close, 1)
plot(ta.mode(rounded, 50), "Mode", color = color.purple, linewidth = 2)See also
Runs on Sidechair