cointoss.portfolio¶
portfolio
¶
Portfolios, Trades, and the positions derived from them.
Implements ADR-0001 as amended 2026-10-30. A Portfolio is owned by one woodglue User and holds
single-leg Trades with a signed quantity and no cash leg. A Trade is never edited or deleted:
a mistaken one is voided, stays recorded, and is skipped by every position.
There is no Portfolio Snapshot yet. derive_positions replays the Trades on every read, which
is instant for a few hand-entered Trades, and is pure so that what a position means is stated
here once rather than in SQL.
Quantities and costs are float, as every other number in the store is (ADR-0008). This is not
a ledger; exact arithmetic would be a decision about Trade alone.
PORTFOLIO_HELD = 'portfolio-held'
module-attribute
¶
The Universe Definition every traded Instrument is included in, so the sweep prices it. It has no rank rule and is never declared in configuration: trades alone add to it.
UserId = str
module-attribute
¶
A woodglue User's id (Caller.user_id), such as usr_1a2b3c4d5e6f. Users live in
woodglue's own database, so it is a reference by value rather than a foreign key.
PortfolioKind
¶
Bases: StrEnum
Whether a Portfolio holds real assets, simulated ones, or a hypothetical.
Source code in src/cointoss/portfolio.py
TradeKind
¶
Bases: StrEnum
A Trade's provenance: imported, an adjustment, or a bootstrap where history is unknown.
Source code in src/cointoss/portfolio.py
Portfolio
¶
Bases: BaseModel
A logical container for holdings, owned by one User. name is unique per owner.
Source code in src/cointoss/portfolio.py
Trade
¶
Bases: BaseModel
One single-leg Trade: a signed quantity of one Instrument at one moment.
Positive quantity is a buy or deposit, negative a sell or withdrawal. traded_at is held in
UTC; a naive time is read as already UTC. cost is the total USD paid for the Trade, or
received for a disposal, when known.
>>> t = Trade(
... id=1,
... portfolio_id=1,
... instrument_id=InstrumentId("crypto.native.btc"),
... traded_at=datetime(2024, 3, 1, 12, 0),
... quantity=0.5,
... kind=TradeKind.REAL,
... )
>>> t.traded_at.isoformat(), t.voided
('2024-03-01T12:00:00+00:00', False)
Source code in src/cointoss/portfolio.py
PortfolioPosition
¶
Bases: NamedTuple
The aggregate quantity of one Instrument in a Portfolio, with its average cost per unit.
average_cost is None when no acquisition counted toward the position carried a cost.
Source code in src/cointoss/portfolio.py
check_quantity(value)
¶
value as a Trade's quantity, or ValueError unless it is finite and nonzero.
Source code in src/cointoss/portfolio.py
check_cost(value)
¶
value as a Trade's cost, or ValueError unless it is finite and not negative.
Source code in src/cointoss/portfolio.py
end_of_day(day)
¶
The first UTC moment after day, which a position as of day counts up to.
Source code in src/cointoss/portfolio.py
derive_positions(trades, as_of)
¶
Positions as of the end of as_of in UTC, one per Instrument, ordered by Instrument Id.
Voided Trades are skipped whenever they were voided: a void says the Trade never happened. Instruments are aggregated as given, so a caller folds Supersession first.
Average cost is the USD cost of the acquisitions that carry one divided by their quantity. Acquisitions without a cost are ignored, and a disposal removes its share of the costed quantity at the average, so it does not move the average. Once a position reaches zero or goes short its cost is forgotten: a short has no average cost, and an acquisition that reopens a long position counts only the part of it that is long. A position whose trades net to zero is still returned.
>>> btc = InstrumentId("crypto.native.btc")
>>> def trade(day: int, quantity: float, cost: float | None = None) -> Trade:
... return Trade(id=day, portfolio_id=1, instrument_id=btc, kind=TradeKind.REAL,
... traded_at=datetime(2024, 3, day, 12), quantity=quantity, cost=cost)
>>> history = [trade(1, 1.0, 30_000.0), trade(2, 1.0), trade(3, 2.0, 90_000.0), trade(4, -1.0)]
>>> derive_positions(history, date(2024, 3, 4))
[PortfolioPosition(instrument_id=InstrumentId('crypto.native.btc'), quantity=3.0, average_cost=40000.0)]
>>> derive_positions(history[1:2], date(2024, 3, 4))[0].average_cost is None
True