map() - Pine Script Function
Overview
Section titled “Overview”map() performs a linear interpolation that translates value expressed in the [from_min, from_max] range into a number that lies proportionally inside [to_min, to_max]. It is widely used to normalize indicators, convert booleans into numeric levels, or scale price moves to oscillator bounds.
Syntax
Section titled “Syntax”map(value, from_min, from_max, to_min, to_max) → series floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
value | series int/float | The value to convert. |
from_min | series int/float | Lower bound of the input range. |
from_max | series int/float | Upper bound of the input range. Must differ from from_min. |
to_min | series int/float | Lower bound of the target range. |
to_max | series int/float | Upper bound of the target range. |
Returns
Section titled “Returns”series float — The proportional value within [to_min, to_max].
Remarks
Section titled “Remarks”- When
from_max == from_min, the function returnsna(division by zero). - If
valuefalls outside the source range, the result can extrapolate beyond[to_min, to_max]. Clamp manually when you need strict bounds. - Combine with
math.min/math.maxto enforce bounds after the mapping.
Example
Section titled “Example”//@version=5indicator("map() oscillator scaling", overlay=false)
rsiLen = input.int(21, "RSI length")
rsiValue = ta.rsi(close, rsiLen)normalized = map(rsiValue, 0, 100, -1, 1)
plot(normalized, "Mapped RSI", color=color.teal)hline(0.0, "Midline", color=color.gray)bgcolor(math.abs(normalized) > 0.5 ? color.new(color.orange, 85) : na)