Skip to content

log() - Pine Script Function

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.

log(x) → series float
NameTypeDescription
xseries int/floatValue whose natural logarithm to compute. Must be strictly greater than 0.

series float — the natural logarithm of x.

  • log() and math.log() are equivalent aliases.
  • Passing a non-positive value (x <= 0) yields na.
  • Combine with exp() to reverse the transformation: exp(log(x)) == x.
//@version=5
indicator("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.0
bgcolor(math.abs(logReturn) > threshold ? color.new(color.red, 90) : na)