Always clear `Position._events` in `.from_msg()`..

Not sure why i ever thought it would work otherwise but, obviously if
you're replicating a `Position` from a **summary** (IPC) msg we
need to wipe any prior clearing events from the events history..
The main use for this loading mechanism is precisely if you don't have
local access to the txn ledger and need to represent a position from
a summary 🤦

Also, never bother with ledger file fqme "rewriting" if the backend has
no symcache support (yet) since obviously there's then no symbol set to
search for a better key xD
account_tests
Tyler Goodlet 2023-07-26 12:10:26 -04:00
parent e344bdbf1b
commit 1d35747fbf
3 changed files with 27 additions and 6 deletions

View File

@ -199,6 +199,7 @@ class TransactionLedger(UserDict):
tid,
txdict,
pairs=symcache.pairs,
symcache=symcache,
)
yield txn
@ -231,6 +232,7 @@ class TransactionLedger(UserDict):
'''
is_paper: bool = self.account == 'paper'
symcache: SymbologyCache = self._symcache
towrite: dict[str, Any] = {}
for tid, txdict in self.tx_sort(self.data.copy()):
# write blank-str expiry for non-expiring assets
@ -241,12 +243,18 @@ class TransactionLedger(UserDict):
txdict['expiry'] = ''
# (maybe) re-write old acro-key
if is_paper:
if (
is_paper
# if symcache is empty/not supported (yet), don't
# bother xD
and symcache.mktmaps
):
fqme: str = txdict.pop('fqsn', None) or txdict['fqme']
bs_mktid: str | None = txdict.get('bs_mktid')
if (
fqme not in self._symcache.mktmaps
fqme not in symcache.mktmaps
or (
# also try to see if this is maybe a paper
# engine ledger in which case the bs_mktid
@ -257,10 +265,10 @@ class TransactionLedger(UserDict):
):
# always take any (paper) bs_mktid if defined and
# in the backend's cache key set.
if bs_mktid in self._symcache.mktmaps:
if bs_mktid in symcache.mktmaps:
fqme: str = bs_mktid
else:
best_fqme: str = list(self._symcache.search(fqme))[0]
best_fqme: str = list(symcache.search(fqme))[0]
log.warning(
f'Could not find FQME: {fqme} in qualified set?\n'
f'Qualifying and expanding {fqme} -> {best_fqme}'
@ -371,7 +379,7 @@ def open_trade_ledger(
if symcache is None:
# XXX: be mega pendantic and ensure the caller knows what
# they're doing..
# they're doing!
if not allow_from_sync_code:
raise RuntimeError(
'You MUST set `allow_from_sync_code=True` when '

View File

@ -605,7 +605,7 @@ class MktPair(Struct, frozen=True):
if isinstance(self.dst, Asset):
return str(self.dst.atype)
return 'unknown'
return 'UNKNOWN'
@property
def price_tick_digits(self) -> int:

View File

@ -292,11 +292,21 @@ class Position(Struct):
msg: BrokerdPosition,
) -> None:
'''
Hard-set the current position from a remotely-received
(normally via IPC) msg by applying the msg as the one (and
only) txn in the `._events` table thus forcing the current
asset allocation blindly.
'''
mkt: MktPair = self.mkt
now_dt: pendulum.DateTime = now()
now_str: str = str(now_dt)
# XXX: wipe all prior txn history since we wanted it we wouldn't
# be using this method to compute our state!
self._events.clear()
# NOTE WARNING XXX: we summarize the pos with a single
# summary transaction (for now) until we either pass THIS
# type as msg directly from emsd or come up with a better
@ -524,6 +534,9 @@ class Account(Struct):
try:
mkt: MktPair = symcache.mktmaps[fqme]
except KeyError:
if _mktmap_table is None:
raise
# XXX: caller is allowed to provide a fallback
# mktmap table for the case where a new position is
# being added and the preloaded symcache didn't