Home » Mathemtical Functions » Understanding the math.atan() Function in Pine Script

Understanding the math.atan() Function in Pine Script

Photo of author
Published on

Let’s dive into the details of the math.atan() function, its syntax, usage, and practical applications in trading scripts.

What is the math.atan() Function?

The math.atan() function computes the arctangent of a given number. The arctangent is the inverse operation of the tangent, which means that math.atan() returns the angle whose tangent is the given number. This function is essential in trigonometry, and in the context of Pine Script, it can be used to calculate angles based on price changes or to adjust indicator outputs.

Syntax

Pine Script’s math.atan() function can be used with various types of inputs, making it versatile for different scripting scenarios. Here’s a breakdown of its syntax and overloads:

math.atan(angle) → const float
math.atan(angle) → input float
math.atan(angle) → simple float
math.atan(angle) → series float
  • angle: This argument represents the value for which you want to calculate the arctangent. It can be a constant, input, simple, or series float/int.

Arguments

  • angle (const int/float): The value, in radians, for which the arctangent is calculated.

Returns

The function returns the arctangent of the input value. The returned angle is within the range [-π/2, π/2] radians.

Practical Example: Calculating Angle Between Two Points

Let’s illustrate the use of math.atan() with a simple example: calculating the angle between two points on a price chart, which can be useful for determining the slope of a trendline.

//@version=5
indicator("Angle between two points", overlay=true)

// Define two points based on price at different times
price1 = close[20] // Price 20 bars ago
price2 = close // Current price

// Calculate the horizontal distance between the two points
timeDifference = 20

// Calculate the vertical difference (rise) between the two points
priceDifference = price2 - price1

// Calculate the angle in radians
angleInRadians = math.atan(priceDifference / timeDifference)

// Convert angle to degrees for better readability
angleInDegrees = angleInRadians * (180 / math.pi)

// Plot the angle value on the chart
plot(angleInDegrees, title="Angle in Degrees", color=color.blue)
Example

Walkthrough

  1. Define Points: We select two prices based on the closing prices at different times to define two points.
  2. Calculate Differences: We compute the horizontal (time) and vertical (price) differences between these points.
  3. Calculate Angle: Using math.atan(), we find the angle in radians between the line connecting these two points and the time axis.
  4. Convert to Degrees: Since radians can be less intuitive, we convert the angle to degrees for better readability.

Key Features and Takeaways

  • Function Useability: math.atan() is versatile, accepting different types of numeric inputs, making it suitable for a wide range of calculations in trading scripts.
  • Syntax and Application: The function’s syntax is straightforward, but understanding its application requires a grasp of trigonometry and its relevance to trading concepts.
  • Practical Applications: Beyond calculating angles, math.atan() can be part of more complex formulas in trading strategies, such as custom oscillators or indicators that adapt based on the slope of trendlines.

In conclusion, the math.atan() function in Pine Script is a powerful tool for traders and developers looking to incorporate mathematical and trigonometric calculations into their trading strategies. Understanding how to use this function effectively can open up new possibilities for technical analysis and indicator development on TradingView.

Leave a Comment