Change `ledger` CLI to use new `open_brokerd_dialog()`

Instead of effectively (and poorly) duplicating the trade dialog setup
logic, just use the new helper we exposed in the EMS module B)
Also, handle paper accounts that have no ledger / positions existing.
basic_buy_bot
Tyler Goodlet 2023-06-12 20:23:01 -04:00
parent b15e736e3e
commit bb02775cab
1 changed files with 105 additions and 87 deletions

View File

@ -19,8 +19,9 @@ CLI front end for trades ledger and position tracking management.
''' '''
from typing import ( from typing import (
Any, AsyncContextManager,
) )
from types import ModuleType
from rich.console import Console from rich.console import Console
from rich.markdown import Markdown from rich.markdown import Markdown
@ -45,7 +46,11 @@ def broker_init(
**start_actor_kwargs, **start_actor_kwargs,
) -> dict: ) -> tuple[
ModuleType,
dict,
AsyncContextManager,
]:
''' '''
Given an input broker name, load all named arguments Given an input broker name, load all named arguments
which can be passed to a daemon + context spawn for which can be passed to a daemon + context spawn for
@ -83,13 +88,9 @@ def broker_init(
from ..brokers._daemon import _setup_persistent_brokerd from ..brokers._daemon import _setup_persistent_brokerd
return ( return (
start_actor_kwargs, # to `ActorNursery.start_actor()`
_setup_persistent_brokerd, # service task ep
getattr( # trades endpoint
brokermod, brokermod,
'trades_dialogue', start_actor_kwargs, # to `ActorNursery.start_actor()`
None, _setup_persistent_brokerd, # deamon service task ep
),
) )
@ -119,10 +120,11 @@ def sync(
console.print(md) console.print(md)
return return
start_kwargs, _, trades_ep = broker_init( brokermod, start_kwargs, deamon_ep = broker_init(
brokername, brokername,
loglevel=loglevel, loglevel=loglevel,
) )
brokername: str = brokermod.name
async def main(): async def main():
@ -136,6 +138,7 @@ def sync(
tractor.open_nursery() as an, tractor.open_nursery() as an,
): ):
try:
log.info( log.info(
f'Piker runtime up as {actor.uid}@{sockaddr}' f'Piker runtime up as {actor.uid}@{sockaddr}'
) )
@ -146,42 +149,53 @@ def sync(
**start_kwargs, **start_kwargs,
) )
if ( from ..clearing import (
brokername == 'paper' open_brokerd_dialog,
or trades_ep is None
):
from ..clearing import _paper_engine as paper
open_trades_endpoint = paper.open_paperboi(
fqme=None, # tell paper to not start clearing loop
broker=brokername,
loglevel=loglevel,
)
else:
# open live brokerd trades endpoint
open_trades_endpoint = portal.open_context(
trades_ep,
loglevel=loglevel,
) )
brokerd_stream: tractor.MsgStream
positions: dict[str, Any] async with open_brokerd_dialog(
accounts: list[str] brokermod,
async with ( portal,
open_trades_endpoint as ( exec_mode=(
brokerd_ctx, 'paper' if account == 'paper'
(positions, accounts), else 'live'
), ),
loglevel=loglevel,
) as (
brokerd_stream,
pp_msg_table,
accounts,
): ):
try:
assert len(accounts) == 1 assert len(accounts) == 1
if (
not pp_msg_table
and account == 'paper'
):
console.print(
'[yellow underline]'
f'No pps found for `{brokername}.paper` account!\n'
'Do you even have any paper ledger files?'
)
return
pps_by_symbol: dict[str, BrokerdPosition] = pp_msg_table[
brokername,
account,
]
summary: str = ( summary: str = (
'[dim underline]Piker Position Summary[/] ' '[dim underline]Piker Position Summary[/] '
f'[dim blue underline]{brokername}[/]' f'[dim blue underline]{brokername}[/]'
'[dim].[/]' '[dim].[/]'
f'[blue underline]{account}[/]' f'[blue underline]{account}[/]'
f'[dim underline] -> total pps: [/]' f'[dim underline] -> total pps: [/]'
f'[green]{len(positions)}[/]\n' f'[green]{len(pps_by_symbol)}[/]\n'
) )
for ppdict in positions: # for ppdict in positions:
ppmsg = BrokerdPosition(**ppdict) for fqme, ppmsg in pps_by_symbol.items():
# ppmsg = BrokerdPosition(**ppdict)
size = ppmsg.size size = ppmsg.size
if size: if size:
ppu: float = round( ppu: float = round(
@ -217,14 +231,18 @@ def sync(
console.print(summary) console.print(summary)
finally:
# exit via ctx cancellation. # exit via ctx cancellation.
brokerd_ctx: tractor.Context = brokerd_stream._ctx
await brokerd_ctx.cancel(timeout=1) await brokerd_ctx.cancel(timeout=1)
# TODO: once ported to newer tractor branch we should # TODO: once ported to newer tractor branch we should
# be able to do a loop like this: # be able to do a loop like this:
# while brokerd_ctx.cancel_called_remote is None: # while brokerd_ctx.cancel_called_remote is None:
# await trio.sleep(0.01) # await trio.sleep(0.01)
# await brokerd_ctx.cancel() # await brokerd_ctx.cancel()
finally:
await portal.cancel_actor() await portal.cancel_actor()
trio.run(main) trio.run(main)