Expression Rules and Syntax
1. Use Cases
Expressions are BeeQuant's core language for describing factor calculations, trading conditions, and label definitions. They are primarily used in the following places:
| Location | Purpose | Example | Supported features |
|---|---|---|---|
| Data-processing node | Factor expression | rsi = ta_rsi(close, 14) | Operator functions + operators |
| Label expression | label = shift(close, -5) / shift(open, -1) - 1 | Operator functions + operators | |
| Filter expression | volume > 1000000 | Comparison + logical operations | |
| Strategy-type node | Timing entry/exit expression | (rsi > 70) & (ma5 > ma20) | Comparisons + logical operations only |
| Asset-selection filter expression | volume > 1000000 | Comparisons + logical operations only |
⚠️ Note: Condition expressions in strategy-type nodes—timing entries/exits and asset-selection filters—cannot call time-series operator functions such as ta_ma, shift, or ta_cross_over. Compute every factor in the data-processing node first, then use only the emitted column names in comparisons and logical operations.
2. Basic Syntax
An expression consists of column names, constants, operators, and function calls.
2.1 Built-in Columns
The data-processing and extraction node retrieves the following columns from the exchange. You can reference them directly in its factor, label, and filter expressions:
| Column | Description | Example |
|---|---|---|
open | Opening price | (close - open) / open |
high | Highest price | high - low |
low | Lowest price | close - low |
close | Closing price (most commonly used) | ta_ma(close, 20) |
⚠️ Reference ≠ output: open/high/low/close/volume and fundamental fields may participate in every expression field of the data-processing and extraction node (factor / label / filter), but they are not forwarded downstream by default. Its output contains only timestamp, symbol, and assigned variables whose names do not begin with _. A strategy condition can reference only columns actually emitted upstream (factor, pass-through, or AI prediction columns). To use close in a strategy condition, explicitly pass it through in a factor expression, for example close = close.
2.2 Constants
| Type | Description | Examples |
|---|---|---|
| Integer | Used for window lengths, period parameters, and similar values | 14, 20, 60 |
| Floating-point number | Used for thresholds, ratios, and similar values | 0.05, 2.0, -0.5 |
| Boolean | True/false | True / 1, False / 0 |
2.3 Variable Definitions
In factor configuration, define variables as variable_name = expression. Put each variable on a separate line.
_ma20 = ta_ma(close, 20)// Intermediate variable; not output
diff = _ma20 - close// Output variable (difference between the moving average and closing price)
rsi = ta_rsi(close, 14)// Output variable
_ma20 is an intermediate variable and is omitted; diff and rsi are output):| timestamp | symbol | close | diff | rsi |
|---|---|---|---|---|
| 2024-12-11 00:00:00 | BTCUSDT | 97800.0 | -679.5 | 58.32 |
| 2024-12-11 01:00:00 | BTCUSDT | 97750.0 | -593.2 | 55.18 |
| 2024-12-11 02:00:00 | BTCUSDT | 97820.0 | -616.8 | 59.45 |
| 2024-12-11 03:00:00 | BTCUSDT | 98050.0 | -781.5 | 63.21 |
● Gold columns are newly added output columns. _ma20 is omitted because its name begins with _.
_ma20 = ta_ma(close, 20)// Intermediate variable; not output
_std = t_std(close, 20)// Intermediate variable; not output
_upper = _ma20 + 2 * _std// Intermediate variable; not output
_lower = _ma20 - 2 * _std// Intermediate variable; not output
bb_pos = (close - _lower) / (_upper - _lower)// Final factor; output
3. Operators
3.1 Arithmetic Operators
| Symbol | Name | Example | Description |
|---|---|---|---|
| + | Addition | close + open | Adds two values |
| - | Subtraction | high - low | Subtracts one value from another |
| * | Multiplication | close * volume | Multiplies two values |
| / | Division | (close - open) / open | Divides one value by another |
| ** | Exponentiation | returns ** 2 | Raises x to the power y |
| % | Modulo | index % 5 | Returns the remainder after division |
3.2 Comparison Operators
Comparison operators return Boolean values and are used primarily in strategy-node condition expressions.
| Symbol | Name | Example | Description |
|---|---|---|---|
| > | Greater than | rsi > 70 | RSI is overbought |
| >= | Greater than or equal to | close >= ma20 | Price is at or above the moving average |
| < | Less than | rsi < 30 | RSI is oversold |
| <= | Less than or equal to | close <= ma20 | Price is at or below the moving average |
| == | Equal to | signal == 1 | Signal equals 1 |
| != | Not equal to | trend != 0 | Trend is not 0 |
3.3 Logical Operators
Logical operators combine multiple conditions.
| Symbol | Name | Example | Description |
|---|---|---|---|
| & | AND | (rsi < 30) & (close > ma20) | Both conditions must be true |
| | | OR | (rsi > 70) | (rsi < 30) | Either condition may be true |
| ~ | NOT | ~(close > open) | Negates the condition |
⚠️ Note: When using & or |, wrap each condition in parentheses to avoid operator-precedence issues.
1. ** Exponentiation
2. ~ Logical NOT
3. * / % Multiplication, division, modulo
4. + - Addition and subtraction
5. > >= < <= Comparisons
6. == != Equality tests
7. & Logical AND
8. | Logical OR
💡 Use parentheses to make the order of operations explicit and improve readability.
4. Function Calls
Function-call syntax is function_name(argument1, argument2, ...). Calls may be nested. See Expression Operators for the complete function list.
ta_ma, ta_ema, ta_wma, t_meanta_rsi, ta_macd, ta_kdj, ta_ccita_atr, ta_bbands, t_stdshift, pct, log, diffif, max, min, absc_rank, c_pctrank, c_zscoreta_ma(close, 20)// 20-period moving average
t_mean(ta_rsi(close, 14), 20)// 20-period average of RSI
if(close > open, 1, -1)// Returns 1 for a bullish candle and -1 for a bearish candle
⚠️ Note: Time-series operator functions such as ta_ma, shift, and ta_cross_over may be used only in factor and label expressions in the data-processing node. Strategy condition expressions cannot call time-series functions; they may only compare and combine precomputed columns.
5. Factor Expression Examples
The following expressions are commonly used in the factor configuration of a data-processing node:
| Factor | Expression | Description |
|---|---|---|
| Moving average | ma20 = ta_ma(close, 20) | 20-period simple moving average |
| RSI | rsi = ta_rsi(close, 14) | 14-period Relative Strength Index |
| Return | ret = pct(close, 1) | Single-period return |
| Volatility | vol = t_std(pct(close, 1), 20) | 20-period standard deviation of returns |
| Volume ratio | vol_ratio = volume / t_mean(volume, 20) | Volume divided by 20-period average volume |
| Momentum | mom = close / shift(close, 20) - 1 | 20-period price rate of change |
| Bollinger position | bb_pos = (close - ta_bbands_lower(close, 20, 2)) / (ta_bbands_upper(close, 20, 2) - ta_bbands_lower(close, 20, 2)) | Price position within the Bollinger Bands (0–1) |
6. Condition Expression Examples
The following expressions are commonly used as entry and exit conditions in strategy-type nodes. A condition expression may directly reference factor variables defined by the upstream data-processing node.
ta_ma, shift, or ta_cross_over. Compute every factor in the data-processing node first. Strategy conditions support only comparisons and logical operations on column names.| Condition | Strategy condition expression | Prerequisite in data processing |
|---|---|---|
| RSI overbought | rsi > 70 | rsi = ta_rsi(close, 14) |
| RSI oversold | rsi < 30 | rsi = ta_rsi(close, 14) |
| Golden cross signal | golden == True | golden = ta_cross_over(ma5, ma20) |
| Death cross signal | death == True | death = ta_cross_under(ma5, ma20) |
| Break above upper band | close > bb_upper | bb_upper = ta_bbands_upper(close, 20, 2) |
| Price and volume breakout | (close > open) & (vol_ratio > 1.5) | vol_ratio = volume / t_mean(volume, 20) |
| Multiple conditions | (rsi < 30) & (close > ma20) & (vol_ratio > 1) | rsi / ma20 / vol_ratio are all precomputed |
| AI prediction signal | pred > 0.6 | pred is emitted by the AI training node |
golden or death may be referenced directly (golden) or compared explicitly (golden == True). Both forms have the same effect.7. Label Expression Examples
The following expressions are commonly used for label definitions in a data-processing node. A label defines an AI model's prediction target and usually uses shift() to access future data.
| Label | Expression | Task type |
|---|---|---|
| Future return | label = shift(close, -5) / shift(open, -1) - 1 | Regression / binary classification |
| Future maximum gain | label = shift(t_max(high, 5), -5) / shift(open, -1) - 1 | Regression |
| Cross-sectional ranking | label = shift(close, -6) / shift(open, -1) - 1 | Ranking (converted by the engine) |
shift(col, n) shifts data backward when n > 0 or forward when n < 0. For example, shift(close, -5) means “the closing price five periods later.”