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

Understanding the math.toradians() Function in Pine Script

Photo of author
Published on

In this article, we delve into the math.toradians() function in Pine Script, an essential tool for traders and developers working on TradingView. This function converts an angle measured in degrees to an approximately equivalent angle in radians, a crucial process in various technical analysis calculations and indicators that involve trigonometric functions.

Syntax

The syntax for the math.toradians() function is straightforward:

math.toradians(degrees) → series float

Arguments

  • degrees (series int/float): This argument represents the angle in degrees that you wish to convert into radians. Pine Script accepts both integer and floating-point numbers for this parameter, offering flexibility in input values.

Returns

  • The function returns the angle value in radians as a series float. This return type ensures that you can work with the resulting values in a time series, allowing for dynamic and responsive indicator and strategy development.

Example

To fully grasp the math.toradians() function’s utility, let’s explore an example where we convert a simple moving average (SMA) of closing prices from degrees to radians. This example not only illustrates the function’s syntax but also demonstrates its application in trading strategy development.

//@version=5
indicator("Degrees to Radians Conversion", overlay=true)

// Calculate a simple moving average of the closing prices
smaClose = ta.sma(close, 14)

// Convert the SMA values from degrees to radians
smaRadians = math.toradians(smaClose)

// Plot the result
plot(smaRadians, title="SMA in Radians", color=color.blue)
Example

Walkthrough of Code

  1. Indicator Declaration:
    • //@version=5: Specifies the version of Pine Script used, which is version 5 in this case.
    • indicator("Degrees to Radians Conversion", overlay=true): Declares a new indicator titled “Degrees to Radians Conversion”. The overlay=true parameter ensures that this indicator is drawn over the main chart, allowing you to see the indicator plotted directly on the price chart.
  2. Calculate a Simple Moving Average (SMA):
    • smaClose = ta.sma(close, 14): Calculates the simple moving average (SMA) of the closing prices over the last 14 periods. close represents the closing price of each candle/bar, and 14 is the number of periods over which the SMA is calculated. The result is stored in the variable smaClose.
  3. Convert SMA Values from Degrees to Radians:
    • smaRadians = math.toradians(smaClose): Converts the SMA values, presumably measured in degrees, to radians. The conversion is done using the math.toradians function, which takes the degrees value (smaClose in this case) as its argument. The result of this conversion is stored in the variable smaRadians.
  4. Plot the Result:
    • plot(smaRadians, title="SMA in Radians", color=color.blue): Plots the converted SMA values (now in radians) on the chart. The plot is labeled “SMA in Radians” and is displayed in blue color for easy identification against the price chart and other indicators.

Takeaways

  • The math.toradians() function is crucial for converting angles from degrees to radians in Pine Script.
  • Its straightforward syntax and flexibility in handling both integer and floating-point numbers make it a versatile tool in technical analysis calculations.
  • By applying this function, traders and developers can enhance their strategies and indicators with trigonometric functions, opening up a broader range of analytical possibilities.

Understanding and utilizing the math.toradians() function is essential for anyone looking to deepen their expertise in Pine Script programming and develop more complex and accurate trading tools on TradingView.

Leave a Comment