Variables and Types#
Declaring Variables#
In Pine Script, variables are declared with = and the type is inferred automatically by the compiler:
myLength = 14 // integer (int)
myLevel = 50.5 // float
mySignal = true // boolean (bool)
myLabel = "Breakout" // stringOnce declared, a variable is recalculated and updated on every bar execution.
Types#
Pine Script basic types:
| Type | Description | Example values |
|---|---|---|
int | Integer | 14, -3, 0 |
float | Floating point | 1.5, 0.618, na |
bool | Boolean | true, false |
string | String | "buy", "MA crossover" |
color | Color | color.red, #FF0000 |
na — Not Available#
na is a Pine Script-specific value representing data that does not exist or has not been defined yet, similar to null or NaN in other languages.
For example, when calculating a 14-period RSI, the first 13 bars don’t have enough data, so the RSI value is na for those bars.
Use the na(x) function to check whether a value is na:
rsiValue = ta.rsi(close, 14)
if na(rsiValue)
label.new(bar_index, high, "Insufficient data")var — Persisting Variables Across Bars#
By default, variables are re-initialized on every bar. If you need to accumulate values across bars, declare with the var keyword:
//@version=6
indicator("Volume Accumulation Demo")
// Without var: resets to 0 on every bar, count always equals 1
// count = 0
// With var: initialized to 0 only on the first bar, retains value afterward
var count = 0
count := count + 1 // Use := to update an already-declared variable
plot(count, title="Bar Count")A var variable’s initial value is assigned only on the first bar; on every subsequent bar it retains the value from the previous bar.
Updating Variable Values#
After declaring a variable, use the := operator to update its value (rather than re-declaring it):
var highestHigh = high // Initialize
if high > highestHigh
highestHigh := high // Update to a higher valueNote:
=is for declaration,:=is for update. Mixing them causes a compile error.
Series vs. Simple Types#
Pine Script variables have two “qualifiers”:
- series: Each bar has its own independent value; past values are accessible via
[N] - simple / const: A single value that doesn’t change throughout the script’s execution
// series float: different close price on every bar
myClose = close // series float
// simple int: fixed parameter that doesn't change
length = input.int(14, "Period") // simple intIn most cases you don’t need to specify this manually — Pine Script infers it automatically. However, some function parameters require a simple type, which means you cannot pass a series value that changes bar by bar.
input — Adjustable Parameters#
Use the input family of functions to create parameters that can be adjusted in the chart UI without modifying the code:
//@version=6
indicator("RSI Indicator")
// Create adjustable parameters
length = input.int(14, "RSI Period", minval=1, maxval=100)
overbought = input.int(70, "Overbought", minval=50, maxval=100)
oversold = input.int(30, "Oversold", minval=0, maxval=50)
rsiValue = ta.rsi(close, length)
plot(rsiValue, "RSI", color=color.purple)
hline(overbought, "Overbought", color=color.red)
hline(oversold, "Oversold", color=color.green)Commonly used input functions:
| Function | Type | Description |
|---|---|---|
input.int(defval, title) | int | Integer input box |
input.float(defval, title) | float | Float input box |
input.bool(defval, title) | bool | Checkbox |
input.string(defval, title) | string | Dropdown or text box |
input.source(defval, title) | series float | Data source selector (close/open/hl2…) |
Type Conversion#
Pine Script handles type conversion automatically in most cases, but sometimes manual conversion is needed:
myInt = 10
myFloat = float(myInt) // int → float
myFloat2 = 3.7
myInt2 = int(myFloat2) // float → int (truncated, result is 3)
myNum = 42
myStr = str.tostring(myNum) // number → string