Skip to content

Built-in Functions

This page documents the callable Rune surface shipping publicly in script(version=1).

Stream Constructors

These are valid in stream name = ... declarations.

ConstructorDescription
ohlcv()Bind the current chart candles
ohlcv(timeframe="15m")Bind a same-instrument higher/same timeframe stream
ohlcv(timeframe=higher_tf)Same as above, but timeframe can come from a param
ohlcv(bar="1H")Alias for timeframe=...
ohlcv(exchange="blofin", symbol="ETH-USDT", timeframe="15m")Bind an external stream supplied by the host runtime

Core Logic

FunctionDescription
if(condition, then, else=null)Inline conditional
lag(value, offset)Value from offset bars ago
prev(value, fallback=null)Previous-bar value with optional fallback
pct(value)Pass-through helper mainly used for sizing
coalesce(...)First non-null value
nz(...)Alias for coalesce(...)
abs(value)Absolute value
min(...)Smallest numeric input
max(...)Largest numeric input
round(value)Rounded numeric value
floor(value)Rounded down
ceil(value)Rounded up
clamp(value, min, max)Restrict a value to a range
log(value)Natural logarithm. ln(value) is the same function
exp(value)e raised to value — the inverse of log
sqrt(value)Principal square root
pow(base, exponent)base raised to exponent

log(...) is the natural log (Pine's math.log), not base 10. Its inverse is exp(...), which is what maps a log-price level back to a real price.

All four return null, never NaN or ±Infinity, when the maths has no real answer — a non-positive log, a negative sqrt, an overflowing exp, or a pow with a negative base and a fractional exponent. That matters more than it sounds: a single NaN propagates through every rolling window downstream of it, so a bad bar would poison the rest of the series. null just breaks the plot at that bar.

Bar and Time

FunctionDescription
true_range()True range for the current bar
tr()Alias for true_range()
resolution_ms()Current chart resolution in milliseconds
bar_index()Zero-based bar index
bars()Total bars in the current dataset
time_at(offset=0)Timestamp at the current or prior offset

Timezone and Calendar

FunctionDescription
timestamp(year, month, day, hour=0, minute=0, tz="UTC")Build a timezone-aware timestamp in milliseconds
year(value=time, tz="UTC")Extract year
month(value=time, tz="UTC")Extract month
day_of_month(value=time, tz="UTC")Extract calendar day
dayofmonth(value=time, tz="UTC")Alias for day_of_month(...)
day_of_week(value=time, tz="UTC")ISO weekday number
dayofweek(value=time, tz="UTC")Alias for day_of_week(...)
hour(value=time, tz="UTC")Extract hour
minute(value=time, tz="UTC")Extract minute
in_session(session, tz="UTC")Check whether current bar time falls inside a HHMM-HHMM window

Averages and Statistics

FunctionDescription
sma(source, period)Simple moving average
ma(source, period)Alias for sma(...)
ema(source, period)Exponential moving average
rma(source, period)Wilder-style running average
wilder(source, period)Alias for rma(...)
wma(source, period)Weighted moving average
vwma(source, period)Volume-weighted moving average
vwap(reset="day", tz="UTC")Volume-weighted average price; reset is one of day, session, week, month, quarter, year
vwap_stdev(reset="day", tz="UTC")Anchored volume-weighted standard deviation around the VWAP — the half-width unit for VWAP bands; same reset values as vwap(...)
swma(source)Symmetrically weighted moving average
alma(source, period, offset=0.85, sigma=6)Arnaud Legoux moving average
hma(source, period)Hull moving average
zlema(source, period)Zero-lag EMA
atr(period)Average true range
stdev(source, period)Rolling standard deviation
dev(source, period)Rolling mean absolute deviation from the window's SMA (TradingView ta.dev parity)
rsi(source, period)Relative strength index

Anchored Period Data

These return values from the prior completed anchor period (previous day, week, and so on), held constant across the current period — the usual basis for pivot points. They return null until a prior period exists. reset is one of day, session, week, month, quarter, year.

FunctionDescription
prev_high(reset="day", tz="UTC")Highest high of the previous period
prev_low(reset="day", tz="UTC")Lowest low of the previous period
prev_close(reset="day", tz="UTC")Closing price of the previous period

Rolling Windows and Momentum

FunctionDescription
highest(source, period)Highest value in the trailing window
lowest(source, period)Lowest value in the trailing window
highestbars(source, period)Bars since the highest value in the trailing window
lowestbars(source, period)Bars since the lowest value in the trailing window
barssince(condition)Bars since the condition last evaluated true
sum(source, period)Rolling sum
cum(source)Cumulative running total
valuewhen(condition, source, occurrence=0)Source value captured at the Nth most recent true condition
change(source, length=1)Difference from length bars ago
roc(source, period=1)Percent rate of change
count(condition, period)Count of truthy bars in the trailing window
any(condition, period)True when any bar in the window is truthy
all(condition, period)True when every bar in the window is truthy
macd(source, fast=12, slow=26, signal=9)Returns MACD line, signal line, histogram
bbands(source, period, mult=2)Returns upper band, basis, lower band (in that order)
bollinger(source, period, mult=2)Alias for bbands(...)

Tuple Examples

let macd_line, signal_line, hist = macd(close, 12, 26, 9)
let upper, basis, lower = bbands(close, 20, 2)

Structure and Crossovers

FunctionDescription
pivot_high(source, left, right=left)Confirmed swing high
pivot_low(source, left, right=left)Confirmed swing low
cross_up(left, right)True when left crosses above right
cross_down(left, right)True when left crosses below right
cross_over(left, right)Alias for cross_up(...)
cross_under(left, right)Alias for cross_down(...)

Higher-Timeframe and Alternate Candle Data

FunctionDescription
htf_open(resolution_ms)Higher-timeframe open
htf_high(resolution_ms)Higher-timeframe high
htf_low(resolution_ms)Higher-timeframe low
htf_close(resolution_ms)Higher-timeframe close
ha_open()Heikin Ashi open
ha_high()Heikin Ashi high
ha_low()Heikin Ashi low
ha_close()Heikin Ashi close

CRODL Trend Functions

FunctionDescription
cw_trail(stop_len, stop_mult)CRODL trail line
cw_fib1(stop_len, stop_mult)First CRODL retracement level
cw_fib2(stop_len, stop_mult)Second CRODL retracement level
cw_fib3(stop_len, stop_mult)Third CRODL retracement level
cw_dir(stop_len, stop_mult)CRODL trend direction
cw_bull_flip(stop_len, stop_mult)Bullish trend flip
cw_bear_flip(stop_len, stop_mult)Bearish trend flip
cw_ex(stop_len, stop_mult)CRODL excursion anchor
cw_score(stop_len, stop_mult, heat_len, heat_levels, heat_thresh)CRODL heat score
cw_signal(long_signal, short_signal, score, score_threshold, cooldown_bars)CRODL signal filter with cooldown

Position-State Builtins

These are primarily useful in strategy scripts with protect(...).

FunctionDescription
position_qty(side, target=null)Current open quantity
position_avg_entry(side, target=null)Average entry price
position_stop(side, target=null)Active stop
position_take(side, target=null)Active take-profit
position_bars(side, target=null)Bars since the oldest matching open leg
position_initial_stop(side, target=null)Original stop attached at entry
position_initial_risk(side, target=null)Original per-unit risk
position_open_r(side, target=null)Unrealized PnL in R-multiples

Contextual DSL Helpers

These are valid inside trade(...) and risk: blocks, not as general-purpose expression calls.

HelperDescription
pct(x)Percentage sizing helper, for example size=pct(50)
cash(x)Fixed-cash budget helper, for example risk=cash(100)
equity_pct(x)Equity-percentage budget helper, for example risk=equity_pct(1)

Helper Functions You Define

Rune also supports your own pure helper functions:

calc rising(value):
  return value > prev(value, value)

These support:

  • positional arguments
  • named arguments
  • tuple returns

Chart-Only Functions

These exist in the chart's script runtime but have no server-side equivalent. Your script renders correctly on the chart; an alert built on it fails when the server tries to evaluate it, because the server's interpreter does not know these names.

Bounded lists

list(...) is a fixed-capacity buffer, not a general array. New values go on the front, and once it is full each push drops the oldest value off the back.

FunctionDescription
list(capacity=100)Create a bounded list
push(list, value)Add to the front, evicting the oldest when full. Returns the value
pop(list)Remove and return the front value, or null when empty
peek(list, offset=0)Read without removing. 0 is the newest. Out of range returns null
len(list)Current number of items
clear(list)Empty the list

There are still no loops, no maps or dictionaries, and no array literals — a bounded list is the only collection Rune has.

Other chart-only names

FunctionDescription
corr(source1, source2, period)Rolling correlation over period bars. correlation(...) is the same function
barindex()Alias for bar_index()

Prefer bar_index() — it is the documented spelling and it works server-side. The undotted barindex() alias is chart-only, so a script that uses it renders identically but cannot back an alert.

Not Supported

These are not part of the public language and should not be used:

  • scan(...) — internal to the screener
  • emit(...) — internal debug hook
  • loops
  • maps or dictionaries
  • Pine-style imperative drawing APIs like line.new(...), label.new(...), or table.new(...)