cointoss.ingest¶
ingest
¶
Ingest: where Observations are constructed, and so where ADR-0004's FIGI attempt is enforced.
cointoss.instrument cannot compel a FIGI attempt -- it is pure and takes the outcome as given
-- so the invariant lives here, at the only place an Observation is built from outside data.
It holds two paths in. Pinning a typed ticker (issue #14): a researcher types a symbol against
a source, and it is resolved to an Instrument once, at edit time, per ADR-0007. And the daily
market sweep, sweep, which turns one CoinGecko markets listing into Instruments,
membership and bars. figi_attempts, pin_with_attempt and anchor_with_attempt split
pinning and retrying around one batched FIGI call, for the stock sweep (cointoss.stocks),
which also runs the unresolved retry for stocks. hold puts a traded
Instrument into portfolio-held, whose Inclusions are priced from their own Price Sources: a
coin by the sweep, even outside its listing, and a stock by cointoss.stock_bars (#41).
It sits apart from its neighbours because it is the one layer that combines them: it makes a
network call, which cointoss.instrument and cointoss.universe must not; it applies identity
rules, which cointoss.store must not; and it writes through the store, which the source
adapters must not.
The resolution policy, in order, following ADR-0004:
- A FIGI hit for a symbol that exactly one unresolved Instrument held, at that source on that
date, is that Instrument's retry: the references are attached to it through
InstrumentRegistry.anchorrather than minting a numbered second one. - Any other FIGI hit is put through the registry, which matches by External Reference or mints. The result is persisted and the member pinned to it.
- A FIGI answer naming several distinct anchors is flagged for review. Picking one would guess, and minting from the symbol would ignore evidence that the ticker is contested.
- Otherwise -- no match, an OpenFIGI outage, or a scope with no FIGI exchange to ask -- the ticker is first looked up in the Ticker History already held, as that source spelled it on that date, and pinned if exactly one Instrument held it.
- Failing that the Observation goes through the registry's symbol-keyed last resort, which
mints an Instrument flagged unresolved (or joins one already minted that way), and the member
is pinned to it. An outage is recorded as
NOT_ATTEMPTEDand a miss asNOT_FOUND; both put the Instrument in the registry's unresolved queue, whichretry_resolutionworks one Instrument at a time. - Anything the registry flags for review, or a symbol two Instruments hold at once, leaves the member stored unresolved, and the edit still succeeds.
A symbol typed against CoinGecko that reaches the registry also carries the coin's CoinGecko
id as a provider External Reference, the one the sweep identifies coins by. Without it a coin
pinned before its first sweep could not be matched by that sweep -- a FIGI-anchored Instrument
is out of reach of the symbol tier -- and crypto.native.btc_2 would be minted beside it. See
_coingecko_coin for how a symbol several coins share is decided.
FigiAttempt = tuple[FigiResolution, tuple[ExternalReference, ...]] | IdentityResult
module-attribute
¶
The outcome of one FIGI attempt and the References it supplies, or a flagged result.
DEFAULT_TOP_N = 250
module-attribute
¶
How many coins a sweep lists by default: one full markets page.
DEFAULT_BAR_WINDOW = timedelta(hours=3)
module-attribute
¶
How long after 00:00 UTC a listing's price still stands for that date's 00:00 price.
SweepTooNarrow
¶
SweepReport
¶
Bases: BaseModel
What one sweep did, for a caller to log or report.
skipped maps a CoinGecko coin id to why no Instrument was named for it; failed maps a
Definition name to why its membership was not recorded. Neither aborts the sweep. runs
are the Evaluation Runs in the order the Definitions were named, including one already
recorded by an earlier sweep the same day.
listed counts the coins of the markets listing, and unlisted the coins fetched by id
beside it because a Definition includes them. bars counts the bars written. bars_kept
counts coins whose bar for the day was already stored and was left as it was.
bars_skipped_late means the sweep ran after the bar window, so it wrote no bars at all.
Source code in src/cointoss/ingest.py
day
property
¶
The UTC date the sweep recorded membership and bars for.
figi_job(source, symbol, scope=None)
¶
The OpenFIGI mapping request for a symbol typed against a source, if one can be formed.
Yahoo spells a share class with a hyphen where OpenFIGI uses a slash.
>>> figi_job(Source.YAHOO, "brk-b").model_dump(exclude_none=True)
{'idType': 'TICKER', 'idValue': 'BRK/B', 'exchCode': 'US'}
>>> figi_job(Source.COINGECKO, "btc").model_dump(exclude_none=True)
{'idType': 'TICKER', 'idValue': 'BTC', 'marketSecDes': 'Curncy'}
>>> figi_job(Source.YAHOO, "7203", scope="jp") is None
True
Source code in src/cointoss/ingest.py
figi_request(instrument)
¶
The FIGI attempt a retry of instrument makes, from its type, scope and display symbol.
Source code in src/cointoss/ingest.py
figi_attempts(figi, requests)
async
¶
The FIGI outcome of each request, in order, from one batched mapping call.
A request with no job -- a scope with no FIGI exchange to ask -- is NOT_ATTEMPTED, and an
answer naming several anchors is flagged for review.
Source code in src/cointoss/ingest.py
retry_resolution(store, instrument_id, at, *, figi=None)
async
¶
Retry the FIGI attempt for one stored Instrument, anchoring it on a hit.
The unit of work for the unresolved retry runner. The request is formed from the
Instrument's type, scope and display symbol, and a hit is attached through
InstrumentRegistry.anchor, which flags contrary evidence rather than acting on it. Returns
None when OpenFIGI still has no single answer to give -- a miss, an outage, or a scope with
no FIGI exchange -- and nothing is written. An unknown id raises KeyError.
Source code in src/cointoss/ingest.py
anchor_with_attempt(store, instrument_id, at, attempt, *, registry=None)
¶
retry_resolution for a FIGI attempt already made, as figi_attempts makes them.
Lets a runner retry many Instruments from one batched mapping call. registry must hold
what the store holds; without one it is loaded.
Source code in src/cointoss/ingest.py
resolve_typed_symbol(store, source, symbol, at, *, scope=None, figi=None, coingecko=None, coingecko_id=None)
async
¶
Resolve a symbol typed against a source on a date, persisting whatever it establishes.
Builds the Observation, makes the FIGI attempt, and resolves it by the policy in the module
docstring. Returns None when no Instrument can be named, which is a result rather than an
error.
For a CoinGecko symbol that is not already found in the Ticker History, the coin's CoinGecko
id is looked up and recorded on the Observation; coingecko_id names the coin outright
when the symbol is shared. It is only valid against CoinGecko.
Source code in src/cointoss/ingest.py
pin_with_attempt(store, source, symbol, at, attempt, *, scope=None, registry=None)
¶
resolve_typed_symbol for a FIGI attempt already made, as figi_attempts makes them.
Lets a sweep pin many symbols from one batched mapping call, by the same policy. Not for
CoinGecko, whose symbols also need the coin's CoinGecko id looked up. registry must hold
what the store holds; without one it is loaded.
Source code in src/cointoss/ingest.py
pin_member(store, name, role, source, symbol, at, *, scope=None, figi=None, coingecko=None, coingecko_id=None)
async
¶
Add a typed ticker to a Universe Definition, pinned to an Instrument if it resolves.
The edit is one new revision, dated at, naming the role's field as changed. It is
recorded whether or not resolution succeeds: an unresolved member is still a change to the
member set, and a cold registry delays membership rather than blocking the edit. Re-adding
a ticker already standing in the same role from the same source changes nothing and makes
no FIGI call.
Source code in src/cointoss/ingest.py
hold(store, instrument_id, source, symbol, at)
¶
Include a traded Instrument in portfolio-held, so the sweep or the stock sweep prices it.
The Definition is created on first use with the Instrument as its one Inclusion and no rank
rule. An Instrument already included changes nothing, so trading it again adds no revision.
source and symbol are what the trade named it by, recorded as the member's provenance.
Source code in src/cointoss/ingest.py
check_listing_width(top_n, exit_ranks)
¶
Refuse a listing of top_n coins that stops short of the widest exit rank.
>>> check_listing_width(120, [120, None, 12])
>>> check_listing_width(119, [120])
Traceback (most recent call last):
...
cointoss.ingest.SweepTooNarrow: top_n 119 is narrower than exit rank 120
Source code in src/cointoss/ingest.py
sweep(store, at, definitions, *, top_n=DEFAULT_TOP_N, bar_window=DEFAULT_BAR_WINDOW, coingecko=None)
async
¶
Turn one CoinGecko markets listing into Instruments, membership and bars for a day.
at is the moment of the sweep, when the listing is fetched. Its UTC date is the date
everything is recorded for, and it stamps the bars' fetched_at and the Runs' run_at.
A day's bar is the price at its 00:00 UTC, which is what CoinGecko labels its own daily
point with, and a listing's current_price is that price only when fetched soon after
midnight. So bars are written only when at is within bar_window of the day's 00:00 UTC,
and are then final, not Provisional. A later sweep still resolves Instruments and records
membership, and reports bars_skipped_late; the day's bars are left for a backfill. Nor is
a bar already stored for the day from CoinGecko overwritten: a re-sweep's later price is not
a new vintage of the 00:00 price, and overwriting it would file a false Restatement.
The top top_n coins are listed by market cap, paging as needed. Each coin is one
Observation whose FIGI attempt is recorded NOT_ATTEMPTED -- the crypto FIGI request is
unverified and must not gate the sweep; retry_resolution takes these up later -- carrying
its CoinGecko id as a provider External Reference, so a coin already held is matched
through a ticker change rather than reminted. Every coin gets native scope, as a
CoinGecko symbol typed for pinning does.
An Inclusion stands whatever its rank, so a coin a named Definition includes but the listing left out is fetched by its CoinGecko id and gets its bar and its Ticker History as a listed coin does. It is not ranked: it cannot enter a band from outside the listing.
Each named Definition is evaluated against CoinGecko's own market_cap_rank, not the
listing position, which the API does not keep equal to it; a coin with no rank, or not
listed, is absent. A Definition is evaluated once per day: when a Run for the day is already
stored, it is returned rather than evaluated again, since a moved market would otherwise
put two memberships on one date. The Definitions are named by the caller rather than read
from the store, because a Definition dropped from configuration stays stored. top_n must
reach the widest exit rank among them, or SweepTooNarrow is raised before anything is
fetched; an unknown name raises UnknownUniverse.
A coin whose Observation cannot be formed, or that the registry flags for review, is logged
and skipped. A Definition whose membership the store refuses for this date is logged and
reported in failed.
Source code in src/cointoss/ingest.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |