Universes from Python¶
What a universe is, and how to write one as an expression, is in Universes. From Python,
everywhere qex takes a universe (cross_section_universe, BookSettings(universe=...),
explain_missing(universe=...)) it takes any of three forms:
| Form | Example | Member when |
|---|---|---|
| a mask | data['member'].notna() |
True |
| a data field's name | 'member' |
the field has a value (1 in, NaN out) |
| a universe expression | "field('mcap') >= 300e6", or a parsed qex(...) |
the expression is nonzero |
Text that names a field of the data is that field ('member' stays the field it always was); anything else is
parsed as qex.
from qex import qex, cross_section_universe, universe_mask
from evaluation import BookSettings, build_book
LARGE = "(field('price') >= 2) & (field('price') * field('shares') >= 2e9)"
with cross_section_universe(LARGE, data): # an expression needs the data
r = qex("rank(returns(5))").evaluate(data) # ranked among the large stocks only
book = build_book(qex("rank(-returns(5))"), data, BookSettings(delay=1, universe=LARGE))
members = universe_mask(LARGE, data) # the True/False frame itself
Inside one compute_context, a universe expression's membership is computed once and shared by every signal
evaluated in it. BookSettings(mask_cross_section=False) applies the universe only to the finished signal, as qex did
before 0.5.
cross_section_universe ¶
Restrict cross-sectional operators to the members of a universe, day by day.
mask is a date x ticker DataFrame of booleans, a universe field (1 in, NaN out), or, with data, a field's name or a universe expression (see the module docstring). Dates or tickers it does not cover are outside the universe. None leaves operators unrestricted.
universe_mask ¶
The members, date x ticker True/False, of a universe given as None, a mask, a data field's name, or a universe expression (qex text or a Signal) evaluated on data. Text that names a field of data is that field.