Sidechair

math.log()

Browse the reference

Returns the natural logarithm of a number: the power e must be raised to in order to get it.

Try it in the editor

math.log(x) answers the question: e to what power gives x. It is defined for positive numbers, and gives na for negatives.

Log of a price ratio is a continuously compounded return, which adds across bars. That makes math.log(close / close[1]) the standard input for volatility.

Syntax

Pine Script
math.log(number: simple int/float) -> simple float
math.log(number: input int/float) -> input float
math.log(number: const int/float) -> const float
math.log(number: series int/float) -> series float

Arguments

NameTypeDefaultDescription
numberconst/input/simple/series int/floatrequiredThe number to take the natural logarithm of. 0 or below gives na.

Returns

const/input/simple/series float: the natural logarithm of the number.

Remarks

  • math.log(1) is 0.

Example

Pine ScriptLog return columnsOpen in editor
//@version=6
indicator("Log return")
r = math.log(close / close[1]) * 100
plot(r, "Log return %", color = r >= 0 ? color.green : color.red, style = plot.style_columns)

See also

Runs on Sidechair