From e4d6f0940d240f205e67d16c8722d616a6f65c72 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 30 Oct 2024 12:49:20 -0400 Subject: [PATCH] Allow ledger passes to ignore (symcache) unknown fqmes For example in the paper-eng, if you have a backend that doesn't fully support a symcache (yet) it's handy to be able to ignore processing other paper-eng txns when all you care about at the moment is the simulated symbol. NOTE, that currently this will still result in a key-error when you load more then one mkt with the paper engine (for which the backend does not have the symcache implemented) since no fqme ad-hoc query was made for the 2nd symbol (and i'm not sure we should support that kinda hackery over just encouraging the sym-cache being added?). Def needs a little more thought depending on how many backends are never going to be able to (easily) support caching.. --- piker/accounting/_pos.py | 45 +++++++++++++++++++++++++++++---- piker/clearing/_paper_engine.py | 1 + 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/piker/accounting/_pos.py b/piker/accounting/_pos.py index 1b305009..5952418f 100644 --- a/piker/accounting/_pos.py +++ b/piker/accounting/_pos.py @@ -30,7 +30,8 @@ from types import ModuleType from typing import ( Any, Iterator, - Generator + Generator, + TYPE_CHECKING, ) import pendulum @@ -59,8 +60,10 @@ from ..clearing._messages import ( BrokerdPosition, ) from piker.types import Struct -from piker.data._symcache import SymbologyCache -from ..log import get_logger +from piker.log import get_logger + +if TYPE_CHECKING: + from piker.data._symcache import SymbologyCache log = get_logger(__name__) @@ -493,6 +496,17 @@ class Account(Struct): _mktmap_table: dict[str, MktPair] | None = None, + only_require: list[str]|True = True, + # ^list of fqmes that are "required" to be processed from + # this ledger pass; we often don't care about others and + # definitely shouldn't always error in such cases. + # (eg. broker backend loaded that doesn't yet supsport the + # symcache but also, inside the paper engine we don't ad-hoc + # request `get_mkt_info()` for every symbol in the ledger, + # only the one for which we're simulating against). + # TODO, not sure if there's a better soln for this, ideally + # all backends get symcache support afap i guess.. + ) -> dict[str, Position]: ''' Update the internal `.pps[str, Position]` table from input @@ -535,11 +549,32 @@ class Account(Struct): if _mktmap_table is None: raise + required: bool = ( + only_require is True + or ( + only_require is not True + and + fqme in only_require + ) + ) # XXX: caller is allowed to provide a fallback # mktmap table for the case where a new position is # being added and the preloaded symcache didn't # have this entry prior (eg. with frickin IB..) - mkt = _mktmap_table[fqme] + if ( + not (mkt := _mktmap_table.get(fqme)) + and + required + ): + raise + + elif not required: + continue + + else: + # should be an entry retreived somewhere + assert mkt + if not (pos := pps.get(bs_mktid)): @@ -656,7 +691,7 @@ class Account(Struct): def write_config(self) -> None: ''' Write the current account state to the user's account TOML file, normally - something like ``pps.toml``. + something like `pps.toml`. ''' # TODO: show diff output? diff --git a/piker/clearing/_paper_engine.py b/piker/clearing/_paper_engine.py index 0393b2e6..fea9ff8e 100644 --- a/piker/clearing/_paper_engine.py +++ b/piker/clearing/_paper_engine.py @@ -653,6 +653,7 @@ async def open_trade_dialog( # in) use manually constructed table from calling # the `.get_mkt_info()` provider EP above. _mktmap_table=mkt_by_fqme, + only_require=list(mkt_by_fqme), ) pp_msgs: list[BrokerdPosition] = []