cointoss.universe¶
universe
¶
Universe Definitions and the pure evaluation that turns one into membership.
Implements ADR-0007. A Universe Definition is the recipe; the membership it produces is a separate, dated record. The recipe says "the top hundred coins, minus the wrapped ones"; the membership says who that was on a Tuesday. Keeping them apart is what lets "I changed my mind about the band" stay distinguishable from "the market moved".
A Definition is a named, editable recipe, and editing appends a revision rather than
overwriting, exactly as a RiskModel does -- the two modules are deliberate analogues and
should read alike. A crypto universe is a market-cap rank band with Inclusions and Exclusions
layered over it as standing policy rather than as one-off corrections that the next evaluation
would undo. A stock universe is a Listing Rule: whatever a source's own published lists name on
the day of the run (ADR-0007 amendment of 2026-11-28). portfolio-held has no rule at all and
is nothing but its Inclusions.
evaluate is a pure function of the parameters, a ranked list or the listed Instruments, and
the previous membership. It makes no source call, reads no database and knows no date, so an
evaluation for any past day is ordinary rather than special. The band is expressed as two ranks,
an entry and a wider exit, so a coin sitting on the boundary does not join and leave every day;
rank rather than an absolute market cap, so a broad drawdown does not empty the universe and
record a mass exodus that is really one macro event.
Pure: no network, no clock, no database. Edit dates and ranked input are supplied by the caller.
UniverseError
¶
UnknownRevision
¶
Bases: UniverseError
A revision number is not in the owning Definition's log.
RaggedRule
¶
Bases: UniverseError
A rule is missing part of itself, contradicts itself, or a recipe states two rules.
RuleKind
¶
Listing
¶
Bases: StrEnum
A list a source publishes of the companies it names, which a Listing Rule admits.
Yahoo publishes, for each of its industries, up to 50 top companies and 5 fastest-growing ones; the two lists of every industry are one Listing each.
Source code in src/cointoss/universe.py
MemberRole
¶
RunOutcome
¶
Bases: StrEnum
Whether an Evaluation Run wrote a Universe Series entry.
The first run of a Definition always does, even when its membership is empty, because it is what turns "not yet started" into "nothing qualified".
Source code in src/cointoss/universe.py
EvaluationRun
¶
Bases: BaseModel
One execution of a Universe Definition: evidence the job ran, not a record of truth.
source_asof is the date the membership is recorded for and run_at is when the job
executed, kept apart so a late or backfilled run is not mistaken for a stale market. The
counts are against the membership in force before the run; n_unresolved counts the
members standing at revision that have no Instrument and so could not take part.
Source code in src/cointoss/universe.py
UniverseParameters
¶
Bases: BaseModel
A Universe Definition's recipe at one revision.
The rule is optional in whole, and is a rank band or a Listing Rule, never both:
portfolio-held has none and is nothing but its Inclusions. When a rank band is present both
ranks are required, because one rank alone is not a band and silently defaulting the other
would invent a policy nobody wrote. A Listing Rule is the non-empty set of listings it
admits from.
Ranks are 1-based, matching how a ranked list is read aloud: the largest coin is rank 1, and
enter_rank=100 means "the top hundred".
Source code in src/cointoss/universe.py
rule_kind
property
¶
Which rule drives membership, None when it is Inclusions alone.
has_rule
property
¶
Whether membership is driven by a rule at all, or by Inclusions alone.
pinned(role, instrument_id)
¶
These parameters with instrument_id added to the set role names.
An unresolved member (None) leaves the sets unchanged, since only resolved members are projected into them.
Source code in src/cointoss/universe.py
UniverseMemberRecord
¶
Bases: BaseModel
The provenance of one hand-pinned member, in the words it was typed in.
A member is resolved to an Instrument once, when it is added, so a ticker later reassigned to
an unrelated issuer cannot silently change what the list means. instrument_id is None while
resolution has not succeeded, which is how "not yet identified" stays distinguishable from
"not in the universe" -- an unresolved member never reaches UniverseParameters and so can
never reach evaluate.
Removal sets removed_in_revision rather than deleting the record, so removing and re-adding
one ticker stays two recorded events instead of flattening into "it is there now".
Source code in src/cointoss/universe.py
covers(revision)
¶
Whether this member was standing at revision.
UniverseDefinitionRevision
¶
Bases: BaseModel
One recorded edit to a Universe Definition.
Holds the whole parameter set in force after the edit, not a diff, for the same reason
Series entries do: a diff makes every read a fold, and one bad early diff poisons the rest.
changed names the fields the edit touched, so the log still reads as a history of edits.
Source code in src/cointoss/universe.py
UniverseDefinition
¶
Bases: BaseModel
A named, editable recipe for producing membership.
Editing keeps the Definition's identity and appends to its log rather than minting a new
one, so there is no versioning lifecycle to operate. The value itself is immutable: an edit
returns a new UniverseDefinition carrying the longer log, exactly as a Series append does.
>>> d = UniverseDefinition.create(
... "top-100", UniverseParameters(enter_rank=100, exit_rank=120), date(2024, 1, 1)
... )
>>> d.revision
1
>>> d = d.edited(date(2024, 6, 1), enter_rank=50, exit_rank=75)
>>> d.revision, d.parameters_at(1).enter_rank, d.parameters.enter_rank
(2, 100, 50)
Source code in src/cointoss/universe.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
revision
property
¶
The latest revision number.
parameters
property
¶
The recipe in force now.
create(name, parameters, at)
classmethod
¶
A Definition at revision 1, whose every populated field counts as changed.
Derived from the parameters themselves rather than from model_fields_set, which
records how an object was built: a Definition read back from storage would otherwise log
every unset field as having changed. An empty Inclusion set is not a change, which is
what makes "somewhere to put a list before I know what goes in it" a revision about
nothing.
Source code in src/cointoss/universe.py
parameters_at(revision)
¶
The recipe in force at a past revision, so an old Series entry stays interpretable.
Source code in src/cointoss/universe.py
edited(at, **changes)
¶
A Definition with edited parameters and one more revision.
An edit that changes nothing is a no-op: no bump and no log entry, so the log records real changes rather than save-button noise.
Source code in src/cointoss/universe.py
resolved_members(records, role, revision)
¶
The Instrument Ids a set of member records projects to at one revision.
This is the projection that fills UniverseParameters: standing members of the given role
that resolved to an Instrument. Unresolved members are dropped here rather than downstream,
which is what keeps a null Instrument out of every evaluated membership.
>>> records = [
... UniverseMemberRecord(
... role=MemberRole.INCLUSION, source="yahoo", symbol_as_typed="AAPL",
... instrument_id="stock.us.aapl",
... ),
... UniverseMemberRecord(
... role=MemberRole.INCLUSION, source="yahoo", symbol_as_typed="NOSUCH",
... ),
... ]
>>> sorted(resolved_members(records, MemberRole.INCLUSION, 1))
[InstrumentId('stock.us.aapl')]
Source code in src/cointoss/universe.py
evaluate(parameters, ranked=(), previous=(), *, listed=())
¶
The membership a recipe produces from the run's source data and the membership before it.
A rank band reads ranked and previous; a Listing Rule reads listed, each Instrument
with a list that named it, and admits an Instrument named by any of the lists the rule
names. A Listing Rule has no hysteresis, so previous plays no part in it: an Instrument
absent from every list is out at this run (ADR-0007 amendment of 2026-11-28). Each rule
ignores the other's input.
ranked is ordered by descending market cap, so position 1 is the largest; where an id
appears twice its best rank is the one that counts. A mapping gives each id its rank outright,
for a source whose ranks are not its list positions: an id skipped or held back upstream then
leaves a gap rather than promoting everything below it. previous is the membership from the
last Series entry, which is what makes the band hysteretic: an id already in the universe
survives while its rank is better than exit_rank, and an id not in it is admitted only at
enter_rank or better. An id absent from ranked entirely is out of the rule's reach
regardless of what it was before -- a coin no source ranks has no rank to be inside a band.
Composition for either rule is (rule output | inclusions) - exclusions, exclusions applied
last, so an instrument both included and excluded resolves to excluded and the answer does
not depend on an ordering accident. A Definition with no rule is its Inclusions alone, which
is portfolio-held. An empty result is a result: nothing here raises on one.
>>> btc, eth, sol = (InstrumentId(f"crypto.native.{s}") for s in ("btc", "eth", "sol"))
>>> band = UniverseParameters(enter_rank=1, exit_rank=2)
>>> sorted(evaluate(band, [btc, eth, sol]))
[InstrumentId('crypto.native.btc')]
>>> sorted(evaluate(band, [btc, eth, sol], previous={eth}))
[InstrumentId('crypto.native.btc'), InstrumentId('crypto.native.eth')]
>>> evaluate(band, [btc, sol], previous={eth})
frozenset({InstrumentId('crypto.native.btc')})
>>> evaluate(band, {eth: 2, sol: 3})
frozenset()
>>> top = UniverseParameters(listings=frozenset({Listing.YAHOO_INDUSTRY_TOP}))
>>> evaluate(top, listed=[(eth, Listing.YAHOO_INDUSTRY_TOP)], previous={btc})
frozenset({InstrumentId('crypto.native.eth')})