Allow `brokerd` runtime switch to paper mode
Previously you couldn't have a brokerd backend which defined `.trades_dialogue()` but which could also indicate that the paper clearing engine should be used. This adds that support by allowing the endpoint task to return a simple `'paper'` string, in which case the ems will boot a paperboi. The obvious useful case for this is if you have a broker you want to use but do not have actual broker credentials setup (yet) with that provider in your `brokers.toml`; demonstrated here with the adjustment to `kraken`'s startup to no longer raise a runtime error B)master
parent
361fc4645c
commit
769b292dca
|
@ -429,8 +429,8 @@ async def trades_dialogue(
|
||||||
async with get_client() as client:
|
async with get_client() as client:
|
||||||
|
|
||||||
if not client._api_key:
|
if not client._api_key:
|
||||||
raise RuntimeError(
|
await ctx.started('paper')
|
||||||
'Missing Kraken API key in `brokers.toml`!?!?')
|
return
|
||||||
|
|
||||||
# TODO: make ems flip to paper mode via
|
# TODO: make ems flip to paper mode via
|
||||||
# some returned signal if the user only wants to use
|
# some returned signal if the user only wants to use
|
||||||
|
|
|
@ -413,8 +413,6 @@ async def stream_quotes(
|
||||||
topic, quote = normalize(ohlc_last)
|
topic, quote = normalize(ohlc_last)
|
||||||
|
|
||||||
task_status.started((init_msgs, quote))
|
task_status.started((init_msgs, quote))
|
||||||
|
|
||||||
# lol, only "closes" when they're margin squeezing clients ;P
|
|
||||||
feed_is_live.set()
|
feed_is_live.set()
|
||||||
|
|
||||||
# keep start of last interval for volume tracking
|
# keep start of last interval for volume tracking
|
||||||
|
|
|
@ -407,11 +407,7 @@ class Router(Struct):
|
||||||
yield relay
|
yield relay
|
||||||
return
|
return
|
||||||
|
|
||||||
trades_endpoint = getattr(brokermod, 'trades_dialogue', None)
|
def mk_paper_ep():
|
||||||
if (
|
|
||||||
trades_endpoint is None
|
|
||||||
or exec_mode == 'paper'
|
|
||||||
):
|
|
||||||
# for logging purposes
|
# for logging purposes
|
||||||
brokermod = paper
|
brokermod = paper
|
||||||
|
|
||||||
|
@ -426,26 +422,53 @@ class Router(Struct):
|
||||||
# load the paper trading engine as a subactor of this emsd
|
# load the paper trading engine as a subactor of this emsd
|
||||||
# actor to simulate the real IPC load it'll have when also
|
# actor to simulate the real IPC load it'll have when also
|
||||||
# pulling data from feeds
|
# pulling data from feeds
|
||||||
open_trades_endpoint = paper.open_paperboi(
|
return paper.open_paperboi(
|
||||||
fqme=fqme,
|
fqme=fqme,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
trades_endpoint = getattr(brokermod, 'trades_dialogue', None)
|
||||||
|
if (
|
||||||
|
trades_endpoint is not None
|
||||||
|
or exec_mode != 'paper'
|
||||||
|
):
|
||||||
# open live brokerd trades endpoint
|
# open live brokerd trades endpoint
|
||||||
open_trades_endpoint = portal.open_context(
|
open_trades_endpoint = portal.open_context(
|
||||||
trades_endpoint,
|
trades_endpoint,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
)
|
)
|
||||||
|
|
||||||
# open trades-dialog endpoint with backend broker
|
else:
|
||||||
|
exec_mode: str = 'paper'
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def maybe_open_paper_ep():
|
||||||
|
if exec_mode == 'paper':
|
||||||
|
async with mk_paper_ep() as msg:
|
||||||
|
yield msg
|
||||||
|
return
|
||||||
|
|
||||||
|
# open trades-dialog endpoint with backend broker
|
||||||
|
async with open_trades_endpoint as msg:
|
||||||
|
ctx, first = msg
|
||||||
|
|
||||||
|
# runtime indication that the backend can't support live
|
||||||
|
# order ctrl yet, so boot the paperboi B0
|
||||||
|
if first == 'paper':
|
||||||
|
async with mk_paper_ep() as msg:
|
||||||
|
yield msg
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
# working live ep case B)
|
||||||
|
yield msg
|
||||||
|
return
|
||||||
|
|
||||||
positions: list[BrokerdPosition]
|
positions: list[BrokerdPosition]
|
||||||
accounts: tuple[str]
|
accounts: tuple[str]
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
open_trades_endpoint as (
|
maybe_open_paper_ep() as (
|
||||||
brokerd_ctx,
|
brokerd_ctx,
|
||||||
(positions, accounts,),
|
(positions, accounts),
|
||||||
),
|
),
|
||||||
brokerd_ctx.open_stream() as brokerd_trades_stream,
|
brokerd_ctx.open_stream() as brokerd_trades_stream,
|
||||||
):
|
):
|
||||||
|
|
Loading…
Reference in New Issue