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

NA Function in Pine Script

Photo of author
Published on

In the realm of Pine Script programming, understanding the function of each built-in function is crucial. Among them, the na() function plays a key role in handling missing or undefined data points in time series. This blog will provide a detailed exploration of the na() function, its syntax, and its use case in Pine Script.

What is the NA Function in Pine Script?

The na() function is a built-in function in Pine Script used to test if a specific value or variable, denoted as x, is ‘not a number’ (na). In the context of Pine Script, ‘na’ represents an undefined or missing value in a series of data.

The syntax for the na() function is as follows:

na(x) → simple bool
na(x) → series bool

The function takes an argument x, which can be any of the following types: int, float, bool, color, string, label, line, box, linefill. The function returns a Boolean value, i.e., true or false, depending on whether x is na or not.

Practical Use of the NA Function

The practical usage of the na() function becomes more evident when visualizing data on a trading chart. Let’s consider a simple yet practical example:

//@version=5
indicator("na")
// Use the `na()` function to test for `na`.
plot(na(close[1]) ? close : close[1])

In the script above, the na() function is used to check if the previous (one period ago) close price (close[1]) is na or not. If it is na, the current close price is plotted on the chart; if it is not na, the previous close price is plotted instead.

The na() function is especially useful when dealing with indicators that use past data points. In the case where past data points are unavailable or undefined, the na() function can be used to ensure that the script handles this elegantly without breaking the execution.

There is an alternative to the na() function called nz(), which also tests for na. The difference is that nz() returns close[1] if it is not na, and close if it is:

// ALTERNATIVE
// `nz()` also tests `close[1]` for `na`. It returns `close[1]` if it is not `na`, and `close` if it is.
plot(nz(close[1], close))

Key Takeaways

Here’s what you should remember about the na() function in Pine Script:

  1. na() is used to check if a value is ‘na’ or ‘not a number’, representing undefined or missing values in a series.
  2. The function accepts various data types as input and returns a Boolean value.
  3. It’s essential when dealing with indicators or scripts that require past data points.
  4. An alternative to na() is the nz() function, which behaves slightly differently in handling ‘na’ values.

Conclusion

Understanding how to handle missing or undefined data is a fundamental part of programming, and in Pine Script, this is where the na() function becomes incredibly useful. This function not only helps in maintaining the robustness of your script but also in improving the accuracy of your trading indicators and strategies. Keep practicing with different functions and happy coding!

Leave a Comment