Avoid crash on trades ledger msgs

Just ignore them for now using new `match:` syntax B)
but we'll do incremental update sooon!

Resolves #311
notokeninwswrapper
Tyler Goodlet 2022-06-29 17:24:38 -04:00
parent 9106d13dfe
commit fcd7e0f3f3
1 changed files with 56 additions and 51 deletions

View File

@ -70,6 +70,7 @@ class Trade(BaseModel):
def pack_positions( def pack_positions(
acc: str, acc: str,
trades: dict trades: dict
) -> list[Any]: ) -> list[Any]:
positions: dict[str, float] = {} positions: dict[str, float] = {}
vols: dict[str, float] = {} vols: dict[str, float] = {}
@ -342,11 +343,13 @@ async def trades_dialogue(
# TODO: maybe add multiple accounts # TODO: maybe add multiple accounts
n.start_soon(handle_order_requests, client, ems_stream) n.start_soon(handle_order_requests, client, ems_stream)
# pull and deliver trades ledger
acc_name = 'kraken.' + client._name acc_name = 'kraken.' + client._name
trades = await client.get_trades() trades = await client.get_trades()
log.info(
f'Loaded {len(trades)} trades from account `{acc_name}`'
)
position_msgs = pack_positions(acc_name, trades) position_msgs = pack_positions(acc_name, trades)
await ctx.started((position_msgs, (acc_name,))) await ctx.started((position_msgs, (acc_name,)))
# Get websocket token for authenticated data stream # Get websocket token for authenticated data stream
@ -355,33 +358,31 @@ async def trades_dialogue(
# lol wtf is this.. # lol wtf is this..
assert resp['error'] == [] assert resp['error'] == []
token = resp['result']['token'] token = resp['result']['token']
async with ( async with (
ctx.open_stream() as ems_stream, ctx.open_stream() as ems_stream,
trio.open_nursery() as n, open_autorecon_ws(
):
# TODO: maybe add multiple accounts
n.start_soon(handle_order_requests, client, ems_stream)
# Process trades msg stream of ws
async with open_autorecon_ws(
'wss://ws-auth.kraken.com/', 'wss://ws-auth.kraken.com/',
fixture=subscribe, fixture=subscribe,
token=token, token=token,
) as ws: ) as ws,
trio.open_nursery() as n,
):
# task for processing inbound requests from ems
n.start_soon(handle_order_requests, client, ems_stream)
# begin trade event processing
async for msg in process_trade_msgs(ws): async for msg in process_trade_msgs(ws):
for trade in msg: for trade in msg:
# check the type of packaged message match trade:
assert type(trade) == Trade
# prepare and send a filled status update # prepare and send a filled status update
case Trade():
filled_msg = BrokerdStatus( filled_msg = BrokerdStatus(
reqid=trade.reqid, reqid=trade.reqid,
time_ns=time.time_ns(), time_ns=time.time_ns(),
account='kraken.spot', account=acc_name,
status='filled', status='filled',
filled=float(trade.size), filled=float(trade.size),
reason='Order filled by kraken', reason='Order filled by kraken',
@ -399,7 +400,6 @@ async def trades_dialogue(
# https://github.com/pikers/piker/issues/296 # https://github.com/pikers/piker/issues/296
remaining=0, remaining=0,
) )
await ems_stream.send(filled_msg.dict()) await ems_stream.send(filled_msg.dict())
# send a fill msg for gui update # send a fill msg for gui update
@ -417,12 +417,17 @@ async def trades_dialogue(
await ems_stream.send(fill_msg.dict()) await ems_stream.send(fill_msg.dict())
case _:
log.warning(f'Unhandled trades msg: {trade}')
await tractor.breakpoint()
async def process_trade_msgs( async def process_trade_msgs(
ws: NoBsWs, ws: NoBsWs,
): ):
''' '''
Parse and pack data feed messages. Parse and pack trades subscription messages, deliver framed
sequences of messages?
''' '''
sequence_counter = 0 sequence_counter = 0