From 56b660fe347d67a40ab75355ada3e04bd576f1a5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 29 Sep 2025 15:02:50 -0400 Subject: [PATCH] Draft a gt-one-`.fqme`-in-txns/account-file test To start this is just a shell for the test, there's no checking logic yet.. put it as `test_accounting.test_ib_account_with_duplicated_mktids()`. The test is composed for now to be completely runtime-free using only the offline txn-ledger / symcache / account loading APIs, ideally we fill in the activated symbology-data-runtime cases once we figure a sane way to handle incremental symcache updates for backends like IB.. To actually fill the test out with real checks we still need to, - extract the problem account file from my ib.algopape into the test harness data. - pick some contracts with multiple fqmes despite a single bs_mktid and ensure they're aggregated as a single `Position` as well as, * ideally de-duplicating txns from the account file section for the mkt.. * warning appropriately about greater-then-one fqme for the bs_mktid and providing a way for the ledger re-writing to choose the appropriate `` as the "primary" when the data-symbology-runtime is up and possibly use it to incrementally update the IB symcache and store offline for next use? --- tests/test_accounting.py | 100 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/tests/test_accounting.py b/tests/test_accounting.py index 952a9229..1e22c91d 100644 --- a/tests/test_accounting.py +++ b/tests/test_accounting.py @@ -12,12 +12,14 @@ from piker import config from piker.accounting import ( Account, calc, - Position, - TransactionLedger, - open_trade_ledger, + open_account, load_account, load_account_from_ledger, + open_trade_ledger, + Position, + TransactionLedger, ) +import tractor def test_root_conf_networking_section( @@ -53,12 +55,17 @@ def test_account_file_default_empty( ) def test_paper_ledger_position_calcs( fq_acnt: tuple[str, str], + debug_mode: bool, ): broker: str acnt_name: str broker, acnt_name = fq_acnt - accounts_path: Path = config.repodir() / 'tests' / '_inputs' + accounts_path: Path = ( + config.repodir() + / 'tests' + / '_inputs' # tests-local-subdir + ) ldr: TransactionLedger with ( @@ -77,6 +84,7 @@ def test_paper_ledger_position_calcs( ledger=ldr, _fp=accounts_path, + debug_mode=debug_mode, ) as (dfs, ledger), @@ -102,3 +110,87 @@ def test_paper_ledger_position_calcs( df = dfs[xrp] assert df['cumsize'][-1] == 0 assert pos.cumsize == 0 + + + +@pytest.mark.parametrize( + 'fq_acnt', + [ + ('ib', 'algopaper'), + ], +) +def test_ib_account_with_duplicated_mktids( + fq_acnt: tuple[str, str], + debug_mode: bool, +): + # ?TODO, once we start symcache-incremental-update-support? + # from piker.data import ( + # open_symcache, + # ) + # + # async def main(): + # async with ( + # # TODO: do this as part of `open_account()`!? + # open_symcache( + # 'ib', + # only_from_memcache=True, + # ) as symcache, + # ): + + + from piker.brokers.ib.ledger import ( + tx_sort, + + # ?TODO, once we want to pull lowlevel txns and process them? + # norm_trade_records, + # update_ledger_from_api_trades, + ) + + broker: str + acnt_id: str = 'algopaper' + broker, acnt_id = fq_acnt + accounts_def = config.load_accounts([broker]) + assert accounts_def[f'{broker}.{acnt_id}'] + + ledger: TransactionLedger + acnt: Account + with ( + tractor.devx.maybe_open_crash_handler(pdb=debug_mode), + + open_trade_ledger( + 'ib', + acnt_id, + tx_sort=tx_sort, + + # TODO, eventually incrementally updated for IB.. + # symcache=symcache, + symcache=None, + allow_from_sync_code=True, + + ) as ledger, + + open_account( + 'ib', + acnt_id, + write_on_exit=True, + ) as acnt, + ): + # per input params + symcache = ledger.symcache + assert not ( + symcache.pairs + or + symcache.pairs + or + symcache.mktmaps + ) + # re-compute all positions that have changed state. + # TODO: likely we should change the API to return the + # position updates from `.update_from_ledger()`? + active, closed = acnt.dump_active() + + # breakpoint() + + # TODO, (see above imports as well) incremental update from + # (updated) ledger? + # -[ ] pull some code from `.ib.broker` content.