Skip to content

Math

Element by element: each date and stock on its own. Any argument can be a number, so sqrt(5), max(x, 0) and power(2, x) all work.

returns(1)
ABCD
Jan 02····
Jan 030.100−0.0500.0170.050
Jan 040.091−0.053·−0.024
Jan 05−0.0830.0280.0160.049
Jan 080.182−0.0810.0320.023
Jan 090.077−0.059−0.0160.045
abs(returns(1))
ABCD
Jan 02····
Jan 030.1000.0500.0170.050
Jan 040.0910.053·0.024
Jan 050.0830.0280.0160.049
Jan 080.1820.0810.0320.023
Jan 090.0770.0590.0160.045

log

log(x)

The natural logarithm. No value for x < 0, and −∞ at 0.

log(1 + returns(20))                        # a 20-day log return
log(field('mcap'))                          # size, on a log scale

exp

exp(x)

e to the x, the inverse of log; ∞ when too large.

exp(ts_sum(log(1 + returns(1)), 20)) - 1    # daily returns compounded over 20 days
1 / (1 + exp(-zscore(returns(5))))          # a sigmoid, from 0 to 1
scale(exp(zscore(returns(5)) / 0.5))        # a cross-sectional softmax, temperature 0.5

abs

abs(x)

The absolute value.

ts_mean(abs(returns(1)), 20)                # average daily move, either way

sign

sign(x)

−1, 0 or 1.

sign(returns(20)) * sqrt(abs(returns(20)))  # keeps the direction, damps the outliers

sqrt

sqrt(x)

The square root. No value for x < 0.

power

power(x, n)

x to the power n; n can be a number or an expression. x ** 2 is the same with a number as the exponent.

power(volatility(63), 2)                    # variance, annualized

max

max(x, y)

The larger of x and y, element by element. Not the largest across stocks or over time: for those, use ts_max or rank.

ts_mean(max(returns(1), 0), 14) / ts_mean(abs(returns(1)), 14)    # the up share of recent moves, as in RSI

min

min(x, y)

The smaller of x and y, element by element.

sqrt(ts_mean(power(min(returns(1), 0), 2), 60) * 252)              # downside deviation, annualized
min(max(returns(1), -0.1), 0.1)                                      # clip at ±10%