Skip to content

Arithmetic Operators

Mathematical operations for signal computation.

Binary Operators

Addition (+)

Text Only
result = a + b

Element-wise addition.

Text Only
signal example:
  adjusted = prices + 1
  combined = signal_a + signal_b
  emit combined

Subtraction (-)

Text Only
result = a - b

Element-wise subtraction.

Text Only
signal example:
  change = prices - lag(prices, 1)
  spread = signal_a - signal_b
  emit spread

Multiplication (*)

Text Only
result = a * b

Element-wise multiplication.

Text Only
signal example:
  scaled = returns * 252        // Annualize
  weighted = signal * weight
  emit scaled

Division (/)

Text Only
result = a / b

Element-wise division.

Text Only
signal example:
  ratio = prices / lag(prices, 20)
  vol_adj = returns / volatility
  emit vol_adj

Unary Operators

Negation (-)

Text Only
result = -x

Negate values.

Text Only
signal example:
  inverted = -returns
  contrarian = -zscore(returns)  // Buy losers, sell winners
  emit contrarian

Functions

abs(x)

Absolute value.

Text Only
abs(x: Numeric) -> Numeric
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
sign(x: Numeric) -> Numeric
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
log(x: Numeric) -> Numeric
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).

Text Only
exp(x: Numeric) -> Numeric
Text Only
signal example:
  growth = exp(log_return)
  cumulative = exp(cumsum(log_returns))
  emit growth

pow(x, n)

Raise to power.

Text Only
pow(x: Numeric, n: Numeric) -> Numeric
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
sqrt(x: Numeric) -> Numeric
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.

Text Only
floor(x: Numeric) -> Numeric
Text Only
signal example:
  buckets = floor(score * 10)  // 0-9 buckets
  emit buckets

ceil(x)

Round up to nearest integer.

Text Only
ceil(x: Numeric) -> Numeric
Text Only
signal example:
  min_position = ceil(score)
  emit min_position

round(x)

Round to nearest integer.

Text Only
round(x: Numeric) -> Numeric
Text Only
signal example:
  discrete = round(score)
  emit discrete

min(a, b)

Element-wise minimum.

Text Only
min(a: Numeric, b: Numeric) -> Numeric
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
max(a: Numeric, b: Numeric) -> Numeric
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
clip(x: Numeric, lo: Numeric, hi: Numeric) -> Numeric
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:

Text Only
clip(x, lo, hi) = max(min(x, hi), lo)

Common Patterns

Log Returns

Text Only
signal log_returns:
  log_ret = log(prices / lag(prices, 1))
  emit log_ret

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

Text Only
a + b        // Adds corresponding elements
a * b        // Multiplies corresponding elements

Broadcasting

Scalars broadcast to match array dimensions:

Text Only
prices + 1           // 1 broadcasts to all elements
returns * 252        // 252 broadcasts

Precedence

From highest to lowest:

  1. Function calls: abs(x), sqrt(x)
  2. Unary negation: -x
  3. Multiplication/Division: *, /
  4. Addition/Subtraction: +, -

Use parentheses to clarify:

Text Only
a + b * c      // = a + (b * c)
(a + b) * c    // = (a + b) * c

Next Steps