Compare commits
5 Commits
e14008701c
...
f2ae3b0e2e
| Author | SHA1 | Date |
|---|---|---|
|
|
f2ae3b0e2e | |
|
|
56b660fe34 | |
|
|
6eced8ca67 | |
|
|
3eb1bf8248 | |
|
|
e007163816 |
|
|
@ -32,6 +32,7 @@ from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tractor.devx import maybe_open_crash_handler
|
||||||
import polars as pl
|
import polars as pl
|
||||||
from pendulum import (
|
from pendulum import (
|
||||||
DateTime,
|
DateTime,
|
||||||
|
|
@ -290,15 +291,13 @@ def iter_by_dt(
|
||||||
|
|
||||||
# XXX: should never get here..
|
# XXX: should never get here..
|
||||||
else:
|
else:
|
||||||
if debug:
|
with maybe_open_crash_handler(pdb=True):
|
||||||
import tractor
|
raise ValueError(
|
||||||
with tractor.devx.maybe_open_crash_handler():
|
f'Invalid txn time ??\n'
|
||||||
raise ValueError(
|
f'txn-id: {k!r}\n'
|
||||||
f'Invalid txn time ??\n'
|
f'{k!r}: {v!r}\n'
|
||||||
f'txn-id: {k!r}\n'
|
)
|
||||||
f'{k!r}: {v!r}\n'
|
# assert v is not None, f'No valid value for `{k}`!?'
|
||||||
)
|
|
||||||
# assert v is not None, f'No valid value for `{k}`!?'
|
|
||||||
|
|
||||||
if _invalid is not None:
|
if _invalid is not None:
|
||||||
_invalid.append(tx)
|
_invalid.append(tx)
|
||||||
|
|
@ -386,6 +385,7 @@ def open_ledger_dfs(
|
||||||
acctname: str,
|
acctname: str,
|
||||||
|
|
||||||
ledger: TransactionLedger | None = None,
|
ledger: TransactionLedger | None = None,
|
||||||
|
debug_mode: bool = False,
|
||||||
|
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
|
|
@ -400,8 +400,7 @@ def open_ledger_dfs(
|
||||||
can update the ledger on exit.
|
can update the ledger on exit.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from piker.toolz import open_crash_handler
|
with maybe_open_crash_handler(pdb=debug_mode):
|
||||||
with open_crash_handler():
|
|
||||||
if not ledger:
|
if not ledger:
|
||||||
import time
|
import time
|
||||||
from ._ledger import open_trade_ledger
|
from ._ledger import open_trade_ledger
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,18 @@ class SymbologyCache(Struct):
|
||||||
# provided by the backend pkg.
|
# provided by the backend pkg.
|
||||||
mktmaps: dict[str, MktPair] = field(default_factory=dict)
|
mktmaps: dict[str, MktPair] = field(default_factory=dict)
|
||||||
|
|
||||||
|
def pformat(self) -> str:
|
||||||
|
return (
|
||||||
|
f'<{type(self).__name__}(\n'
|
||||||
|
f' .mod: {self.mod!r}\n'
|
||||||
|
f' .assets: {len(self.assets)!r}\n'
|
||||||
|
f' .pairs: {len(self.pairs)!r}\n'
|
||||||
|
f' .mktmaps: {len(self.mktmaps)!r}\n'
|
||||||
|
f')>'
|
||||||
|
)
|
||||||
|
|
||||||
|
__repr__ = pformat
|
||||||
|
|
||||||
def write_config(self) -> None:
|
def write_config(self) -> None:
|
||||||
|
|
||||||
# put the backend's pair-struct type ref at the top
|
# put the backend's pair-struct type ref at the top
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,12 @@ from piker.service import (
|
||||||
from piker.log import get_console_log
|
from piker.log import get_console_log
|
||||||
|
|
||||||
|
|
||||||
|
# include `tractor`'s built-in fixtures!
|
||||||
|
pytest_plugins: tuple[str] = (
|
||||||
|
"tractor._testing.pytest",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
parser.addoption("--ll", action="store", dest='loglevel',
|
parser.addoption("--ll", action="store", dest='loglevel',
|
||||||
default=None, help="logging level to set when testing")
|
default=None, help="logging level to set when testing")
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,14 @@ from piker import config
|
||||||
from piker.accounting import (
|
from piker.accounting import (
|
||||||
Account,
|
Account,
|
||||||
calc,
|
calc,
|
||||||
Position,
|
open_account,
|
||||||
TransactionLedger,
|
|
||||||
open_trade_ledger,
|
|
||||||
load_account,
|
load_account,
|
||||||
load_account_from_ledger,
|
load_account_from_ledger,
|
||||||
|
open_trade_ledger,
|
||||||
|
Position,
|
||||||
|
TransactionLedger,
|
||||||
)
|
)
|
||||||
|
import tractor
|
||||||
|
|
||||||
|
|
||||||
def test_root_conf_networking_section(
|
def test_root_conf_networking_section(
|
||||||
|
|
@ -53,12 +55,17 @@ def test_account_file_default_empty(
|
||||||
)
|
)
|
||||||
def test_paper_ledger_position_calcs(
|
def test_paper_ledger_position_calcs(
|
||||||
fq_acnt: tuple[str, str],
|
fq_acnt: tuple[str, str],
|
||||||
|
debug_mode: bool,
|
||||||
):
|
):
|
||||||
broker: str
|
broker: str
|
||||||
acnt_name: str
|
acnt_name: str
|
||||||
broker, acnt_name = fq_acnt
|
broker, acnt_name = fq_acnt
|
||||||
|
|
||||||
accounts_path: Path = config.repodir() / 'tests' / '_inputs'
|
accounts_path: Path = (
|
||||||
|
config.repodir()
|
||||||
|
/ 'tests'
|
||||||
|
/ '_inputs' # tests-local-subdir
|
||||||
|
)
|
||||||
|
|
||||||
ldr: TransactionLedger
|
ldr: TransactionLedger
|
||||||
with (
|
with (
|
||||||
|
|
@ -77,6 +84,7 @@ def test_paper_ledger_position_calcs(
|
||||||
ledger=ldr,
|
ledger=ldr,
|
||||||
|
|
||||||
_fp=accounts_path,
|
_fp=accounts_path,
|
||||||
|
debug_mode=debug_mode,
|
||||||
|
|
||||||
) as (dfs, ledger),
|
) as (dfs, ledger),
|
||||||
|
|
||||||
|
|
@ -102,3 +110,87 @@ def test_paper_ledger_position_calcs(
|
||||||
df = dfs[xrp]
|
df = dfs[xrp]
|
||||||
assert df['cumsize'][-1] == 0
|
assert df['cumsize'][-1] == 0
|
||||||
assert pos.cumsize == 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.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue