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

View File

@ -34,7 +34,6 @@ from pendulum import (
datetime, datetime,
parse, parse,
) )
import tomli
import toml import toml
from .. import config from .. import config
@ -227,30 +226,12 @@ def open_trade_ledger(
name as defined in the user's ``brokers.toml`` config. name as defined in the user's ``brokers.toml`` config.
''' '''
ldir: Path = config._config_dir / 'ledgers' ledger_dict, fpath = config.load_ledger(broker, account)
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')
cpy = ledger_dict.copy() cpy = ledger_dict.copy()
ledger = TransactionLedger( ledger = TransactionLedger(
ledger_dict=cpy, ledger_dict=cpy,
file_path=tradesfile, file_path=fpath,
) )
try: try:
yield ledger yield ledger
finally: finally:

View File

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

View File

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