Expression language
One small language for conditions and computed columns, used everywhere: screen (where, select, sort), event-study (condition), backtest (entry, exit) and query (select, where).
Expressions are parsed and evaluated by our own engine. They are never executed as code: there are no variables, loops, imports or calls outside the documented functions, and nothing you send can reach the server. Limits: 2,000 characters, 60 function calls, windows up to 5,000 bars.
An expression is evaluated over the whole price history and produces one value per bar. A screen looks at the last bar, an event study at every bar where the condition is true, a backtest trades on it. We will build one condition step by step:
rsi(14)[1] < 30 and close > sma(200) and close > close[1]
1Fields: the raw series
The simplest expression is a field: close, volume, high. Each is a series with one value per bar.
close the closing price of each bar volume (high - low) / close arithmetic works bar by bar: the day's range as a fraction
Operators: + - * /, unary -, and parentheses. Write percentages as 10% (= 0.10) or as decimals.
2Functions: indicators and windows
Functions turn a series into another series. Most take a window in bars; the series defaults to close, and you can pass another as the first argument.
sma(200) 200-bar simple moving average of close sma(volume, 20) ... of volume rsi(14) 14-bar RSI, 0 to 100 ret(20) 20-bar total return (0.10 = 10%) highest(high, 252) 52-week high
Window arguments must be whole numbers written in the expression (not computed). Before an indicator has enough history its value is null.
3Comparisons: from numbers to true/false
Compare two series with < <= > >= == !=. The result is 1 (true) or 0 (false) on each bar.
rsi(14) < 30 oversold close > sma(200) above the 200-day average between(rsi(14), 40, 60) neutral momentum
A condition is true when its value is greater than 0. A comparison involving null is false, so new listings simply don’t match until there is enough history.
4Combining with and, or, not
rsi(14) < 30 and close > sma(200) oversold inside an uptrend ret(5) < -5% or gap < -3% a sharp drop either way not rising(close, 5)
not binds tightest, then and, then or. Use parentheses when you mix them: (a or b) and c.
5Lags: looking back n bars
Put [n] after a field or function to read its value n bars ago. prev(x, n) does the same for any expression.
close[1] yesterday's close close > close[1] an up day rsi(14)[1] < 30 and rsi(14) >= 30 RSI just climbed back above 30 close > prev(highest(high, 252), 1) a close above yesterday's 52-week high
Lagging the reference level (as in the last line) matters: today’s 52-week high already includes today, so close > highest(high, 252) can never be true.
6Crossings: the bar where something changes
cross_above(a, b) is true only on the bar where a moves from below b to above it. That gives one signal per event instead of a run of true bars, which is what backtest entries and event studies usually want.
cross_above(sma(50), sma(200)) golden cross cross_below(close, sma(200)) losing the 200-day cross_above(macd(), macd_signal()) count(close > open, 10) >= 8 8 green bars out of 10 streak() == -3 third down close in a row
Putting it all together, “oversold in an uptrend, and the first up day after it”:
rsi(14)[1] < 30 and close > sma(200) and close > close[1]
Naming columns
In select, write expr as name. In screen, sort is an expression; in query, sort is a column name.
"select": ["rsi(14) as rsi", "close / sma(200) - 1 as dist_200d", "ret(20) as ret_1m"]
Errors
Invalid expressions return 400 BAD_EXPRESSION with a message and the position of the problem:
{ "ok": false, "error": { "code": "BAD_EXPRESSION", "message": "unknown function 'rsj' at position 0", "position": 0 } }
Fields
| Field | Meaning |
|---|---|
open (o) | Opening price of the bar. |
high (h) | High of the bar. |
low (l) | Low of the bar. |
close (c, price) | Closing price (split- and dividend-adjusted on daily bars). |
volume (v) | Volume. |
dollar_volume | close × volume. |
hl2 | (high + low) / 2. |
hlc3 | (high + low + close) / 3. |
range | high / low − 1: the bar’s range as a decimal. |
gap | open / previous close − 1. |
ret | Bar return: close / previous close − 1. |
day | Day of the month, 1–31. |
weekday | 1 = Monday … 7 = Sunday. |
month | 1–12. |
year | Calendar year. |
bar | Bar number from the start of the history (0, 1, 2 …). |
pi | The constant π. |
Functions (49)
[x,] means the series argument is optional (default close). n=14 shows a default window.
| Signature | What it does |
|---|---|
abs(x) | Absolute value. |
adx(n=14) | Average directional index (trend strength, 0-100). |
atr(n=14) | Average true range (Wilder). |
atr_pct(n=14) | ATR as a fraction of close. |
bb_lower(n=20, k=2) | Bollinger band lower (n=20, k=2). |
bb_mid(n=20, k=2) | Bollinger band mid (n=20, k=2). |
bb_pct(n=20, k=2) | Bollinger band pct (n=20, k=2). |
bb_upper(n=20, k=2) | Bollinger band upper (n=20, k=2). |
bb_width(n=20, k=2) | Bollinger band width (n=20, k=2). |
between(x, lo, hi) | 1 if lo <= x <= hi. |
cci(n=20) | Commodity channel index. |
change([x,] n) | x - x[n]. |
count(cond, n) | How many of the last n bars the condition was true. count(close > open, 10). |
cross_above(a, b) | 1 on the bar where a crosses above b. cross_above(sma(50), sma(200)). |
cross_below(a, b) | 1 on the bar where a crosses below b. |
dist_high(n=252) | close / highest(high, n) - 1. dist_high(252) = distance from 52-week high. |
dist_low(n=252) | close / lowest(low, n) - 1. |
drawdown() | Distance from the running all-time high of close (0 = at high, -0.2 = 20% below). |
ema([x,] n) | Exponential moving average. |
falling([x,] n) | 1 if x is lower than n bars ago. |
highest([x,] n) | Highest value in the last n bars (default series: high). |
log(x) | Natural log. |
lowest([x,] n) | Lowest value in the last n bars (default series: low). |
macd(fast=12, slow=26, signal=9) | MACD line: ema(12) - ema(26). |
macd_hist(12, 26, 9) | MACD histogram (line - signal). |
macd_signal(12, 26, 9) | MACD signal line. |
max(a, b) | Element-wise maximum of two series. |
mfi(n=14) | Money flow index, 0-100. |
min(a, b) | Element-wise minimum of two series. |
obv() | On-balance volume. |
pct_rank([x,] n) | Percentile (0-100) of today's value within the last n bars (n<=1000). |
prev(x, n=1) | Value n bars ago (same as x[n]). |
ret(n) | ret(x, n) | Total return over n bars: close / close[n] - 1. ret(20) > 10%. |
rising([x,] n) | 1 if x is higher than n bars ago. |
roc([x,] n) | Rate of change: x / x[n] - 1. |
rsi([x,] n=14) | Wilder RSI, 0-100. rsi(14), rsi(x, 14). |
rvol(n=20) | Relative volume: volume / sma(volume, n). |
sign(x) | -1, 0 or 1. |
sma([x,] n) | Simple moving average. sma(50) = 50-bar average close; sma(volume, 20). |
sqrt(x) | Square root. |
stdev([x,] n) | Rolling sample standard deviation. |
stoch(n=14) | Stochastic %K, 0-100. |
streak([x]) | Consecutive up (+) or down (-) closes. |
sum([x,] n) | Rolling sum over n bars. |
vol(n=20) | Annualised realised volatility of log returns over n bars (0.25 = 25%). |
vwap(n=20) | Rolling volume-weighted average price over n bars. |
williams_r(n=14) | Williams %R, -100..0. |
wma([x,] n) | Linearly weighted moving average. |
zscore([x,] n) | (x - sma(x,n)) / stdev(x,n). |
Operators
+ - * / | Arithmetic, bar by bar. Division by zero gives null. |
< <= > >= == != | Comparisons, 1 or 0. |
and or not | Logic. Precedence: not, and, or. |
x[n] | Value n bars ago. |
10% | Percent literal, = 0.10. |