In this article, we will delve into how to use the str.tonumber()
function in Pine Script version 5, providing comprehensive examples and explanations.
Using str.tonumber()
Function
The str.tonumber()
function in Pine Script version 5 is designed to convert a string that represents a number into a numeric value. If the string cannot be converted to a number, the function returns na
(not a number). This is particularly useful when dealing with data that comes in text format but needs to be used in numerical calculations.
Syntax
numericValue = str.tonumber(string)
string
: The string input that you want to convert to a number.
Example
Let’s consider an example where we have a string variable that contains a number, and we want to convert this string to a numeric value to perform mathematical operations.
//@version=5 indicator("String to Number Conversion", overlay=true) // Define a string variable that represents a number stringNumber = "123.45" // Convert the string to a numeric value numericValue = str.tonumber(stringNumber) // Plot the numeric value on the chart plot(numericValue, title="Numeric Value", color=color.blue)
In this example, stringNumber
is a string variable that holds the value "123.45"
. We use the str.tonumber(stringNumber)
function to convert it into a numeric value, which is then stored in the variable numericValue
. This numeric value is plotted on the chart, demonstrating the conversion from string to number.
Detailed Walkthrough
- Script Version Declaration:
//@version=5
indicates that this script is written for Pine Script version 5, ensuring compatibility with the latest features and functionalities offered by TradingView’s Pine Script language.
- Indicator Declaration:
indicator("String to Number Conversion", overlay=true)
defines a new indicator titled “String to Number Conversion”. Theoverlay=true
parameter specifies that this indicator will be plotted directly on the trading chart, overlaying the price data.
- String Variable Definition:
stringNumber = "123.45"
declares a variable namedstringNumber
and assigns it a string value of"123.45"
. This string represents a numeric value (in this case, a floating-point number) as text.
- String to Numeric Conversion:
numericValue = str.tonumber(stringNumber)
uses thestr.tonumber()
function to convert the string stored instringNumber
to a numeric value. The result of this conversion is stored in a new variable namednumericValue
.- This conversion is crucial for further mathematical or analytical operations, as it transforms the textual representation of a number into a format that can be numerically manipulated.
- Plotting the Numeric Value:
plot(numericValue, title="Numeric Value", color=color.blue)
plots the numeric value obtained from the conversion on the chart. Theplot
function is used for this purpose, withnumericValue
as the data to be plotted.- The
title="Numeric Value"
parameter specifies the title of the plot, which will be visible on the chart’s legend. - The
color=color.blue
parameter sets the color of the plot to blue, enhancing the visual distinction of this plotted data on the chart.
Key Features and Takeaways
- Function Useability:
str.tonumber()
is a versatile function in Pine Script version 5 for converting strings that represent numbers into numeric values, enhancing the script’s ability to handle dynamic data types. - Syntax and Application: The function is straightforward to use, with a single argument that is the string to be converted. It returns a numeric value if successful or
na
if the conversion fails. - Error Handling: Implementing error checking when using
str.tonumber()
is crucial for robust script development, allowing for graceful handling of non-numeric strings or malformed inputs.