Sidechair

ta.mode()

Browse the reference

Returns the most frequent value of source in the last length bars; when values tie, the smallest of them.

Try it in the editor

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

Pine Script
ta.mode(source: series int/float, length: series int) -> series float
ta.mode(source: series int, length: series int) -> series int

Arguments

NameTypeDefaultDescription
sourceseries int/float or series intrequiredThe series whose most frequent value over the last length bars is returned.
lengthseries intrequiredNumber 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

Example

Pine ScriptMost common rounded closeOpen in editor
//@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