Pine Script, the scripting language designed for custom indicators and strategies on TradingView, includes various mathematical functions to assist with complex calculations. One such function is `math.log()`

, which computes the natural logarithm of a given number. The natural logarithm is the power to which the base `e`

(Euler’s number, approximately 2.71828) must be raised to produce a certain number. This article explores the `math.log()`

function in Pine Script, covering its syntax, overloads, arguments, and applications.

## Syntax

Pine Script’s `math.log()`

function can be used in different contexts with varying types of input, which are referred to as overloads. The basic syntax of the `math.log()`

function is:

math.log(number) → const float math.log(number) → input float math.log(number) → simple float math.log(number) → series float

Each overload serves different types of variables, ranging from constants to series data, allowing for flexibility in calculations across various scenarios in trading scripts.

### Arguments

The `math.log()`

function accepts a single argument:

`number`

(const int/float): This is the number for which the natural logarithm is calculated. It’s important to note that the`number`

should be greater than 0, as the logarithm of zero or negative numbers is undefined in real numbers.

### Returns

The function returns the natural logarithm of the provided number. The return type can vary depending on the context in which the function is used but is generally a float value.

### Application and Examples

### Example 1: Calculating the Natural Logarithm of a Constant

//@version=5 indicator("Natural Log Example", overlay=true) logValue = math.log(10) plot(logValue, title="Natural Log of 10")

### Walkthrough of Code

**Indicator Declaration**:`indicator("Natural Log Example", overlay=true)`

declares a new indicator named “Natural Log Example”. The`overlay=true`

parameter indicates that this indicator is intended to be overlaid on the main price chart, not plotted in a separate panel.

**Calculating the Natural Logarithm of a Constant Value**:`logValue = math.log(10)`

calculates the natural logarithm (log to the base e) of the constant value 10. The result of this calculation is stored in the variable`logValue`

. The`math.log()`

function is used to perform the logarithm calculation.

**Plotting the Calculated Logarithm Value**:`plot(logValue, title="Natural Log of 10")`

plots the calculated natural logarithm value as an indicator on the chart. Since`logValue`

is a constant (resulting from the natural log of 10), the plot will be a straight horizontal line across the chart at the value of the natural log of 10. The title “Natural Log of 10” is displayed on the indicator to describe what is being plotted.

### Example 2: Using `math.log()`

with Series Data

//@version=5 indicator("Log of Series Data", overlay=false) priceLog = math.log(close) plot(priceLog, title="Natural Log of Closing Price")

Here, `math.log(close)`

computes the natural logarithm of the closing price, a series float, for each bar on the chart. This example demonstrates the function’s utility with time-series data, a common application in financial analysis.

### Walkthrough of Code

**Indicator Declaration**:`indicator("Log of Series Data", overlay=false)`

declares a new indicator named “Log of Series Data”. The`overlay=false`

parameter specifies that this indicator should be drawn in a separate panel below the main price chart, not overlaid on top of the price chart.

**Calculating the Natural Logarithm of Closing Prices**:`priceLog = math.log(close)`

calculates the natural logarithm (log to the base e) of the closing price of the financial instrument for each bar or candlestick on the chart. The`close`

keyword represents the closing prices, and`math.log()`

is the function that computes the natural logarithm.

**Plotting the Natural Logarithm of Closing Prices**:`plot(priceLog, title="Natural Log of Closing Price")`

plots the calculated natural logarithm values on the chart. The title “Natural Log of Closing Price” is displayed on the indicator panel to indicate what is being plotted.

## Key Features and Takeaways

**Flexibility**:`math.log()`

can be used with constant, input, simple, and series floats, making it versatile for various scripting scenarios.**Applicability**: Essential for exponential and logarithmic calculations, such as compound interest calculations or growth rates in trading algorithms.**Limitations**: The argument must be greater than 0, as the logarithm of non-positive numbers is not defined in the context of real numbers.

Understanding and utilizing the `math.log()`

function can significantly enhance the analytical capabilities of your custom Pine Script indicators and strategies, enabling more sophisticated financial analyses and decision-making processes.