Understanding the NA Function in Pine Script
The na()
function in Pine Script is one of those things that seems simple at first, but it's actually pretty important once you start building more complex indicators. If you've ever wondered why your script crashes on certain bars or why some calculations just don't work right, there's a good chance it has to do with missing data - and that's where na()
comes in handy.

What's the deal with NA anyway?
So here's the thing - when you're working with price data, sometimes certain values just don't exist yet. Think about it: if you're on the very first bar of a chart and you try to reference close[1]
(yesterday's close), what should happen? There is no yesterday! That's where Pine Script uses something called "na" which basically means "not available" or "no data here."
The na()
function is super straightforward - you give it a value, and it tells you whether that value is missing or not:
na(x) → true or false
It works with pretty much any type of data you throw at it - numbers, colors, strings, you name it.
Why should you care?

Here's where it gets practical. Let's say you're building an indicator that compares today's price to the price from 5 days ago. What happens on the first 4 bars of your chart? There's no data from 5 days ago because... well, those bars don't exist yet.
Without checking for na
, your script might:
- Crash completely
- Give you weird results
- Just not work on certain timeframes
Here's a simple example that shows how this works:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//======================================================================//
// ____ _ _ __ //
// | _ \(_)_ __ ___(_)/ _|_ _ //
// | |_) | | '_ \ / _ \ | |_| | | | //
// | __/| | | | | __/ | _| |_| | //
// |_| |_|_| |_|\___|_|_| \__, | //
// |___/ //
//======================================================================//
//@version=6
indicator("Simple NA Example")
// Check if yesterday's close exists before using it
plot(na(close[1]) ? close : close[1])
What this does is pretty clever: if yesterday's close doesn't exist (like on the first bar), it just plots today's close instead. If yesterday's close does exist, it plots that. No crashes, no weird behavior.
A quick detour: NA vs NZ
While we're talking about handling missing data, there's another function called nz()
that's worth knowing about. Instead of just telling you "hey, this is missing," nz()
actually gives you a backup value when something is missing.
// If close is missing, use open instead
plot(nz(close, open))
It's like having a Plan B built right into your code.
Some practical tips
After working with Pine Script for a while, here are a few things I've learned about dealing with na
:
Always check before you calculate: If you're doing math with historical data, check if that data exists first. It'll save you a lot of headaches.
Test with different timeframes: What works on a daily chart might break on a 1-minute chart because there's less historical data available.
Don't overthink it: Most of the time, a simple na()
check is all you need. You don't have to get fancy with it.
Use it in conditions: Something like if not na(close[10])
is a clean way to make sure you have enough data before running your calculations.
The bottom line
The na()
function isn't glamorous, but it's one of those things that separates scripts that work reliably from scripts that randomly break. It's basically Pine Script's way of helping you deal with the messy reality that data isn't always perfect or complete.
Once you get in the habit of checking for missing data, you'll find your scripts become way more stable and work across different markets and timeframes without throwing errors. And honestly, that's worth the extra line or two of code.