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:

LocationPurposeExampleSupported features
Data-processing nodeFactor expressionrsi = ta_rsi(close, 14)Operator functions + operators
Label expressionlabel = shift(close, -5) / shift(open, -1) - 1Operator functions + operators
Filter expressionvolume > 1000000Comparison + logical operations
Strategy-type nodeTiming entry/exit expression(rsi > 70) & (ma5 > ma20)Comparisons + logical operations only
Asset-selection filter expressionvolume > 1000000Comparisons + 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:

ColumnDescriptionExample
openOpening price(close - open) / open
highHighest pricehigh - low
lowLowest priceclose - low
closeClosing 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

TypeDescriptionExamples
IntegerUsed for window lengths, period parameters, and similar values14, 20, 60
Floating-point numberUsed for thresholds, ratios, and similar values0.05, 2.0, -0.5
BooleanTrue/falseTrue / 1, False / 0

2.3 Variable Definitions

In factor configuration, define variables as variable_name = expression. Put each variable on a separate line.

Syntax
# Format: variable_name = expression
_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
Example output from the configuration above (_ma20 is an intermediate variable and is omitted; diff and rsi are output):
timestampsymbolclosediffrsi
2024-12-11 00:00:00BTCUSDT97800.0-679.558.32
2024-12-11 01:00:00BTCUSDT97750.0-593.255.18
2024-12-11 02:00:00BTCUSDT97820.0-616.859.45
2024-12-11 03:00:00BTCUSDT98050.0-781.563.21

Gold columns are newly added output columns. _ma20 is omitted because its name begins with _.

💡 When to use intermediate variables:They make complex factor calculations easier to read.
# Example: calculate the position within Bollinger Bands
_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

SymbolNameExampleDescription
+Additionclose + openAdds two values
-Subtractionhigh - lowSubtracts one value from another
*Multiplicationclose * volumeMultiplies two values
/Division(close - open) / openDivides one value by another
**Exponentiationreturns ** 2Raises x to the power y
%Moduloindex % 5Returns the remainder after division

3.2 Comparison Operators

Comparison operators return Boolean values and are used primarily in strategy-node condition expressions.

SymbolNameExampleDescription
>Greater thanrsi > 70RSI is overbought
>=Greater than or equal toclose >= ma20Price is at or above the moving average
<Less thanrsi < 30RSI is oversold
<=Less than or equal toclose <= ma20Price is at or below the moving average
==Equal tosignal == 1Signal equals 1
!=Not equal totrend != 0Trend is not 0

3.3 Logical Operators

Logical operators combine multiple conditions.

SymbolNameExampleDescription
&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.

Operator precedence (highest to lowest)

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.

Common function categories
Moving averages
ta_ma, ta_ema, ta_wma, t_mean
Momentum indicators
ta_rsi, ta_macd, ta_kdj, ta_cci
Volatility
ta_atr, ta_bbands, t_std
Time shifts
shift, pct, log, diff
Conditional functions
if, max, min, abs
Cross-sectional functions
c_rank, c_pctrank, c_zscore
Function-call examples
# Simple call
ta_ma(close, 20)// 20-period moving average
# Nested call
t_mean(ta_rsi(close, 14), 20)// 20-period average of RSI
# Conditional function
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:

FactorExpressionDescription
Moving averagema20 = ta_ma(close, 20)20-period simple moving average
RSIrsi = ta_rsi(close, 14)14-period Relative Strength Index
Returnret = pct(close, 1)Single-period return
Volatilityvol = t_std(pct(close, 1), 20)20-period standard deviation of returns
Volume ratiovol_ratio = volume / t_mean(volume, 20)Volume divided by 20-period average volume
Momentummom = close / shift(close, 20) - 120-period price rate of change
Bollinger positionbb_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.

🚫 Important: A strategy condition cannot call operator functions such as 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.
ConditionStrategy condition expressionPrerequisite in data processing
RSI overboughtrsi > 70rsi = ta_rsi(close, 14)
RSI oversoldrsi < 30rsi = ta_rsi(close, 14)
Golden cross signalgolden == Truegolden = ta_cross_over(ma5, ma20)
Death cross signaldeath == Truedeath = ta_cross_under(ma5, ma20)
Break above upper bandclose > bb_upperbb_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 signalpred > 0.6pred is emitted by the AI training node
💡 Tip: A Boolean factor column such as 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.

LabelExpressionTask type
Future returnlabel = shift(close, -5) / shift(open, -1) - 1Regression / binary classification
Future maximum gainlabel = shift(t_max(high, 5), -5) / shift(open, -1) - 1Regression
Cross-sectional rankinglabel = shift(close, -6) / shift(open, -1) - 1Ranking (converted by the engine)
💡 About shift():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.”
    BeeQuant - AI Quantitative Trading Platform