Globals
NA helpers, series storage, logging, bar info, and display utilities.
NA and Series Helpers
na
na -> NaN
na() -> NaN
na(value) -> booleanna can be used either as a missing-value sentinel or as a function.
When called with no arguments, returns NaN. Bare na and na() are equivalent when you want to represent a missing plotted value.
When called with a value, returns true if the value is missing (undefined, null, NaN, or na).
Numeric TA/math builtins use NaN as their normal missing-value sentinel during warmup or invalid numeric states.
| Param | Type | Description |
|---|---|---|
value | any | Value to check |
//@version=2
indicator("na Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const val = candles.close() >= candles.open() ? candles.close() : na
if (na(ta.sma(candles.close(), 50))) return
plot("Conditional Close", val, { color: color.blue })
}nz
nz(value, replacement?) -> numberReturns value if it's a valid number, otherwise returns replacement.
| Param | Type | Default | Description |
|---|---|---|---|
value | any | — | Value to check |
replacement | number | 0 | Fallback if value is missing |
//@version=2
indicator("nz Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const safe = nz(ta.ema(candles.close(), 20), 0)
plot("Safe EMA", safe, { color: color.orange })
}Series
Series(name) -> number[]Returns a mutable named series array for storing per-bar values across evaluations.
| Param | Type | Description |
|---|---|---|
name | string | Unique series identifier |
//@version=2
indicator("Series Example", false)
const candles = subscribe(data.OHLCV)
const myData = Series("custom")
function onBar(index) {
myData[barIndex()] = candles.close() - candles.open()
plot("Close - Open", myData[barIndex()], { color: color.teal })
}Logging
log
log(...args) -> voidWrites arguments to the script log panel. Accepts any number of values.
//@version=2
indicator("log Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
if (!context.isNew) return
log("price:", candles.close(), "rsi:", ta.rsi(candles.close(), 14))
}Bar Info
unix
unix(offset?) -> numberReturns the unix timestamp (seconds) of the bar at offset from the first subscription.
| Param | Type | Default | Description |
|---|---|---|---|
offset | number | 0 | 0 = current bar, 1 = previous, etc. |
barCount
barCount() -> numberCompatibility helper. Returns the number of bars currently available in the first subscription.
barIndex
barIndex() -> numberReturns the absolute index of the current bar within the loaded context (0 = oldest loaded bar).
loadedBarCount
loadedBarCount() -> numberReturns the total number of bars in the loaded context. Unlike barCount(), this is the stable loaded-context size rather than the current first-subscription bar count kept for compatibility.
lastBarIndex
lastBarIndex() -> numberReturns the last absolute bar index in the loaded context. This is typically loadedBarCount() - 1.
isNewBar
isNewBar() -> booleanReturns true when the current bar's unix timestamp differs from the previous evaluation. Useful for detecting when a new candle has formed.
isRealtimeBar
isRealtimeBar() -> booleanReturns !isNewBar(). true when the current bar is being updated in-place.
Display
setDisplayName
setDisplayName(name) -> voidOverride the indicator title shown in the chart UI.
| Param | Type | Description |
|---|---|---|
name | string | New display name |
//@version=2
indicator("RSI Example", false)
const len = input.int("Length", 14, { min: 1 })
const candles = subscribe(data.OHLCV)
setDisplayName("RSI (" + len + ")")
function onBar(index) {
plot("RSI", ta.rsi(candles.close(), len), { color: color.blue })
}agg
agg(...exchanges) -> string
agg(exchanges) -> stringBuilds a canonical aggregated exchange string from exchange ids.
- accepts either multiple string arguments or a single
string[] - splits existing colon-separated aggregate strings
- sorts exchanges alphabetically
- removes duplicates
//@version=2
indicator("Aggregation Helper Example", false)
const ex = agg(["bybitf", "binancef"], "okxf:bybitf")
const oi = subscribe(data.OI, { exchange: ex })
function onBar(index) {
plot("OI Close", oi.close(), { color: color.blue })
}getExchangeList
getExchangeList() -> string[]Returns an array of available exchange identifiers.
//@version=2
indicator("Exchange List Example", false)
const exchanges = getExchangeList()
function onBar(index) {
if (!context.isFirst) return
log("Available:", exchanges.join(", "))
}