Skip to content

cointoss.app

app

The cointoss namespace: the scheduled sweeps and backfill, and the read API.

CointossApp is a lythonic NamespaceFragment. woodglue mounts it from woodglue.yaml as one entry of a namespace's entries, and fragment_entry builds that entry:

namespaces:
  cointoss:
    run_engine: true
    entries:
      - type: fragment
        gref: "cointoss.app:CointossApp"
        nsref: "data:"
        configs:
          sweep:
            triggers: [{name: daily_sweep, schedule: "5 0 * * *"}]
          backfill:
            triggers: [{name: daily_backfill, schedule: "0 1 * * *"}]
          stock_sweep:
            triggers: [{name: weekday_stock_sweep, schedule: "30 22 * * 1-5"}]

The read nodes are tagged api, so woodglue serves them over JSON-RPC as cointoss.data:members and so on. sweep and backfill are not: they write and call CoinGecko, so they run from their triggers, from woodglue's system.fire_trigger, or from the CLI, never from an ordinary API call.

backfill (cointoss.backfill) has its own trigger rather than running inside the sweep, so a slow or failing backfill neither delays the day's sweep nor marks it failed, and each firing is logged on its own. It fires an hour after the default sweep; the order is not load-bearing, since neither overwrites a bar the other stored. It takes the sweep lock for each write rather than for its whole run, which is long, so a sweep or backup waiting for the lock gets it within one coin's write.

stock_sweep (cointoss.stocks) evaluates the reserved yahoo-industries universe from Yahoo's industry lists, resolves their tickers through OpenFIGI and fetches the stocks' bars from Yahoo (cointoss.stock_bars), on its own trigger on weekdays after the US close, 22:30 UTC by default. It is idempotent within a US session, and like the backfill it fetches outside the sweep lock and takes it for its writes, since its OpenFIGI calls are paced to minutes on a first run.

The portfolio writes, create_portfolio, add_trade and void_trade, are api nodes too, but declare the Required Action WRITE_ACTION, write (woodglue ADR 0005). woodglue runs them over JSON-RPC only for a Grant of write on the namespace, which no call Grant reaches, wildcards such as call * included; the explorer's Grant and a reader's Role are call cointoss. The portfolio Role (PORTFOLIO_ROLE, PORTFOLIO_GRANTS) holds write cointoss beside call cointoss, and the built-in admin Role reaches every Action. A refused write is answered before the node runs, which a JSON-RPC client receives as -32003. woodglue checks only requests it serves: a node called in process, as the tests do, runs for any Caller.

The caller is the owner: create_portfolio records the Caller's User id, and add_trade and void_trade act only on that User's Portfolios. Another User's Portfolio or Trade is reported exactly as a missing one is, NotFound, so ids do not reveal what others hold. With auth disabled every request is woodglue's anonymous Caller, and owns as anonymous.

Runtime settings come from the data directory (cointoss.config.Settings): init.data_dir if given, else woodglue's instance data directory (current_mount), else $COINTOSS_HOME, else ~/.local/share/cointoss. A woodglue.yaml written by cointoss init carries no init.data_dir, so a copied instance serves its own store; older files that carry one still work. Settings are read once, when the fragment is built, so a bad cointoss.yaml stops woodglue at startup rather than failing the next sweep silently. A config edit takes effect on restart.

Each call opens the store and closes it before returning, rather than holding one connection for the fragment's life. woodglue calls a sync node on the IOLoop thread while lythonic's DagRunner runs one in an executor thread, and a sqlite3 connection refuses use from a thread other than its creator's; the store's WAL journal lets these short readers run beside a sweep's writes; and nothing is left holding a transaction or a replaced file. Opening costs the schema and version check, a few small queries, which a local API at human rates does not notice.

A read never creates anything: against a data directory with no cointoss.db it raises NotInitialized, naming cointoss init. Only init and the sweep create the store.

One sweep runs at a time per data directory. The sweep node takes an exclusive flock on sweep.lock there, which cointoss backup also holds while it copies. A scheduled firing waits for the lock, up to SWEEP_LOCK_WAIT: lythonic records a firing as the trigger's last run even when it fails, so a firing skipped because a backup held the lock would not be retried until the next day, losing that day's bars. It waits by retrying a non-blocking flock between asyncio.sleeps, never with a blocking flock, which would stall woodglue's IOLoop and the JSON-RPC API with it. A manual cointoss sweep passes wait=0, so one started while another runs raises SweepInProgress at once and does nothing.

A request the store cannot answer -- an unknown universe or Instrument, a date before a universe's first entry, a malformed date -- raises an ApiError carrying a JSON-RPC error code and a message naming what was not found. ApiError is a woodglue RpcError, so a JSON-RPC client receives that code and message, and woodglue logs it without a traceback.

DEFAULT_SWEEP_SCHEDULE = '5 0 * * *' module-attribute

Five past midnight UTC: after CoinGecko's 00:00 point exists, and early enough to be it.

SWEEP_NODE = 'sweep' module-attribute

The fragment's sweep node, as named in a namespace entry's configs.

DEFAULT_BACKFILL_SCHEDULE = '0 1 * * *' module-attribute

01:00 UTC: after the default sweep has written the day's bars and released the lock.

DEFAULT_STOCK_SWEEP_SCHEDULE = '30 22 * * 1-5' module-attribute

22:30 UTC on weekdays: after the US close at 16:00 New York, in summer and in winter.

NAMESPACE = 'cointoss' module-attribute

The woodglue namespace cointoss init mounts the fragment under.

WRITE_ACTION = 'write' module-attribute

The Required Action of the portfolio writes; only a write Grant on the namespace runs them.

PORTFOLIO_GRANTS = (f'call {NAMESPACE}', f'{WRITE_ACTION} {NAMESPACE}') module-attribute

The Grants of PORTFOLIO_ROLE: the read API and the portfolio writes.

SWEEP_LOCK_WAIT = 600.0 module-attribute

Seconds a scheduled sweep or a backup waits for the sweep lock before giving up.

LOCK_POLL = 1.0 module-attribute

Seconds between attempts to take a held sweep lock.

ApiError

Bases: RpcError

A request the store cannot answer, with the JSON-RPC error code it stands for.

Source code in src/cointoss/app.py
class ApiError(RpcError):
    """A request the store cannot answer, with the JSON-RPC error code it stands for."""

    code: int = -32000

NotFound

Bases: ApiError

The universe or Instrument named is not stored, or holds nothing on the date asked.

Source code in src/cointoss/app.py
class NotFound(ApiError):
    """The universe or Instrument named is not stored, or holds nothing on the date asked."""

    code: int = -32001

NotInitialized

Bases: ApiError

The data directory holds no database yet.

Source code in src/cointoss/app.py
class NotInitialized(ApiError):
    """The data directory holds no database yet."""

    code: int = -32002

BadRequest

Bases: ApiError

A parameter is malformed: not an ISO date, a range ending before it starts.

Source code in src/cointoss/app.py
class BadRequest(ApiError):
    """A parameter is malformed: not an ISO date, a range ending before it starts."""

    code: int = -32602

Unauthorized

Bases: ApiError

A write called in-process, with no Caller to own what it writes.

Source code in src/cointoss/app.py
class Unauthorized(ApiError):
    """A write called in-process, with no Caller to own what it writes."""

    code: int = -32000

Conflict

Bases: ApiError

The write contradicts what is stored: a Portfolio name taken, a Trade already voided.

Source code in src/cointoss/app.py
class Conflict(ApiError):
    """The write contradicts what is stored: a Portfolio name taken, a Trade already voided."""

    code: int = -32004

SweepInProgress

Bases: Exception

Another sweep holds the data directory's sweep lock.

Source code in src/cointoss/app.py
class SweepInProgress(Exception):
    """Another sweep holds the data directory's sweep lock."""

MemberView

Bases: BaseModel

One member on a date and its bar for that date, if one is stored.

Close, volume and market cap are what a swept CoinGecko bar holds. Rank is not persisted, so it is not here.

Source code in src/cointoss/app.py
class MemberView(BaseModel):
    """One member on a date and its bar for that date, if one is stored.

    Close, volume and market cap are what a swept CoinGecko bar holds. Rank is not persisted,
    so it is not here.
    """

    model_config: ClassVar[ConfigDict] = ConfigDict(frozen=True)

    instrument_id: str
    symbol: str | None
    name: str | None
    close: float | None
    volume: float | None
    market_cap: float | None
    source: Source | None
    fetched_at: dt.datetime | None

ChangeView

Bases: BaseModel

What joined and what left a universe on one date.

Source code in src/cointoss/app.py
class ChangeView(BaseModel):
    """What joined and what left a universe on one date."""

    model_config: ClassVar[ConfigDict] = ConfigDict(frozen=True)

    date: dt.date
    admitted: list[str]
    dropped: list[str]

UniverseView

Bases: BaseModel

A stored Universe Definition at its latest revision.

Source code in src/cointoss/app.py
class UniverseView(BaseModel):
    """A stored Universe Definition at its latest revision."""

    model_config: ClassVar[ConfigDict] = ConfigDict(frozen=True)

    name: str
    revision: int
    revised_at: dt.date
    enter_rank: int | None
    exit_rank: int | None
    inclusions: list[str]
    exclusions: list[str]

SweepOutcome

Bases: BaseModel

What one firing of the sweep did for the UTC day.

already_swept means every declared universe already had a Run for day, so nothing was fetched and report is None.

Source code in src/cointoss/app.py
class SweepOutcome(BaseModel):
    """What one firing of the sweep did for the UTC `day`.

    `already_swept` means every declared universe already had a Run for `day`, so nothing was
    fetched and `report` is None.
    """

    model_config: ClassVar[ConfigDict] = ConfigDict(frozen=True)

    day: dt.date
    already_swept: bool
    reconciled: list[UniverseReconciled]
    report: ingest.SweepReport | None

CointossApp

Bases: NamespaceFragment

The scheduled sweeps and backfill, and the read API over one instance's store.

Source code in src/cointoss/app.py
class CointossApp(NamespaceFragment):
    """The scheduled sweeps and backfill, and the read API over one instance's store."""

    settings: Settings

    def __init__(self, data_dir: str | None = None) -> None:
        if data_dir is not None:
            home = Path(data_dir)
        else:
            mount = current_mount.get(None)
            home = mount.data_dir if mount is not None else None
        self.settings = Settings.load(home)

    @contextmanager
    def _store(self) -> Iterator[Store]:
        """The store for a read, which must already exist; opening never creates one here."""
        if not self.settings.db_path.exists():
            raise NotInitialized(
                f"no database at {self.settings.db_path}; run `cointoss init` first"
            )
        try:
            with Store(self.settings.db_path) as store:
                yield store
        except (
            UnknownUniverse,
            UnknownInstrument,
            UnknownPortfolio,
            UnknownTrade,
            NotYetStarted,
        ) as exc:
            raise NotFound(str(exc)) from exc
        except (DuplicatePortfolio, TradeAlreadyVoided) as exc:
            raise Conflict(str(exc)) from exc

    @nsnode(tags=["scheduled"])
    async def sweep(self, force: bool = False, wait: float = SWEEP_LOCK_WAIT) -> SweepOutcome:
        """Reconcile the declared universes, then sweep CoinGecko `markets` for today (UTC).

        Idempotent within a UTC day. Once every declared universe has a Run for today, a further
        firing fetches nothing and reports `already_swept`. Otherwise -- the first firing, a
        retry after a failure, a universe newly declared, or `force` -- it sweeps at the current
        time. `ingest.sweep` returns a universe's Run already recorded for the day rather than
        evaluating it again, and keeps any bar already stored for the day, so a re-sweep only
        fills in what is missing and never files a Restatement against the day's own bars.

        The universes swept are the declared ones and, once a trade has created it,
        `portfolio-held`.

        Creates the data directory and store if absent. Waits up to `wait` seconds for the sweep
        lock, since the trigger fires the node with no arguments, and raises `SweepInProgress` if
        another sweep or a backup still holds it then.
        """
        self.settings.data_dir.mkdir(parents=True, exist_ok=True)
        async with sweep_lock_async(self.settings.data_dir, wait):
            # Read the clock once the lock is held: the wait may have taken minutes.
            now = as_utc(utcnow())
            day = now.date()
            with Store(self.settings.db_path) as store:
                reconciled = ensure_universes(store, self.settings.universes, day)
                names = [declared.name for declared in self.settings.universes]
                if store.load_universe(PORTFOLIO_HELD) is not None:
                    names.append(PORTFOLIO_HELD)
                swept_today = {name for name in names if store.runs_on(name, day)}
                if not force and names and swept_today >= set(names):
                    log.info("sweep %s: already swept", day)
                    return SweepOutcome(
                        day=day, already_swept=True, reconciled=reconciled, report=None
                    )
                report = await ingest.sweep(
                    store,
                    now,
                    names,
                    top_n=self.settings.top_n,
                    bar_window=self.settings.bar_window,
                )
        log.info(
            "sweep %s: listed %d, %d bars, %d kept, %d restatements%s, failed %s",
            day,
            report.listed,
            report.bars,
            report.bars_kept,
            report.restatements,
            ", late: no bars" if report.bars_skipped_late else "",
            sorted(report.failed) or "none",
        )
        return SweepOutcome(day=day, already_swept=False, reconciled=reconciled, report=report)

    @nsnode(tags=["scheduled"])
    async def backfill(
        self, days: int = BACKFILL_DAYS, wait: float = SWEEP_LOCK_WAIT
    ) -> BackfillReport:
        """Fill every date with no stored bar, from CoinGecko's last `days` days and from Yahoo.

        See `cointoss.backfill`. Each write waits up to `wait` seconds for the sweep lock and
        raises `SweepInProgress` if it is still held then. Refuses a data directory with no
        store, since only a sweep has universes whose members it could fill.
        """
        if not self.settings.db_path.exists():
            raise NotInitialized(
                f"no database at {self.settings.db_path}; run `cointoss init` first"
            )
        data_dir = self.settings.data_dir
        with Store(self.settings.db_path) as store:
            return await run_backfill(
                store, utcnow(), days=days, lock=lambda: sweep_lock_async(data_dir, wait)
            )

    @nsnode(tags=["scheduled"])
    async def stock_sweep(
        self, force: bool = False, wait: float = SWEEP_LOCK_WAIT
    ) -> StockSweepReport:
        """Evaluate `yahoo-industries` for the latest US session closed, and retry unresolved stocks.

        See `cointoss.stocks`. Idempotent within a session unless `force`. Creates the data
        directory and store if absent. Its writes wait up to `wait` seconds for the sweep lock,
        and raise `SweepInProgress` if it is still held then.
        """
        self.settings.data_dir.mkdir(parents=True, exist_ok=True)
        data_dir = self.settings.data_dir
        with Store(self.settings.db_path) as store:
            return await sweep_stocks(
                store, utcnow(), force=force, lock=lambda: sweep_lock_async(data_dir, wait)
            )

    @nsnode(tags=["api"])
    def members(self, universe: str, date: str) -> MembersView:
        """The members of a universe on a date, each with that date's close, volume and market cap.

        Bars are dated by the session they close (Session Offset), so on the latest swept date a
        CoinGecko-priced member has no close yet: its close is the next day's 00:00 price, and
        arrives with the next sweep.
        """
        on = _date(date, "date")
        with self._store() as store:
            bar_by_member = store.member_bars(universe, on)
            instruments = {i: store.load_instrument(i) for i in bar_by_member}
        views: list[MemberView] = []
        for instrument_id, bar in bar_by_member.items():
            instrument = instruments[instrument_id]
            views.append(
                MemberView(
                    instrument_id=str(instrument_id),
                    symbol=instrument.symbol if instrument else None,
                    name=instrument.name if instrument else None,
                    close=bar.close if bar else None,
                    volume=bar.volume if bar else None,
                    market_cap=bar.market_cap if bar else None,
                    source=bar.source if bar else None,
                    fetched_at=bar.fetched_at if bar else None,
                )
            )
        return MembersView(universe=universe, date=on, members=views)

    @nsnode(tags=["api"])
    def changes(self, universe: str, start: str, end: str) -> list[ChangeView]:
        """Joins and leaves by date, for each date in `[start, end]` the membership changed.

        A universe's first entry reports every member as joining.
        """
        first, last = _range(start, end)
        with self._store() as store:
            series = store.load_universe_series(universe)
        views: list[ChangeView] = []
        previous: dt.date | None = None
        for entry in series.entries:
            if first <= entry.as_of <= last:
                if previous is None:
                    admitted, dropped = tuple(entry.universe), ()
                else:
                    change = series.change(previous, entry.as_of)
                    admitted, dropped = change.admitted, change.dropped
                views.append(
                    ChangeView(date=entry.as_of, admitted=list(admitted), dropped=list(dropped))
                )
            previous = entry.as_of
        return views

    @nsnode(tags=["api"])
    def bars(self, instrument_id: str, start: str, end: str) -> list[Bar]:
        """One Instrument's bars from `start` to `end` inclusive, through its Price Sources.

        Each is dated by the session it closes, as `Store.bars_for` resolves them.
        """
        first, last = _range(start, end)
        with self._store() as store:
            return store.bars_for(InstrumentId(instrument_id), first, last)

    @nsnode(tags=["api"])
    def universes_of(self, instrument_id: str, start: str, end: str) -> list[str]:
        """The universes an Instrument was a member of on any day of `[start, end]`."""
        first, last = _range(start, end)
        with self._store() as store:
            return list(store.universes_containing(InstrumentId(instrument_id), first, last))

    @nsnode(tags=["api"])
    def runs(self, universe: str, limit: int = 20) -> list[EvaluationRun]:
        """A universe's most recent Evaluation Runs, newest first: whether the sweep is alive."""
        if limit < 1:
            raise BadRequest(f"limit must be at least 1, got {limit}")
        with self._store() as store:
            return store.load_evaluation_runs(universe)[::-1][:limit]

    @nsnode(tags=["api"])
    def universes(self) -> list[UniverseView]:
        """Every stored Universe Definition with its current band, declared in config or not."""
        with self._store() as store:
            definitions = [store.require_universe(n) for n in store.universe_names()]
        return [
            UniverseView(
                name=d.name,
                revision=d.revision,
                revised_at=d.revisions[-1].changed_at,
                enter_rank=d.parameters.enter_rank,
                exit_rank=d.parameters.exit_rank,
                inclusions=sorted(d.parameters.inclusions),
                exclusions=sorted(d.parameters.exclusions),
            )
            for d in definitions
        ]

    @nsnode(tags=["api"])
    def datasets(self) -> list[DatasetView]:
        """Every Dataset `frame` reads, with its columns and the JSON Schema of its params."""
        return describe()

    @nsnode(tags=["api"])
    def frame(self, dataset: str, params: dict[str, Any] | None = None) -> FrameData:
        """One Dataset as a long table, `params` validated against its model (see `datasets`).

        An unknown Dataset is `NotFound`; params its model refuses, or a match over
        `cointoss.datasets.ROW_CAP` rows, are `BadRequest`. Nothing matching is an empty frame.

        An owned Dataset (`trades`, `positions`, `portfolio_value`) holds only the caller's own
        Portfolios, and another User's Portfolio named in its params is `NotFound`, as one that
        does not exist. Read in-process, with no Caller, it is `Unauthorized`, as a write is.
        """
        try:
            found = lookup(dataset)
            parsed = found.parse(params)
        except UnknownDataset as exc:
            raise NotFound(str(exc)) from None
        except InvalidParams as exc:
            raise BadRequest(str(exc)) from None
        if isinstance(found, OwnedDataset):
            owner = _caller_user(f"{dataset} reads a User's portfolios")
            read = partial(found.read, params=parsed, owner=owner)
        else:
            read = partial(found.read, params=parsed)
        with self._store() as store:
            try:
                return read(store)
            except TooManyRows as exc:
                raise BadRequest(
                    f"{dataset}: {exc.count} rows match, over the cap of {exc.limit}; "
                    "narrow the range, the instruments or the universe"
                ) from None

    @nsnode(tags=["api"], requires=WRITE_ACTION)
    def create_portfolio(
        self, name: str, kind: str = PortfolioKind.REAL, custodian: str | None = None
    ) -> Portfolio:
        """Create a Portfolio owned by the caller. `kind` is `real`, `paper` or `what_if`.

        A name is unique among the caller's own Portfolios; reusing one is a `Conflict`.
        """
        owner = _writer()
        portfolio_kind = _choice(PortfolioKind, kind, "kind")
        if not name.strip():
            raise BadRequest("name: a portfolio needs a name")
        with self._store() as store:
            return store.create_portfolio(owner, name.strip(), portfolio_kind, custodian)

    @nsnode(tags=["api"], requires=WRITE_ACTION)
    async def add_trade(
        self,
        portfolio_id: int,
        instrument: str,
        quantity: float,
        cost: float | None = None,
        traded_at: str | None = None,
        kind: str = TradeKind.REAL,
        note: str | None = None,
        source: str = Source.COINGECKO,
        coingecko_id: str | None = None,
    ) -> Trade:
        """Record a Trade in one of the caller's Portfolios, and hold its Instrument for pricing.

        `instrument` is a stored Instrument Id (`type.scope.symbol`) or a symbol typed against
        `source`, pinned as `cointoss.ingest.resolve_typed_symbol` pins one, with its mandatory
        FIGI attempt; `coingecko_id` names the coin when a CoinGecko symbol is shared. A positive
        `quantity` is a buy, a negative one a sale; `cost` is the Trade's total in USD.
        `traded_at` is an ISO time, read as UTC when it has no offset, and now by default.

        The Instrument is included in `portfolio-held`, created on first use, so the next
        sweep writes a coin's bar even when it ranks outside every declared universe, and the
        next stock sweep a stock's whole history, listed by Yahoo or not; a stock is typed
        against `yahoo`.

        The other arguments are checked before the Instrument is named, since pinning a symbol
        stores its Instrument. The Inclusion is written before the Trade, each in its own commit:
        should the Trade then fail, an Instrument held with no Trade only costs the sweep a
        bar, while a Trade whose Instrument is not held would go unpriced.
        """
        owner = _writer()
        now = as_utc(utcnow())
        when = now if traded_at is None else _moment(traded_at, "traded_at")
        trade_kind = _choice(TradeKind, kind, "kind")
        typed_source = _choice(Source, source, "source")
        checked_quantity = _checked(check_quantity, quantity, "quantity")
        checked_cost = None if cost is None else _checked(check_cost, cost, "cost")
        with self._store() as store:
            _require_owned(store, owner, portfolio_id)
            named, by_source, by_symbol = await _named_instrument(
                store, instrument, typed_source, now.date(), coingecko_id
            )
            ingest.hold(store, named, by_source, by_symbol, now.date())
            try:
                return store.record_trade(
                    portfolio_id,
                    named,
                    when,
                    checked_quantity,
                    kind=trade_kind,
                    cost=checked_cost,
                    note=note,
                )
            except ValidationError as exc:
                raise BadRequest(one_line(exc)) from None

    @nsnode(tags=["api"], requires=WRITE_ACTION)
    def void_trade(self, trade_id: int) -> Trade:
        """Void one of the caller's Trades now. It stays recorded and counts toward nothing.

        A Trade is voided once: voiding it again is a `Conflict`, and the first Void stands.
        """
        owner = _writer()
        with self._store() as store:
            trade = store.load_trade(trade_id)
            if trade is None or not _owns(store, owner, trade.portfolio_id):
                raise NotFound(f"no trade {trade_id} of yours")
            return store.void_trade(trade_id, at=as_utc(utcnow()))

sweep(force=False, wait=SWEEP_LOCK_WAIT) async

Reconcile the declared universes, then sweep CoinGecko markets for today (UTC).

Idempotent within a UTC day. Once every declared universe has a Run for today, a further firing fetches nothing and reports already_swept. Otherwise -- the first firing, a retry after a failure, a universe newly declared, or force -- it sweeps at the current time. ingest.sweep returns a universe's Run already recorded for the day rather than evaluating it again, and keeps any bar already stored for the day, so a re-sweep only fills in what is missing and never files a Restatement against the day's own bars.

The universes swept are the declared ones and, once a trade has created it, portfolio-held.

Creates the data directory and store if absent. Waits up to wait seconds for the sweep lock, since the trigger fires the node with no arguments, and raises SweepInProgress if another sweep or a backup still holds it then.

Source code in src/cointoss/app.py
@nsnode(tags=["scheduled"])
async def sweep(self, force: bool = False, wait: float = SWEEP_LOCK_WAIT) -> SweepOutcome:
    """Reconcile the declared universes, then sweep CoinGecko `markets` for today (UTC).

    Idempotent within a UTC day. Once every declared universe has a Run for today, a further
    firing fetches nothing and reports `already_swept`. Otherwise -- the first firing, a
    retry after a failure, a universe newly declared, or `force` -- it sweeps at the current
    time. `ingest.sweep` returns a universe's Run already recorded for the day rather than
    evaluating it again, and keeps any bar already stored for the day, so a re-sweep only
    fills in what is missing and never files a Restatement against the day's own bars.

    The universes swept are the declared ones and, once a trade has created it,
    `portfolio-held`.

    Creates the data directory and store if absent. Waits up to `wait` seconds for the sweep
    lock, since the trigger fires the node with no arguments, and raises `SweepInProgress` if
    another sweep or a backup still holds it then.
    """
    self.settings.data_dir.mkdir(parents=True, exist_ok=True)
    async with sweep_lock_async(self.settings.data_dir, wait):
        # Read the clock once the lock is held: the wait may have taken minutes.
        now = as_utc(utcnow())
        day = now.date()
        with Store(self.settings.db_path) as store:
            reconciled = ensure_universes(store, self.settings.universes, day)
            names = [declared.name for declared in self.settings.universes]
            if store.load_universe(PORTFOLIO_HELD) is not None:
                names.append(PORTFOLIO_HELD)
            swept_today = {name for name in names if store.runs_on(name, day)}
            if not force and names and swept_today >= set(names):
                log.info("sweep %s: already swept", day)
                return SweepOutcome(
                    day=day, already_swept=True, reconciled=reconciled, report=None
                )
            report = await ingest.sweep(
                store,
                now,
                names,
                top_n=self.settings.top_n,
                bar_window=self.settings.bar_window,
            )
    log.info(
        "sweep %s: listed %d, %d bars, %d kept, %d restatements%s, failed %s",
        day,
        report.listed,
        report.bars,
        report.bars_kept,
        report.restatements,
        ", late: no bars" if report.bars_skipped_late else "",
        sorted(report.failed) or "none",
    )
    return SweepOutcome(day=day, already_swept=False, reconciled=reconciled, report=report)

backfill(days=BACKFILL_DAYS, wait=SWEEP_LOCK_WAIT) async

Fill every date with no stored bar, from CoinGecko's last days days and from Yahoo.

See cointoss.backfill. Each write waits up to wait seconds for the sweep lock and raises SweepInProgress if it is still held then. Refuses a data directory with no store, since only a sweep has universes whose members it could fill.

Source code in src/cointoss/app.py
@nsnode(tags=["scheduled"])
async def backfill(
    self, days: int = BACKFILL_DAYS, wait: float = SWEEP_LOCK_WAIT
) -> BackfillReport:
    """Fill every date with no stored bar, from CoinGecko's last `days` days and from Yahoo.

    See `cointoss.backfill`. Each write waits up to `wait` seconds for the sweep lock and
    raises `SweepInProgress` if it is still held then. Refuses a data directory with no
    store, since only a sweep has universes whose members it could fill.
    """
    if not self.settings.db_path.exists():
        raise NotInitialized(
            f"no database at {self.settings.db_path}; run `cointoss init` first"
        )
    data_dir = self.settings.data_dir
    with Store(self.settings.db_path) as store:
        return await run_backfill(
            store, utcnow(), days=days, lock=lambda: sweep_lock_async(data_dir, wait)
        )

stock_sweep(force=False, wait=SWEEP_LOCK_WAIT) async

Evaluate yahoo-industries for the latest US session closed, and retry unresolved stocks.

See cointoss.stocks. Idempotent within a session unless force. Creates the data directory and store if absent. Its writes wait up to wait seconds for the sweep lock, and raise SweepInProgress if it is still held then.

Source code in src/cointoss/app.py
@nsnode(tags=["scheduled"])
async def stock_sweep(
    self, force: bool = False, wait: float = SWEEP_LOCK_WAIT
) -> StockSweepReport:
    """Evaluate `yahoo-industries` for the latest US session closed, and retry unresolved stocks.

    See `cointoss.stocks`. Idempotent within a session unless `force`. Creates the data
    directory and store if absent. Its writes wait up to `wait` seconds for the sweep lock,
    and raise `SweepInProgress` if it is still held then.
    """
    self.settings.data_dir.mkdir(parents=True, exist_ok=True)
    data_dir = self.settings.data_dir
    with Store(self.settings.db_path) as store:
        return await sweep_stocks(
            store, utcnow(), force=force, lock=lambda: sweep_lock_async(data_dir, wait)
        )

members(universe, date)

The members of a universe on a date, each with that date's close, volume and market cap.

Bars are dated by the session they close (Session Offset), so on the latest swept date a CoinGecko-priced member has no close yet: its close is the next day's 00:00 price, and arrives with the next sweep.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def members(self, universe: str, date: str) -> MembersView:
    """The members of a universe on a date, each with that date's close, volume and market cap.

    Bars are dated by the session they close (Session Offset), so on the latest swept date a
    CoinGecko-priced member has no close yet: its close is the next day's 00:00 price, and
    arrives with the next sweep.
    """
    on = _date(date, "date")
    with self._store() as store:
        bar_by_member = store.member_bars(universe, on)
        instruments = {i: store.load_instrument(i) for i in bar_by_member}
    views: list[MemberView] = []
    for instrument_id, bar in bar_by_member.items():
        instrument = instruments[instrument_id]
        views.append(
            MemberView(
                instrument_id=str(instrument_id),
                symbol=instrument.symbol if instrument else None,
                name=instrument.name if instrument else None,
                close=bar.close if bar else None,
                volume=bar.volume if bar else None,
                market_cap=bar.market_cap if bar else None,
                source=bar.source if bar else None,
                fetched_at=bar.fetched_at if bar else None,
            )
        )
    return MembersView(universe=universe, date=on, members=views)

changes(universe, start, end)

Joins and leaves by date, for each date in [start, end] the membership changed.

A universe's first entry reports every member as joining.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def changes(self, universe: str, start: str, end: str) -> list[ChangeView]:
    """Joins and leaves by date, for each date in `[start, end]` the membership changed.

    A universe's first entry reports every member as joining.
    """
    first, last = _range(start, end)
    with self._store() as store:
        series = store.load_universe_series(universe)
    views: list[ChangeView] = []
    previous: dt.date | None = None
    for entry in series.entries:
        if first <= entry.as_of <= last:
            if previous is None:
                admitted, dropped = tuple(entry.universe), ()
            else:
                change = series.change(previous, entry.as_of)
                admitted, dropped = change.admitted, change.dropped
            views.append(
                ChangeView(date=entry.as_of, admitted=list(admitted), dropped=list(dropped))
            )
        previous = entry.as_of
    return views

bars(instrument_id, start, end)

One Instrument's bars from start to end inclusive, through its Price Sources.

Each is dated by the session it closes, as Store.bars_for resolves them.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def bars(self, instrument_id: str, start: str, end: str) -> list[Bar]:
    """One Instrument's bars from `start` to `end` inclusive, through its Price Sources.

    Each is dated by the session it closes, as `Store.bars_for` resolves them.
    """
    first, last = _range(start, end)
    with self._store() as store:
        return store.bars_for(InstrumentId(instrument_id), first, last)

universes_of(instrument_id, start, end)

The universes an Instrument was a member of on any day of [start, end].

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def universes_of(self, instrument_id: str, start: str, end: str) -> list[str]:
    """The universes an Instrument was a member of on any day of `[start, end]`."""
    first, last = _range(start, end)
    with self._store() as store:
        return list(store.universes_containing(InstrumentId(instrument_id), first, last))

runs(universe, limit=20)

A universe's most recent Evaluation Runs, newest first: whether the sweep is alive.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def runs(self, universe: str, limit: int = 20) -> list[EvaluationRun]:
    """A universe's most recent Evaluation Runs, newest first: whether the sweep is alive."""
    if limit < 1:
        raise BadRequest(f"limit must be at least 1, got {limit}")
    with self._store() as store:
        return store.load_evaluation_runs(universe)[::-1][:limit]

universes()

Every stored Universe Definition with its current band, declared in config or not.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def universes(self) -> list[UniverseView]:
    """Every stored Universe Definition with its current band, declared in config or not."""
    with self._store() as store:
        definitions = [store.require_universe(n) for n in store.universe_names()]
    return [
        UniverseView(
            name=d.name,
            revision=d.revision,
            revised_at=d.revisions[-1].changed_at,
            enter_rank=d.parameters.enter_rank,
            exit_rank=d.parameters.exit_rank,
            inclusions=sorted(d.parameters.inclusions),
            exclusions=sorted(d.parameters.exclusions),
        )
        for d in definitions
    ]

datasets()

Every Dataset frame reads, with its columns and the JSON Schema of its params.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def datasets(self) -> list[DatasetView]:
    """Every Dataset `frame` reads, with its columns and the JSON Schema of its params."""
    return describe()

frame(dataset, params=None)

One Dataset as a long table, params validated against its model (see datasets).

An unknown Dataset is NotFound; params its model refuses, or a match over cointoss.datasets.ROW_CAP rows, are BadRequest. Nothing matching is an empty frame.

An owned Dataset (trades, positions, portfolio_value) holds only the caller's own Portfolios, and another User's Portfolio named in its params is NotFound, as one that does not exist. Read in-process, with no Caller, it is Unauthorized, as a write is.

Source code in src/cointoss/app.py
@nsnode(tags=["api"])
def frame(self, dataset: str, params: dict[str, Any] | None = None) -> FrameData:
    """One Dataset as a long table, `params` validated against its model (see `datasets`).

    An unknown Dataset is `NotFound`; params its model refuses, or a match over
    `cointoss.datasets.ROW_CAP` rows, are `BadRequest`. Nothing matching is an empty frame.

    An owned Dataset (`trades`, `positions`, `portfolio_value`) holds only the caller's own
    Portfolios, and another User's Portfolio named in its params is `NotFound`, as one that
    does not exist. Read in-process, with no Caller, it is `Unauthorized`, as a write is.
    """
    try:
        found = lookup(dataset)
        parsed = found.parse(params)
    except UnknownDataset as exc:
        raise NotFound(str(exc)) from None
    except InvalidParams as exc:
        raise BadRequest(str(exc)) from None
    if isinstance(found, OwnedDataset):
        owner = _caller_user(f"{dataset} reads a User's portfolios")
        read = partial(found.read, params=parsed, owner=owner)
    else:
        read = partial(found.read, params=parsed)
    with self._store() as store:
        try:
            return read(store)
        except TooManyRows as exc:
            raise BadRequest(
                f"{dataset}: {exc.count} rows match, over the cap of {exc.limit}; "
                "narrow the range, the instruments or the universe"
            ) from None

create_portfolio(name, kind=PortfolioKind.REAL, custodian=None)

Create a Portfolio owned by the caller. kind is real, paper or what_if.

A name is unique among the caller's own Portfolios; reusing one is a Conflict.

Source code in src/cointoss/app.py
@nsnode(tags=["api"], requires=WRITE_ACTION)
def create_portfolio(
    self, name: str, kind: str = PortfolioKind.REAL, custodian: str | None = None
) -> Portfolio:
    """Create a Portfolio owned by the caller. `kind` is `real`, `paper` or `what_if`.

    A name is unique among the caller's own Portfolios; reusing one is a `Conflict`.
    """
    owner = _writer()
    portfolio_kind = _choice(PortfolioKind, kind, "kind")
    if not name.strip():
        raise BadRequest("name: a portfolio needs a name")
    with self._store() as store:
        return store.create_portfolio(owner, name.strip(), portfolio_kind, custodian)

add_trade(portfolio_id, instrument, quantity, cost=None, traded_at=None, kind=TradeKind.REAL, note=None, source=Source.COINGECKO, coingecko_id=None) async

Record a Trade in one of the caller's Portfolios, and hold its Instrument for pricing.

instrument is a stored Instrument Id (type.scope.symbol) or a symbol typed against source, pinned as cointoss.ingest.resolve_typed_symbol pins one, with its mandatory FIGI attempt; coingecko_id names the coin when a CoinGecko symbol is shared. A positive quantity is a buy, a negative one a sale; cost is the Trade's total in USD. traded_at is an ISO time, read as UTC when it has no offset, and now by default.

The Instrument is included in portfolio-held, created on first use, so the next sweep writes a coin's bar even when it ranks outside every declared universe, and the next stock sweep a stock's whole history, listed by Yahoo or not; a stock is typed against yahoo.

The other arguments are checked before the Instrument is named, since pinning a symbol stores its Instrument. The Inclusion is written before the Trade, each in its own commit: should the Trade then fail, an Instrument held with no Trade only costs the sweep a bar, while a Trade whose Instrument is not held would go unpriced.

Source code in src/cointoss/app.py
@nsnode(tags=["api"], requires=WRITE_ACTION)
async def add_trade(
    self,
    portfolio_id: int,
    instrument: str,
    quantity: float,
    cost: float | None = None,
    traded_at: str | None = None,
    kind: str = TradeKind.REAL,
    note: str | None = None,
    source: str = Source.COINGECKO,
    coingecko_id: str | None = None,
) -> Trade:
    """Record a Trade in one of the caller's Portfolios, and hold its Instrument for pricing.

    `instrument` is a stored Instrument Id (`type.scope.symbol`) or a symbol typed against
    `source`, pinned as `cointoss.ingest.resolve_typed_symbol` pins one, with its mandatory
    FIGI attempt; `coingecko_id` names the coin when a CoinGecko symbol is shared. A positive
    `quantity` is a buy, a negative one a sale; `cost` is the Trade's total in USD.
    `traded_at` is an ISO time, read as UTC when it has no offset, and now by default.

    The Instrument is included in `portfolio-held`, created on first use, so the next
    sweep writes a coin's bar even when it ranks outside every declared universe, and the
    next stock sweep a stock's whole history, listed by Yahoo or not; a stock is typed
    against `yahoo`.

    The other arguments are checked before the Instrument is named, since pinning a symbol
    stores its Instrument. The Inclusion is written before the Trade, each in its own commit:
    should the Trade then fail, an Instrument held with no Trade only costs the sweep a
    bar, while a Trade whose Instrument is not held would go unpriced.
    """
    owner = _writer()
    now = as_utc(utcnow())
    when = now if traded_at is None else _moment(traded_at, "traded_at")
    trade_kind = _choice(TradeKind, kind, "kind")
    typed_source = _choice(Source, source, "source")
    checked_quantity = _checked(check_quantity, quantity, "quantity")
    checked_cost = None if cost is None else _checked(check_cost, cost, "cost")
    with self._store() as store:
        _require_owned(store, owner, portfolio_id)
        named, by_source, by_symbol = await _named_instrument(
            store, instrument, typed_source, now.date(), coingecko_id
        )
        ingest.hold(store, named, by_source, by_symbol, now.date())
        try:
            return store.record_trade(
                portfolio_id,
                named,
                when,
                checked_quantity,
                kind=trade_kind,
                cost=checked_cost,
                note=note,
            )
        except ValidationError as exc:
            raise BadRequest(one_line(exc)) from None

void_trade(trade_id)

Void one of the caller's Trades now. It stays recorded and counts toward nothing.

A Trade is voided once: voiding it again is a Conflict, and the first Void stands.

Source code in src/cointoss/app.py
@nsnode(tags=["api"], requires=WRITE_ACTION)
def void_trade(self, trade_id: int) -> Trade:
    """Void one of the caller's Trades now. It stays recorded and counts toward nothing.

    A Trade is voided once: voiding it again is a `Conflict`, and the first Void stands.
    """
    owner = _writer()
    with self._store() as store:
        trade = store.load_trade(trade_id)
        if trade is None or not _owns(store, owner, trade.portfolio_id):
            raise NotFound(f"no trade {trade_id} of yours")
        return store.void_trade(trade_id, at=as_utc(utcnow()))

utcnow()

The clock the sweep reads; a module function so a test can fix it.

Source code in src/cointoss/app.py
def utcnow() -> dt.datetime:
    """The clock the sweep reads; a module function so a test can fix it."""
    return dt.datetime.now(dt.UTC)

utc_today()

Today's date on the UTC calendar, by utcnow.

Source code in src/cointoss/app.py
def utc_today() -> dt.date:
    """Today's date on the UTC calendar, by `utcnow`."""
    return as_utc(utcnow()).date()

fragment_entry(*, data_dir=None, schedule=DEFAULT_SWEEP_SCHEDULE, backfill_schedule=DEFAULT_BACKFILL_SCHEDULE, stock_schedule=DEFAULT_STOCK_SWEEP_SCHEDULE)

The woodglue.yaml namespace entry mounting the fragment with its scheduled triggers.

lythonic reads triggers only from configuration, so the default schedule lives here and in whatever cointoss init writes from it. Without data_dir the fragment resolves the data directory itself when it is built, from woodglue's mount when there is one.

Source code in src/cointoss/app.py
def fragment_entry(
    *,
    data_dir: Path | None = None,
    schedule: str = DEFAULT_SWEEP_SCHEDULE,
    backfill_schedule: str = DEFAULT_BACKFILL_SCHEDULE,
    stock_schedule: str = DEFAULT_STOCK_SWEEP_SCHEDULE,
) -> dict[str, Any]:
    """The `woodglue.yaml` namespace entry mounting the fragment with its scheduled triggers.

    lythonic reads triggers only from configuration, so the default schedule lives here and in
    whatever `cointoss init` writes from it. Without `data_dir` the fragment resolves the data
    directory itself when it is built, from woodglue's mount when there is one.
    """
    entry: dict[str, Any] = {
        "type": "fragment",
        "gref": FRAGMENT_GREF,
        "nsref": "data:",
        "configs": {
            SWEEP_NODE: {"triggers": [{"name": SWEEP_TRIGGER, "schedule": schedule}]},
            BACKFILL_NODE: {
                "triggers": [{"name": BACKFILL_TRIGGER, "schedule": backfill_schedule}]
            },
            STOCK_SWEEP_NODE: {
                "triggers": [{"name": STOCK_SWEEP_TRIGGER, "schedule": stock_schedule}]
            },
        },
    }
    if data_dir is not None:
        entry["init"] = {"data_dir": str(data_dir)}
    return entry

fragment_schedule(entry, node=SWEEP_NODE, trigger=SWEEP_TRIGGER)

A node's schedule in a namespace entry shaped like fragment_entry's, if any.

>>> fragment_schedule(fragment_entry(schedule="35 0 * * *"))
'35 0 * * *'
>>> fragment_schedule(fragment_entry(), BACKFILL_NODE, BACKFILL_TRIGGER)
'0 1 * * *'
>>> fragment_schedule({"type": "fragment"}) is None
True
Source code in src/cointoss/app.py
def fragment_schedule(
    entry: dict[str, Any], node: str = SWEEP_NODE, trigger: str = SWEEP_TRIGGER
) -> str | None:
    """A node's schedule in a namespace entry shaped like `fragment_entry`'s, if any.

    >>> fragment_schedule(fragment_entry(schedule="35 0 * * *"))
    '35 0 * * *'
    >>> fragment_schedule(fragment_entry(), BACKFILL_NODE, BACKFILL_TRIGGER)
    '0 1 * * *'
    >>> fragment_schedule({"type": "fragment"}) is None
    True
    """
    for configured in entry.get("configs", {}).get(node, {}).get("triggers", []):
        if configured.get("name") == trigger:
            return configured.get("schedule")
    return None

sweep_lock(data_dir, wait=0.0)

Hold the data directory's sweep lock, waiting up to wait seconds for it.

Raises SweepInProgress if it is still held then; with no wait, at once. The wait sleeps the calling thread, so the event loop uses sweep_lock_async. An flock is released by the kernel when its holder exits, so a killed sweep leaves no stale lock behind.

Source code in src/cointoss/app.py
@contextmanager
def sweep_lock(data_dir: Path, wait: float = 0.0) -> Iterator[None]:
    """Hold the data directory's sweep lock, waiting up to `wait` seconds for it.

    Raises `SweepInProgress` if it is still held then; with no `wait`, at once. The wait sleeps
    the calling thread, so the event loop uses `sweep_lock_async`. An `flock` is released by the
    kernel when its holder exits, so a killed sweep leaves no stale lock behind.
    """
    path = data_dir / SWEEP_LOCK
    deadline = time.monotonic() + wait
    with path.open("a") as f:
        while not _try_lock(f):
            remaining = deadline - time.monotonic()
            if remaining <= 0:
                raise _lock_held(path, wait) from None
            time.sleep(min(LOCK_POLL, remaining))
        try:
            yield
        finally:
            fcntl.flock(f, fcntl.LOCK_UN)

sweep_lock_async(data_dir, wait=0.0) async

sweep_lock, waiting with asyncio.sleep so the event loop keeps running meanwhile.

Source code in src/cointoss/app.py
@asynccontextmanager
async def sweep_lock_async(data_dir: Path, wait: float = 0.0) -> AsyncIterator[None]:
    """`sweep_lock`, waiting with `asyncio.sleep` so the event loop keeps running meanwhile."""
    path = data_dir / SWEEP_LOCK
    deadline = time.monotonic() + wait
    with path.open("a") as f:
        while not _try_lock(f):
            remaining = deadline - time.monotonic()
            if remaining <= 0:
                raise _lock_held(path, wait) from None
            await asyncio.sleep(min(LOCK_POLL, remaining))
        try:
            yield
        finally:
            fcntl.flock(f, fcntl.LOCK_UN)

one_line(exc, *, whole='')

An error's message on one line: each validation error's field and reason, or the text.

A validation error of the whole model, with no field, is labelled whole when given.

>>> one_line(ValueError("not   here"))
'not here'
Source code in src/cointoss/app.py
def one_line(exc: Exception, *, whole: str = "") -> str:
    """An error's message on one line: each validation error's field and reason, or the text.

    A validation error of the whole model, with no field, is labelled `whole` when given.

    >>> one_line(ValueError("not   here"))
    'not here'
    """
    if not isinstance(exc, ValidationError):
        return " ".join(str(exc).split())
    reasons: list[str] = []
    for error in exc.errors():
        label = ".".join(str(part) for part in error["loc"]) or whole
        reasons.append(f"{label}: {error['msg']}" if label else error["msg"])
    return "; ".join(reasons)