From cc15d0248850892cd7a62d8a8742a6f53df16b38 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 1 Jul 2022 15:18:23 -0400 Subject: [PATCH] Fix `.minimize_clears()` to include clears since zero This was just implemented totally wrong but somehow worked XD The idea was to include all trades that contribute to ongoing position size since the last time the position was "net zero", i.e. no position in the asset. Adjust arithmetic to *subtract* from the current size until a zero size condition is met and then keep all those clears as part of the "current state" clears table. Additionally this fixes another bug where the positions freshly loaded from a ledger *were not* being merged with the current `pps.toml` state. --- piker/pp.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/piker/pp.py b/piker/pp.py index 0a67d04f..5a953e0d 100644 --- a/piker/pp.py +++ b/piker/pp.py @@ -285,15 +285,14 @@ class Position(Struct): all transactions before the last net zero size to avoid unecessary history irrelevant to the current pp state. - ''' - size: float = 0 + size: float = self.size clears_since_zero: deque[tuple(str, dict)] = deque() # scan for the last "net zero" position by # iterating clears in reverse. for tid, clear in reversed(self.clears.items()): - size += clear['size'] + size -= clear['size'] clears_since_zero.appendleft((tid, clear)) if size == 0: @@ -358,8 +357,6 @@ def update_pps( # track clearing data pp.update(r) - assert len(set(pp.clears)) == len(pp.clears) - return pps @@ -632,7 +629,7 @@ def load_pps_from_toml( # index clears entries in "object" form by tid in a top # level dict instead of a list (as is presented in our # ``pps.toml``). - clears = {} + clears = pp_objs[bsuid].clears for clears_table in clears_list: tid = clears_table.pop('tid') clears[tid] = clears_table @@ -716,6 +713,11 @@ def update_pps_conf( for bsuid in list(pp_objs): pp = pp_objs[bsuid] + + # XXX: debug hook for size mismatches + # if bsuid == 447767096: + # breakpoint() + pp.minimize_clears() if (