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] = {}
@ -104,8 +105,8 @@ def pack_positions(
async def handle_order_requests( async def handle_order_requests(
client: Client, client: Client,
ems_order_stream: tractor.MsgStream, ems_order_stream: tractor.MsgStream,
) -> None: ) -> None:
@ -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,74 +358,76 @@ 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,
async for msg in process_trade_msgs(ws): trio.open_nursery() as n,
for trade in msg: ):
# check the type of packaged message # task for processing inbound requests from ems
assert type(trade) == Trade n.start_soon(handle_order_requests, client, ems_stream)
# begin trade event processing
async for msg in process_trade_msgs(ws):
for trade in msg:
match trade:
# prepare and send a filled status update # prepare and send a filled status update
filled_msg = BrokerdStatus( case Trade():
reqid=trade.reqid, filled_msg = BrokerdStatus(
time_ns=time.time_ns(), reqid=trade.reqid,
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',
broker_details={ broker_details={
'name': 'kraken', 'name': 'kraken',
'broker_time': trade.broker_time 'broker_time': trade.broker_time
}, },
# TODO: figure out if kraken gives a count # TODO: figure out if kraken gives a count
# of how many units of underlying were # of how many units of underlying were
# filled. Alternatively we can decrement # filled. Alternatively we can decrement
# this value ourselves by associating and # this value ourselves by associating and
# calcing from the diff with the original # calcing from the diff with the original
# client-side request, see: # client-side request, see:
# 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
fill_msg = BrokerdFill(
reqid=trade.reqid,
time_ns=time.time_ns(),
# send a fill msg for gui update action=trade.action,
fill_msg = BrokerdFill( size=float(trade.size),
reqid=trade.reqid, price=float(trade.price),
time_ns=time.time_ns(), # TODO: maybe capture more msg data i.e fees?
broker_details={'name': 'kraken'},
broker_time=float(trade.broker_time)
)
action=trade.action, await ems_stream.send(fill_msg.dict())
size=float(trade.size),
price=float(trade.price),
# TODO: maybe capture more msg data i.e fees?
broker_details={'name': 'kraken'},
broker_time=float(trade.broker_time)
)
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