Be pedantic in `open_trade_ledger()` from sync code
Require passing an explicit flag when entering from sync code with an extra super duper explicit runtime error to indicate how to use in the async case as well! Also, do rewrites of both the fqme (from best match in the symcache according to search - the worst case) or from the `bs_mktid` field if it exists (should only be true for paper engine accounts) AND the `bs_mktid` for paper accounts if it seems un-fully-qualified.account_tests
parent
8330b36e58
commit
14d5b3c963
|
@ -209,11 +209,26 @@ class TransactionLedger(UserDict):
|
||||||
):
|
):
|
||||||
txdict['expiry'] = ''
|
txdict['expiry'] = ''
|
||||||
|
|
||||||
# re-write old acro-key
|
# (maybe) re-write old acro-key
|
||||||
|
|
||||||
fqme: str = txdict.pop('fqsn', None) or txdict['fqme']
|
fqme: str = txdict.pop('fqsn', None) or txdict['fqme']
|
||||||
|
bs_mktid: str | None = txdict.get('bs_mktid')
|
||||||
|
|
||||||
if fqme not in self._symcache.mktmaps:
|
if (
|
||||||
|
fqme not in self._symcache.mktmaps
|
||||||
|
or (
|
||||||
|
# also try to see if this is maybe a paper
|
||||||
|
# engine ledger in which case the bs_mktid
|
||||||
|
# should be the fqme as well!
|
||||||
|
self.account == 'paper'
|
||||||
|
and bs_mktid
|
||||||
|
and fqme != bs_mktid
|
||||||
|
)
|
||||||
|
):
|
||||||
|
# always take any (paper) bs_mktid if defined and
|
||||||
|
# in the backend's cache key set.
|
||||||
|
if bs_mktid in self._symcache.mktmaps:
|
||||||
|
fqme: str = bs_mktid
|
||||||
|
else:
|
||||||
best_fqme: str = list(self._symcache.search(fqme))[0]
|
best_fqme: str = list(self._symcache.search(fqme))[0]
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Could not find FQME: {fqme} in qualified set?\n'
|
f'Could not find FQME: {fqme} in qualified set?\n'
|
||||||
|
@ -221,6 +236,15 @@ class TransactionLedger(UserDict):
|
||||||
)
|
)
|
||||||
fqme = best_fqme
|
fqme = best_fqme
|
||||||
|
|
||||||
|
if (
|
||||||
|
self.account == 'paper'
|
||||||
|
and bs_mktid
|
||||||
|
and bs_mktid != fqme
|
||||||
|
):
|
||||||
|
# in paper account case always make sure both the
|
||||||
|
# fqme and bs_mktid are fully qualified..
|
||||||
|
txdict['bs_mktid'] = fqme
|
||||||
|
|
||||||
txdict['fqme'] = fqme
|
txdict['fqme'] = fqme
|
||||||
towrite[tid] = txdict
|
towrite[tid] = txdict
|
||||||
|
|
||||||
|
@ -272,6 +296,9 @@ def open_trade_ledger(
|
||||||
broker: str,
|
broker: str,
|
||||||
account: str,
|
account: str,
|
||||||
|
|
||||||
|
allow_from_sync_code: bool = False,
|
||||||
|
symcache: SymbologyCache | None = None,
|
||||||
|
|
||||||
# default is to sort by detected datetime-ish field
|
# default is to sort by detected datetime-ish field
|
||||||
tx_sort: Callable = iter_by_dt,
|
tx_sort: Callable = iter_by_dt,
|
||||||
rewrite: bool = False,
|
rewrite: bool = False,
|
||||||
|
@ -292,10 +319,27 @@ def open_trade_ledger(
|
||||||
ledger_dict, fpath = load_ledger(broker, account)
|
ledger_dict, fpath = load_ledger(broker, account)
|
||||||
cpy = ledger_dict.copy()
|
cpy = ledger_dict.copy()
|
||||||
|
|
||||||
|
# XXX NOTE: if not provided presume we are being called from
|
||||||
|
# sync code and need to maybe run `trio` to generate..
|
||||||
|
if symcache is None:
|
||||||
|
|
||||||
|
# XXX: be mega pendantic and ensure the caller knows what
|
||||||
|
# they're doing..
|
||||||
|
if not allow_from_sync_code:
|
||||||
|
raise RuntimeError(
|
||||||
|
'You MUST set `allow_from_sync_code=True` when '
|
||||||
|
'calling `open_trade_ledger()` from sync code! '
|
||||||
|
'If you are calling from async code you MUST '
|
||||||
|
'instead pass a `symcache: SymbologyCache`!'
|
||||||
|
)
|
||||||
|
|
||||||
from ..data._symcache import (
|
from ..data._symcache import (
|
||||||
get_symcache,
|
get_symcache,
|
||||||
)
|
)
|
||||||
symcache: SymbologyCache = get_symcache(broker)
|
symcache: SymbologyCache = get_symcache(broker)
|
||||||
|
|
||||||
|
assert symcache
|
||||||
|
|
||||||
ledger = TransactionLedger(
|
ledger = TransactionLedger(
|
||||||
ledger_dict=cpy,
|
ledger_dict=cpy,
|
||||||
file_path=fpath,
|
file_path=fpath,
|
||||||
|
|
|
@ -31,7 +31,6 @@ from typing import (
|
||||||
|
|
||||||
import polars as pl
|
import polars as pl
|
||||||
from pendulum import (
|
from pendulum import (
|
||||||
# datetime,
|
|
||||||
DateTime,
|
DateTime,
|
||||||
from_timestamp,
|
from_timestamp,
|
||||||
parse,
|
parse,
|
||||||
|
@ -354,16 +353,13 @@ def open_ledger_dfs(
|
||||||
brokername,
|
brokername,
|
||||||
acctname,
|
acctname,
|
||||||
rewrite=True,
|
rewrite=True,
|
||||||
|
allow_from_sync_code=True,
|
||||||
) as ledger,
|
) as ledger,
|
||||||
):
|
):
|
||||||
if not ledger:
|
if not ledger:
|
||||||
raise ValueError(f'No ledger for {acctname}@{brokername} exists?')
|
raise ValueError(f'No ledger for {acctname}@{brokername} exists?')
|
||||||
|
|
||||||
print(f'LEDGER LOAD TIME: {time.time() - now}')
|
print(f'LEDGER LOAD TIME: {time.time() - now}')
|
||||||
# if acctname == 'paper':
|
|
||||||
# txns: dict[str, Transaction] = ledger.to_trans()
|
|
||||||
# else:
|
|
||||||
|
|
||||||
# process raw TOML ledger into txns using the
|
# process raw TOML ledger into txns using the
|
||||||
# appropriate backend normalizer.
|
# appropriate backend normalizer.
|
||||||
# cache: AssetsInfo = get_symcache(
|
# cache: AssetsInfo = get_symcache(
|
||||||
|
|
Loading…
Reference in New Issue