Home » Data Types In Pinescript » int Function in Pine Script

int Function in Pine Script

Photo of author
Published on

Pine Script comes with several built-in functions that make it a powerful tool for strategy development and financial analysis. One such function is the int function. It is a basic yet essential component of the language, used for casting or truncating float values to integers. In this tutorial, we will dig deeper into understanding the int function and its use cases in Pine Script.

Definition of int Function

The int function in Pine Script is used to convert or cast float values into integers. When used, it can either return a simple integer, an input integer, a constant integer, or a series integer. This is usually necessary when dealing with data types that do not support float values.

The syntax of the function is fairly straightforward. It takes a single argument, x, which can be either a constant, input, or a series of floats. It then casts or truncates this value to an integer and returns the result. The syntax looks like this:

int(x)

Understanding the Different Use Cases of int Function

As a robust function, int can be used in a variety of contexts within Pine Script. Let’s discuss the different types of integers it can return and their relevance:

Simple int

The int function can return a simple integer. This is the basic use case, where it directly converts a floating-point number into an integer. The fractional part of the float value gets truncated.

Input int

When int is used with an input variable of type float, it casts that input value to an integer.

Const int

If you apply the int function on a constant floating-point number, it will return a constant integer value.

Series int

Finally, you can use the int function to convert a series of floating-point numbers into a series of integers.

Example

To ensure a comprehensive understanding of the int function in Pine Script, let’s break down the provided script:

//@version=5
indicator('My Script', overlay=false)
float_series = close / open
integer_series = int(float_series)
plot(integer_series, color=color.new(color.red, 0))
int Function

Directive and Indicator Definition

The first line //@version=5 is the version directive. This sets the version of Pine Script being used, which in this case is version 5.

indicator('My Script', overlay=false)

The second line calls the indicator function, which is used to define a new custom indicator. This particular indicator is given the title ‘My Script’. The overlay argument is set to false, which means the indicator will appear in a separate pane beneath the main chart, instead of overlaying on the price data.

Creating a Series of Float Values

float_series = close / open

Here, a new variable float_series is defined. This variable is assigned the value of the closing price divided by the opening price. This division operation creates a series of floating-point numbers that represents the ratio of close to open price for each period on the chart.

Casting Floats to Integers

integer_series = int(float_series)

This line is where the int function is utilized. It takes the series of floats we defined earlier (float_series) as its argument. The function then casts each float value in the series to an integer, and these integers are stored in a new variable called integer_series.

Plotting the Integer Series

plot(integer_series, color=color.new(color.red, 0))

Finally, the plot function is used to display the integer_series on the chart. The color.new(color.red, 0) syntax creates a new, fully opaque red color to be used for the plot. In Pine Script version 5, colors are transparent by default, and the second argument 0 in color.new() sets the transparency level to zero (fully opaque).

In summary, this script creates an indicator that displays a red line plot of the close-to-open price ratio for each period, with the ratios converted to integers to simplify the data.

Key Takeaways

The int function is a fundamental part of Pine Script, offering the ability to convert floating-point numbers into integers. It supports casting simple floats, input floats, constant floats, and series of floats into their corresponding integer forms. Understanding how to use it can aid in handling data types that do not support float values and simplifying complex data for analysis.

Conclusion

In conclusion, mastering the int function in Pine Script can be highly beneficial for creating and enhancing your strategies on TradingView. As with any programming language, understanding the basics will provide a solid foundation for more complex scripting tasks. Keep practicing with different use-cases to gain more familiarity with the int function.

Leave a Comment