log() - Pine Script Function
Overview
Section titled “Overview”log() calculates the natural logarithm (base e) of the supplied value. It is frequently used to convert price ratios into additive log returns or to undo an exp() operation.
Syntax
Section titled “Syntax”log(x) → series floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
x | series int/float | Value whose natural logarithm to compute. Must be strictly greater than 0. |
Returns
Section titled “Returns”series float — the natural logarithm of x.
Remarks
Section titled “Remarks”log()andmath.log()are equivalent aliases.- Passing a non-positive value (
x <= 0) yieldsna. - Combine with
exp()to reverse the transformation:exp(log(x)) == x.
Example
Section titled “Example”//@version=5indicator("Log return volatility", overlay=false)
period = input.int(20, "Return period")
// Convert price changes to log returns.logReturn = log(close / close[period])
plot(logReturn, "Log return", color=color.new(color.blue, 0))plot(ta.sma(logReturn, period), "Average", color=color.orange)
// Highlight unusually large log moves.threshold = ta.stdev(logReturn, period) * 2.0bgcolor(math.abs(logReturn) > threshold ? color.new(color.red, 90) : na)