From a75aa5e461bce23db78e6c9a2790f642c98b8468 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 5 Apr 2023 14:15:02 -0400 Subject: [PATCH] Use `force_mkt` override in paper pps updates When processing paper trades ledgers we normally won't have specific `MktPair` info for the backend market we're simulating, as such we need to look up this info when updating pps.toml files such that we get precision info correct (particularly in the case of cryptos!) and can also run paper ledger processing without running the simulated clearing loop. In order to make it happen we lookup any `get_mkt_info()` ep on the backend and pass the output to the `force_mkt` input of the `PpTable.update_from_trans()` method. --- piker/accounting/_pos.py | 4 ++-- piker/clearing/_paper_engine.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/piker/accounting/_pos.py b/piker/accounting/_pos.py index 12daa6bf..2b3af58f 100644 --- a/piker/accounting/_pos.py +++ b/piker/accounting/_pos.py @@ -504,7 +504,7 @@ class PpTable(Struct): trans: dict[str, Transaction], cost_scalar: float = 2, - mkt: MktPair | None = None, + force_mkt: MktPair | None = None, ) -> dict[str, Position]: @@ -523,7 +523,7 @@ class PpTable(Struct): # template the mkt-info presuming a legacy market ticks # if no info exists in the transactions.. - mkt: MktPair | Symbol | None = mkt or t.sys + mkt: MktPair | Symbol | None = force_mkt or t.sys if not mkt: mkt = MktPair.from_fqme( fqme, diff --git a/piker/clearing/_paper_engine.py b/piker/clearing/_paper_engine.py index 3c6fdc4c..086263a2 100644 --- a/piker/clearing/_paper_engine.py +++ b/piker/clearing/_paper_engine.py @@ -34,9 +34,13 @@ import pendulum import trio import tractor +from ..brokers import get_brokermod from .. import data from ..data.types import Struct -from ..accounting._mktinfo import Symbol +from ..accounting._mktinfo import ( + Symbol, + MktPair, +) from ..accounting import ( Position, PpTable, @@ -545,8 +549,31 @@ async def trades_dialogue( 'paper', ) as ledger ): + # attempt to get market info from the backend instead of presuming + # the ledger entries have everything correct. + # TODO: how to process ledger info from backends? + # - should we be rolling our own actor-cached version of these + # client API refs or using portal IPC to send requests to the + # existing brokerd daemon? + # - alternatively we can possibly expect and use + # a `.broker.norm_trade_records()` ep? + mkt: MktPair | None = None + brokermod = get_brokermod(broker) + gmi = getattr(brokermod, 'get_mkt_info', None) + if gmi: + mkt, pair = await brokermod.get_mkt_info( + fqme.rstrip(f'.{broker}'), + ) + # update pos table from ledger history - ppt.update_from_trans(ledger.to_trans()) + ppt.update_from_trans( + ledger.to_trans(), + + # NOTE: here we pass in any `MktPair` provided by the + # backend broker instead of assuming the pps.toml contains + # the correct contents! + force_mkt=mkt + ) pp_msgs: list[BrokerdPosition] = [] pos: Position