Arithmetic Operators¶
Mathematical operations for signal computation.
Binary Operators¶
Addition (+)¶
Element-wise addition.
Subtraction (-)¶
Element-wise subtraction.
Multiplication (*)¶
Element-wise multiplication.
Division (/)¶
Element-wise division.
Text Only
signal example:
ratio = prices / lag(prices, 20)
vol_adj = returns / volatility
emit vol_adj
Unary Operators¶
Negation (-)¶
Negate values.
Text Only
signal example:
inverted = -returns
contrarian = -zscore(returns) // Buy losers, sell winners
emit contrarian
Functions¶
abs(x)¶
Absolute value.
Text Only
signal example:
absolute_return = abs(returns)
distance = abs(prices - moving_avg)
emit distance
sign(x)¶
Sign of value (-1, 0, or 1).
Text Only
signal example:
direction = sign(returns)
// direction is -1 for negative, 0 for zero, 1 for positive
emit direction
log(x)¶
Natural logarithm.
Text Only
signal example:
log_prices = log(prices)
log_return = log(prices / lag(prices, 1))
log_market_cap = log(market_cap)
emit log_return
Warning
log(x) is undefined for x <= 0. Ensure positive values.
exp(x)¶
Exponential function (e^x).
pow(x, n)¶
Raise to power.
Text Only
signal example:
squared = pow(returns, 2)
variance = rolling_mean(squared, 20)
cubed = pow(x, 3)
emit variance
sqrt(x)¶
Square root.
Text Only
signal example:
std_dev = sqrt(variance)
annualized_vol = sqrt(252) * daily_vol
emit std_dev
Warning
sqrt(x) is undefined for x < 0. Ensure non-negative values.
floor(x)¶
Round down to nearest integer.
ceil(x)¶
Round up to nearest integer.
round(x)¶
Round to nearest integer.
min(a, b)¶
Element-wise minimum.
Text Only
signal example:
capped = min(score, 2) // Cap at 2
lower = min(signal_a, signal_b)
emit capped
max(a, b)¶
Element-wise maximum.
Text Only
signal example:
floored = max(score, -2) // Floor at -2
higher = max(signal_a, signal_b)
emit floored
clip(x, lo, hi)¶
Clamp values to range [lo, hi].
Text Only
signal example:
bounded = clip(score, -3, 3) // Clip to [-3, 3]
weights = clip(raw_weights, -0.1, 0.1) // Max 10% position
emit bounded
Equivalent to:
Common Patterns¶
Log Returns¶
Volatility Scaling¶
Text Only
signal vol_scaled:
target_vol = 0.15 // 15% annualized
current_vol = sqrt(252) * rolling_std(ret(prices, 1), 20)
scale_factor = target_vol / current_vol
emit returns * scale_factor
Distance from Mean¶
Text Only
signal distance:
ma = rolling_mean(prices, 20)
dist = abs(prices - ma)
normalized_dist = dist / ma
emit normalized_dist
Bounded Scores¶
Text Only
signal bounded:
raw = zscore(returns)
bounded = clip(raw, -3, 3) // Clip extremes
emit bounded
Annualization¶
Text Only
signal annualized:
daily_ret = ret(prices, 1)
annual_ret = daily_ret * 252
daily_vol = rolling_std(daily_ret, 20)
annual_vol = daily_vol * sqrt(252)
sharpe = annual_ret / annual_vol
emit sharpe
Type Behavior¶
Scalar Operations¶
Text Only
x + 1 // Adds 1 to every element
x * 2 // Multiplies every element by 2
x / 100 // Divides every element by 100
Element-wise Operations¶
Broadcasting¶
Scalars broadcast to match array dimensions:
Precedence¶
From highest to lowest:
- Function calls:
abs(x),sqrt(x) - Unary negation:
-x - Multiplication/Division:
*,/ - Addition/Subtraction:
+,-
Use parentheses to clarify: