FAQ

How to fix “Value with NA type cannot be assigned to a variable that was defined without type keyword.”

How to fix “Value with NA type cannot be assigned to a variable that was defined without type keyword.”

You can receive this error by running this simple code:

//@version=4
study("My Script")
plot(close)

x = na

Add to Chart operation failed, reason: line 5: Value with NA type cannot be assigned to a variable that was defined without type keyword

The issue is that PineScript doesn’t know what type should this variable be. It can’t guess the type from the NA value.

You have 2 ways of solving it. First, you can use a type keyword before defining this variable. For example, if you want to define a float variable you can use the following code:

float x = na

For character and integer variables, you can use string and int keywords.

The second way is to use built-in functions for every type in PineScript. It works following way:

x = float(na)

You can find functions for all types of PineScript, for example:

x = string(na)
x = color(na)
x = label(na)
x = line(na)
x = box(na)

So as you can see it’s really easy to fix this error, just let PineScript know in advance what is the type of your NA variable.


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png
Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news