Skip to content

Commit

Permalink
Minimize clears and audit sizing for all updates in `.update_from_tra…
Browse files Browse the repository at this point in the history
…ns()`
  • Loading branch information
goodboy committed Jul 26, 2022
1 parent 958e542 commit 5520e9e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions piker/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,22 @@ def to_pretoml(self) -> dict:
be_price = d.pop('be_price')
d['size'], d['be_price'] = self.audit_sizing(size, be_price)

if expiry:
if self.expiry is None:
d.pop('expiry', None)
elif expiry:
d['expiry'] = str(expiry)

clears_list = []

toml_clears_list = []
for tid, data in clears.items():
inline_table = toml.TomlDecoder().get_empty_inline_table()
inline_table['tid'] = tid

for k, v in data.items():
inline_table[k] = v

clears_list.append(inline_table)
toml_clears_list.append(inline_table)

d['clears'] = clears_list
d['clears'] = toml_clears_list

return d

Expand Down Expand Up @@ -413,6 +414,11 @@ def update_from_trans(
pp.add_clear(r)
updated[r.bsuid] = pp

# minimize clears tables and update sizing.
for bsuid, pp in updated.items():
pp.minimize_clears()
pp.size, pp.be_price = pp.audit_sizing()

return updated

def dump_active(
Expand All @@ -434,7 +440,7 @@ def dump_active(
# NOTE: newly closed position are also important to report/return
# since a consumer, like an order mode UI ;), might want to react
# based on the closure.
pp_entries = {}
pp_active_entries = {}
closed_pp_objs: dict[str, Position] = {}

pp_objs = self.pps
Expand All @@ -446,9 +452,8 @@ def dump_active(
# if bsuid == qqqbsuid:
# breakpoint()

size, be_price = pp.audit_sizing()

pp.minimize_clears()
size, be_price = pp.audit_sizing()

if (
# "net-zero" is a "closed" position
Expand All @@ -464,7 +469,7 @@ def dump_active(
# used to check for duplicate clears that may come in as
# new transaction from some backend API and need to be
# ignored; the closed positions won't be written to the
# ``pps.toml`` since ``pp_entries`` above is what's
# ``pps.toml`` since ``pp_active_entries`` above is what's
# written.
# closed_pp = pp_objs.pop(bsuid, None)
closed_pp = pp_objs.get(bsuid)
Expand All @@ -475,9 +480,6 @@ def dump_active(
# serialize to pre-toml form
asdict = pp.to_pretoml()

if pp.expiry is None:
asdict.pop('expiry', None)

# TODO: we need to figure out how to have one top level
# listing venue here even when the backend isn't providing
# it via the trades ledger..
Expand All @@ -490,9 +492,9 @@ def dump_active(
# the broker name.. maybe we need to rethink this?
brokerless_key = fqsn.removeprefix(f'{brokername}.')

pp_entries[brokerless_key] = asdict
pp_active_entries[brokerless_key] = asdict

return pp_entries, closed_pp_objs
return pp_active_entries, closed_pp_objs


def update_pps(
Expand Down

0 comments on commit 5520e9e

Please sign in to comment.