Always use the "most resolved" `Position.symbol: MktPair`

When loading a `Position` from a pps file we might not have the entire
`MktPair` field-set loaded (though going forward that shouldn't really
ever happen except in the case of a legacy `pps.toml`), in which case we
can check if the `.fqme: str` value loaded from the transaction is
longer and use that instead - presuming it must have more mkt meta-data
filled out.

Also includes some more `fqsn` -> `fqme` renames.
rekt_pps
Tyler Goodlet 2023-03-29 18:01:36 -04:00
parent 50be10a9bd
commit 9f7aa3d1ff
1 changed files with 21 additions and 15 deletions

View File

@ -121,9 +121,8 @@ class Position(Struct):
# it via the trades ledger.. # it via the trades ledger..
# drop symbol obj in serialized form # drop symbol obj in serialized form
s = d.pop('symbol') s = d.pop('symbol')
fqsn = s.fqme fqme = s.fqme
broker, key, suffix = unpack_fqme(fqme)
broker, key, suffix = unpack_fqme(fqsn)
if isinstance(s, Symbol): if isinstance(s, Symbol):
sym_info = s.broker_info[broker] sym_info = s.broker_info[broker]
@ -182,7 +181,7 @@ class Position(Struct):
d['clears'] = toml_clears_list d['clears'] = toml_clears_list
return fqsn, d return fqme, d
def ensure_state(self) -> None: def ensure_state(self) -> None:
''' '''
@ -522,7 +521,7 @@ class PpTable(Struct):
# template the mkt-info presuming a legacy market ticks # template the mkt-info presuming a legacy market ticks
# if no info exists in the transactions.. # if no info exists in the transactions..
mkt = t.sys mkt: MktPair | Symbol = t.sys
if not mkt: if not mkt:
mkt = MktPair.from_fqme( mkt = MktPair.from_fqme(
fqme, fqme,
@ -531,18 +530,25 @@ class PpTable(Struct):
bs_mktid=bs_mktid, bs_mktid=bs_mktid,
) )
pp = pps.setdefault( pp = pps.get(bs_mktid)
bs_mktid, if not pp:
# if no existing pp, allocate fresh one. # if no existing pp, allocate fresh one.
Position( pp = pps[bs_mktid] = Position(
mkt, mkt,
size=0.0, size=0.0,
ppu=0.0, ppu=0.0,
bs_mktid=bs_mktid, bs_mktid=bs_mktid,
expiry=t.expiry, expiry=t.expiry,
) )
) else:
# NOTE: if for some reason a "less resolved" mkt pair
# info has been set (based on the `.fqme` being
# a shorter string), instead use the one from the
# transaction since it likely has (more) full
# information from the provider.
if len(pp.symbol.fqme) < len(fqme):
pp.symbol = mkt
clears = pp.clears clears = pp.clears
if clears: if clears:
first_clear_dt = pp.first_clear_dt first_clear_dt = pp.first_clear_dt
@ -641,12 +647,12 @@ class PpTable(Struct):
pos.ensure_state() pos.ensure_state()
# serialize to pre-toml form # serialize to pre-toml form
fqsn, asdict = pos.to_pretoml() fqme, asdict = pos.to_pretoml()
log.info(f'Updating active pp: {fqsn}') log.info(f'Updating active pp: {fqme}')
# XXX: ugh, it's cuz we push the section under # XXX: ugh, it's cuz we push the section under
# the broker name.. maybe we need to rethink this? # the broker name.. maybe we need to rethink this?
brokerless_key = fqsn.removeprefix(f'{self.brokername}.') brokerless_key = fqme.removeprefix(f'{self.brokername}.')
to_toml_dict[brokerless_key] = asdict to_toml_dict[brokerless_key] = asdict
return to_toml_dict return to_toml_dict
@ -662,8 +668,8 @@ class PpTable(Struct):
pp_entries = self.to_toml() pp_entries = self.to_toml()
if pp_entries: if pp_entries:
log.info( log.info(
f'Updating ``pps.toml``:\n' f'Updating positions in ``{self.conf_path}``:\n'
f'Current positions:\n{pformat(pp_entries)}' f'n{pformat(pp_entries)}'
) )
self.conf[self.brokername][self.acctid] = pp_entries self.conf[self.brokername][self.acctid] = pp_entries