Skip to content

Logic and Conditions

Arithmetic, comparisons, logic, and the operators that choose between values or say whether there is one. The rules for missing values are in Missing Values; precedence is in Syntax.

Operators

Operator Description
+, -, *, / Arithmetic, element by element
** Power, with a number as the exponent
>, <, >=, <=, ==, != 1 or 0, missing where either side is missing
&, |, ~ And, or, not, three-valued

Comparisons

A comparison gives 1 or 0, and no value where either side has none: there are no returns on Jan 02.

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
returns(1) > 0
ABCD
Jan 02····
Jan 031011
Jan 0410·0
Jan 050111
Jan 081011
Jan 091001

And, or, not

Three-valued: false & missing is 0, true | missing is 1, and otherwise missing with missing is missing. Below, "up today and high-beta". On Jan 02 there are no returns: B and D have betas under 1, so the answer is 0 without knowing the return; for A and C it depends on the return, so there is no answer.

returns(1) > 0
ABCD
Jan 02····
Jan 031011
Jan 0410·0
Jan 050111
Jan 081011
Jan 091001
field('beta') > 1
ABCD
Jan 021010
Jan 031010
Jan 041010
Jan 051010
Jan 081010
Jan 091010
(returns(1) > 0) & (field('beta') > 1)
ABCD
Jan 02·0·0
Jan 031010
Jan 0410·0
Jan 050010
Jan 081010
Jan 091000

& and | bind tighter than comparisons, so the parentheses are needed.

Choosing values

where

where(cond, if_true, if_false)

if_true where cond is nonzero, if_false where it is 0, and no value where it is missing.

Below, the day's return rank, for high-beta stocks only.

field('beta') > 1
ABCD
Jan 021010
Jan 031010
Jan 041010
Jan 051010
Jan 081010
Jan 091010
where(field('beta') > 1, rank(returns(1)), None)
ABCD
Jan 02····
Jan 031.00·0.50·
Jan 041.00···
Jan 050.25·0.50·
Jan 081.00·0.75·
Jan 091.00·0.50·
where(volatility(20) > 0.6, None, rank(-returns(5)))      # leave out the most volatile stocks

coalesce

coalesce(x, y)

x where it has a value, else y: a number, a label or an expression. It says what missing should mean: below, a day without a report counts as no surprise. Choose with care: a missing return is not a 0% return.

field('surprise')
ABCD
Jan 02····
Jan 030.05···
Jan 04····
Jan 05··−0.02·
Jan 08·−0.01·0.03
Jan 09····
coalesce(field('surprise'), 0)
ABCD
Jan 020.000.000.000.00
Jan 030.050.000.000.00
Jan 040.000.000.000.00
Jan 050.000.00−0.020.00
Jan 080.00−0.010.000.03
Jan 090.000.000.000.00
coalesce(field('price') > 5, 0)              # no price counts as not above $5
coalesce(group('sector'), 'Other')           # a label for stocks without one

None

None

No value here, on purpose: where(cond, x, None). None and 0 are different answers. A 0 is a value, and once a book demeans the signal it is a position against the day's average; None takes the stock out.

Is there a value?

is_valid

is_valid(x)

1 where x has a value, else 0. An infinity counts as a value.

close()
ABCD
Jan 0210.020.030.040.0
Jan 0311.019.030.542.0
Jan 0412.018.0·41.0
Jan 0511.018.531.043.0
Jan 0813.017.032.044.0
Jan 0914.016.031.546.0
is_valid(close())
ABCD
Jan 021111
Jan 031111
Jan 041101
Jan 051111
Jan 081111
Jan 091111
is_valid(field('surprise'))                  # 1 on report days

is_finite

is_finite(x)

1 where x is a finite number, 0 where it is missing or infinite.

where(is_finite(volume(20) / volume(1)), volume(20) / volume(1), None)