Partition ledger data frames by `bs_mktid`

Since some backends are going to have the issue of supporting multiple
venues for a given "position distinguishing instrument", like IB, we
can't presume that every `Position` can be uniquely keyed by
a `MktPair.fqme` (since the venue part can change and still be the same
"pair" relationship in accounting terms) so instead presume the
"backend system's market id" is the unique key (at least for now)
instead of the fqme.

More practically we use the `bs_mktid` to groupby-partition the per
pair DFs from the trades ledger and attempt to scan-match the input
fqme (in `ledger disect` cli) against the fqme column values set.
account_tests
Tyler Goodlet 2023-07-26 12:13:54 -04:00
parent 1d35747fbf
commit bebc817d19
2 changed files with 21 additions and 3 deletions

View File

@ -428,7 +428,7 @@ def open_ledger_dfs(
# break up into a frame per mkt / fqme # break up into a frame per mkt / fqme
dfs: dict[str, pl.DataFrame] = ldf.partition_by( dfs: dict[str, pl.DataFrame] = ldf.partition_by(
'fqme', 'bs_mktid',
as_dict=True, as_dict=True,
) )

View File

@ -19,6 +19,7 @@ CLI front end for trades ledger and position tracking management.
''' '''
from __future__ import annotations from __future__ import annotations
from pprint import pformat
from rich.console import Console from rich.console import Console
@ -264,7 +265,7 @@ def disect(
# ledger dfs groupby-partitioned by fqme # ledger dfs groupby-partitioned by fqme
dfs: dict[str, pl.DataFrame] dfs: dict[str, pl.DataFrame]
# actual ledger ref filled in with all txns # actual ledger instance
ldgr: TransactionLedger ldgr: TransactionLedger
pl.Config.set_tbl_cols(16) pl.Config.set_tbl_cols(16)
@ -277,7 +278,24 @@ def disect(
): ):
# look up specific frame for fqme-selected asset # look up specific frame for fqme-selected asset
df = dfs[fqme] if (df := dfs.get(fqme)) is None:
mktids2fqmes: dict[str, list[str]] = {}
for bs_mktid in dfs:
df: pl.DataFrame = dfs[bs_mktid]
fqmes: pl.Series[str] = df['fqme']
uniques: list[str] = fqmes.unique()
mktids2fqmes[bs_mktid] = set(uniques)
if fqme in uniques:
break
print(
f'No specific ledger for fqme={fqme} could be found in\n'
f'{pformat(mktids2fqmes)}?\n'
f'Maybe the `{brokername}` backend uses something '
'else for its `bs_mktid` then the `fqme`?\n'
'Scanning for matches in unique fqmes per frame..\n'
)
# :pray:
assert not df.is_empty() assert not df.is_empty()
# TODO: we REALLY need a better console REPL for this # TODO: we REALLY need a better console REPL for this