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.
pps_postmortem
Tyler Goodlet 2022-07-01 15:18:23 -04:00
parent d5bc43e8dd
commit cc15d02488
1 changed files with 8 additions and 6 deletions

View File

@ -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 (