Fix disti-mode paper pps relaying

Turns out we were putting too many brokername suffixes in the symbol
field and thus the order mode msg parser wasn't matching the current
asset to said msgs correctly and pps weren't being shown...

This repairs that plus simplifies the order mode initial pos msg loading
to just delegate into `process_trade_msg()` just as is done for
real-time msg updates.
size_in_shm_token
Tyler Goodlet 2022-08-26 23:41:47 -04:00
parent 02f53d0c13
commit 5c8c5d8fbf
2 changed files with 20 additions and 29 deletions

View File

@ -23,7 +23,6 @@ from contextlib import asynccontextmanager
from datetime import datetime from datetime import datetime
from operator import itemgetter from operator import itemgetter
import itertools import itertools
from pprint import pformat
import time import time
from typing import ( from typing import (
Any, Any,
@ -204,7 +203,7 @@ class PaperBoi(Struct):
async def fake_fill( async def fake_fill(
self, self,
symbol: str, fqsn: str,
price: float, price: float,
size: float, size: float,
action: str, # one of {'buy', 'sell'} action: str, # one of {'buy', 'sell'}
@ -258,34 +257,34 @@ class PaperBoi(Struct):
await self.ems_trades_stream.send(msg) await self.ems_trades_stream.send(msg)
# lookup any existing position # lookup any existing position
token = f'{symbol}.{self.broker}' key = fqsn.rstrip(f'.{self.broker}')
pp = self._positions.setdefault( pp = self._positions.setdefault(
token, fqsn,
Position( Position(
Symbol( Symbol(
key=symbol, key=key,
broker_info={self.broker: {}}, broker_info={self.broker: {}},
), ),
size=size, size=size,
ppu=price, ppu=price,
bsuid=symbol, bsuid=key,
) )
) )
t = Transaction( t = Transaction(
fqsn=symbol, fqsn=fqsn,
tid=oid, tid=oid,
size=size, size=size,
price=price, price=price,
cost=0, # TODO: cost model cost=0, # TODO: cost model
dt=pendulum.from_timestamp(fill_time_s), dt=pendulum.from_timestamp(fill_time_s),
bsuid=symbol, bsuid=key,
) )
pp.add_clear(t) pp.add_clear(t)
pp_msg = BrokerdPosition( pp_msg = BrokerdPosition(
broker=self.broker, broker=self.broker,
account='paper', account='paper',
symbol=symbol, symbol=fqsn,
# TODO: we need to look up the asset currency from # TODO: we need to look up the asset currency from
# broker info. i guess for crypto this can be # broker info. i guess for crypto this can be
# inferred from the pair? # inferred from the pair?
@ -416,7 +415,7 @@ async def simulate_fills(
# clearing price would have filled entirely # clearing price would have filled entirely
await client.fake_fill( await client.fake_fill(
symbol=sym, fqsn=sym,
# todo slippage to determine fill price # todo slippage to determine fill price
price=tick_price, price=tick_price,
size=size, size=size,
@ -443,7 +442,7 @@ async def handle_order_requests(
# error on bad inputs # error on bad inputs
reason = None reason = None
if account != 'paper': if account != 'paper':
reason = f'Paper account only. No account found: `{account}` ?' reason = f'No account found:`{account}` (paper only)?'
elif order.size == 0: elif order.size == 0:
reason = 'Invalid size: 0' reason = 'Invalid size: 0'
@ -543,7 +542,6 @@ async def trades_dialogue(
# TODO: load paper positions per broker from .toml config file # TODO: load paper positions per broker from .toml config file
# and pass as symbol to position data mapping: ``dict[str, dict]`` # and pass as symbol to position data mapping: ``dict[str, dict]``
# await ctx.started(all_positions)
await ctx.started((pp_msgs, ['paper'])) await ctx.started((pp_msgs, ['paper']))
async with ( async with (

View File

@ -642,18 +642,7 @@ async def open_order_mode(
# Pack position messages by account, should only be one-to-one. # Pack position messages by account, should only be one-to-one.
# NOTE: requires the backend exactly specifies # NOTE: requires the backend exactly specifies
# the expected symbol key in its positions msg. # the expected symbol key in its positions msg.
pps_by_account = {} # pps_by_account = {}
for (broker, acctid), msgs in position_msgs.items():
for msg in msgs:
sym = msg['symbol']
if (
(sym == symkey) or (
# mega-UGH, i think we need to fix the FQSN
# stuff sooner then later..
sym == symkey.removesuffix(f'.{broker}'))
):
pps_by_account[acctid] = msg
# update pp trackers with data relayed from ``brokerd``. # update pp trackers with data relayed from ``brokerd``.
for account_name in accounts: for account_name in accounts:
@ -667,11 +656,6 @@ async def open_order_mode(
# XXX: BLEH, do we care about this on the client side? # XXX: BLEH, do we care about this on the client side?
bsuid=symbol, bsuid=symbol,
) )
msg = pps_by_account.get(account_name)
if msg:
log.info(f'Loading pp for {symkey}:\n{pformat(msg)}')
startup_pp.update_from_msg(msg)
# allocator config # allocator config
alloc = mk_allocator( alloc = mk_allocator(
symbol=symbol, symbol=symbol,
@ -789,6 +773,15 @@ async def open_order_mode(
# Begin order-response streaming # Begin order-response streaming
done() done()
for (broker, acctid), msgs in position_msgs.items():
for msg in msgs:
log.info(f'Loading pp for {symkey}:\n{pformat(msg)}')
await process_trade_msg(
mode,
book,
msg,
)
# start async input handling for chart's view # start async input handling for chart's view
async with ( async with (