Start to prep `Transaction` for `MktPair`..

rekt_pps
Tyler Goodlet 2023-03-14 20:59:32 -04:00
parent 2583706b35
commit 63304f535c
1 changed files with 38 additions and 4 deletions

View File

@ -35,7 +35,10 @@ import toml
from .. import config from .. import config
from ..data.types import Struct from ..data.types import Struct
from ..log import get_logger from ..log import get_logger
from ._mktinfo import Symbol from ._mktinfo import (
Symbol, # legacy
MktPair,
)
log = get_logger(__name__) log = get_logger(__name__)
@ -90,10 +93,13 @@ def open_trade_ledger(
class Transaction(Struct, frozen=True): class Transaction(Struct, frozen=True):
# TODO: should this be ``.to`` (see below)?
fqsn: str
sym: Symbol # TODO: unify this with the `MktPair`,
# once we have that as a required field,
# we don't really need the fqsn any more..
fqsn: str
sym: Symbol | MktPair
tid: Union[str, int] # unique transaction id tid: Union[str, int] # unique transaction id
size: float size: float
price: float price: float
@ -101,17 +107,45 @@ class Transaction(Struct, frozen=True):
dt: datetime dt: datetime
expiry: datetime | None = None expiry: datetime | None = None
@property
def mktpair(self) -> MktPair:
sym = self.sym
if isinstance(sym, MktPair):
# presume it's already set as our desired
# ``MktPair`` type:
return sym
# cast to new type
return MktPair.from_fqme(
sym.fqme,
price_tick=digits_to_dec(
Decimal(str(sym.tick_size)),
),
size_tick=digits_to_dec(
Decimal(str(sym.lot_tick_size)),
),
)
# remap for back-compat
@property
def fqme(self) -> str:
return self.fqsn
# optional key normally derived from the broker # optional key normally derived from the broker
# backend which ensures the instrument-symbol this record # backend which ensures the instrument-symbol this record
# is for is truly unique. # is for is truly unique.
bsuid: Union[str, int] | None = None bsuid: Union[str, int] | None = None
# XXX NOTE: this will come from the `MktPair`
# instead of defined here right?
# optional fqsn for the source "asset"/money symbol? # optional fqsn for the source "asset"/money symbol?
# from: Optional[str] = None # from: Optional[str] = None
def iter_by_dt( def iter_by_dt(
clears: dict[str, Any], clears: dict[str, Any],
) -> Iterator[tuple[str, dict]]: ) -> Iterator[tuple[str, dict]]:
''' '''
Iterate entries of a ``clears: dict`` table sorted by entry recorded Iterate entries of a ``clears: dict`` table sorted by entry recorded