Use new `.config` helpers for `accounting._pos/._ledger` file loading

master
Tyler Goodlet 2023-05-12 13:02:29 -04:00
parent 5278f8b560
commit 5f79434b23
4 changed files with 13 additions and 35 deletions

View File

@ -61,7 +61,7 @@ def get_likely_pair(
dst: str,
bs_mktid: str,
) -> str:
) -> str | None:
'''
Attempt to get the likely trading pair matching a given destination
asset `dst: str`.
@ -76,9 +76,9 @@ def get_likely_pair(
# positions where the src fiat was used to
# buy some other dst which was furhter used
# to buy another dst..)
log.warning(
f'No src fiat {src} found in {bs_mktid}?'
)
# log.warning(
# f'No src fiat {src} found in {bs_mktid}?'
# )
return
likely_dst = bs_mktid[:src_name_start]

View File

@ -34,7 +34,6 @@ from pendulum import (
datetime,
parse,
)
import tomli
import toml
from .. import config
@ -227,30 +226,12 @@ def open_trade_ledger(
name as defined in the user's ``brokers.toml`` config.
'''
ldir: Path = config._config_dir / 'ledgers'
if not ldir.is_dir():
ldir.mkdir()
fname = f'trades_{broker}_{account}.toml'
tradesfile: Path = ldir / fname
if not tradesfile.is_file():
log.info(
f'Creating new local trades ledger: {tradesfile}'
)
tradesfile.touch()
with tradesfile.open(mode='rb') as cf:
start = time.time()
ledger_dict = tomli.load(cf)
log.info(f'Ledger load took {time.time() - start}s')
ledger_dict, fpath = config.load_ledger(broker, account)
cpy = ledger_dict.copy()
ledger = TransactionLedger(
ledger_dict=cpy,
file_path=tradesfile,
file_path=fpath,
)
try:
yield ledger
finally:

View File

@ -187,7 +187,7 @@ class Position(Struct):
inline_table = toml.TomlDecoder().get_empty_inline_table()
# serialize datetime to parsable `str`
dtstr = inline_table['dt'] = str(data['dt'])
dtstr = inline_table['dt'] = data['dt'].isoformat('T')
assert 'Datetime' not in dtstr
# insert optional clear fields in column order
@ -670,8 +670,7 @@ class PpTable(Struct):
pos: Position
for bs_mktid, pos in active.items():
# keep the minimal amount of clears that make up this
# NOTE: we only store the minimal amount of clears that make up this
# position since the last net-zero state.
pos.minimize_clears()
pos.ensure_state()
@ -679,7 +678,7 @@ class PpTable(Struct):
# serialize to pre-toml form
fqme, asdict = pos.to_pretoml()
# assert 'Datetime' not in asdict['dt']
assert 'Datetime' not in asdict['clears'][0]['dt']
log.info(f'Updating active pp: {fqme}')
# XXX: ugh, it's cuz we push the section under
@ -807,9 +806,7 @@ def open_pps(
'''
conf: dict
conf_path: Path
conf, conf_path = config.load(
f'pps.{brokername}.{acctid}',
)
conf, conf_path = config.load_account(brokername, acctid)
if brokername in conf:
log.warning(

View File

@ -79,7 +79,7 @@ def broker_init(
# enabled.append('piker.data.feed')
# non-blocking setup of brokerd service nursery
from ..brokers import _setup_persistent_brokerd
from ..brokers._daemon import _setup_persistent_brokerd
return (
start_actor_kwargs, # to `ActorNursery.start_actor()`
@ -217,4 +217,4 @@ def sync(
if __name__ == "__main__":
ledger()
ledger() # this is called from ``>> ledger <accountname>``