From a9c016ba103d9b4a57de5be084bdfa08d36af62e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 17 Jun 2023 13:26:20 -0400 Subject: [PATCH] Use `Client._pairs` cross-venue table for orders Since the request handler task will work concurrently across venues (spot, futes, margin) we need to be sure that we look up the correct venue to update the order dialog and this is naturally determined by the FQME-style symbol in the `BrokerdOrder` msg; the best way to map that symbol-key to the correct venue/`Pair` is by using said `._pairs: ChainMap`. Further, handle limit order errors by catching and relaying back an error response to the EMS. Fix the "account name" to be `binance.usdtm` so that we can eventually and explicitly support all venues by name. --- piker/brokers/binance/broker.py | 51 ++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/piker/brokers/binance/broker.py b/piker/brokers/binance/broker.py index a9f0a0a9..2eff326f 100644 --- a/piker/brokers/binance/broker.py +++ b/piker/brokers/binance/broker.py @@ -48,6 +48,7 @@ from piker.data._web_bs import ( ) from piker.brokers import ( open_cached_client, + BrokerError, ) from piker.clearing._messages import ( BrokerdOrder, @@ -91,7 +92,8 @@ async def handle_order_requests( ) await ems_order_stream.send(BrokerdError( oid=cancel.oid, - symbol=cancel.symbol, + # TODO: do we need the symbol? + symbol='unknown', reason=( 'Invalid `binance` order request dialog oid', ) @@ -100,12 +102,12 @@ async def handle_order_requests( else: await client.submit_cancel( - cancel.symbol, + existing.symbol, cancel.oid, ) case { - 'account': ('binance.futes' | 'binance.spot') as account, + 'account': ('binance.usdtm' | 'binance.spot') as account, 'action': action, } if action in {'buy', 'sell'}: @@ -131,19 +133,34 @@ async def handle_order_requests( ) await ems_order_stream.send(resp) - # call our client api to submit the order - reqid = await client.submit_limit( - symbol=order.symbol, - side=order.action, - quantity=order.size, - price=order.price, - oid=order.oid - ) - # thank god at least someone lets us do this XD - assert reqid == order.oid + # lookup the binance-native symbol + bs_mktid: str = client._pairs[order.symbol.upper()].symbol - # track latest request state - dialogs[reqid].maps.append(msg) + # call our client api to submit the order + try: + reqid = await client.submit_limit( + symbol=bs_mktid, + side=order.action, + quantity=order.size, + price=order.price, + oid=order.oid + ) + # thank god at least someone lets us do this XD + assert reqid == order.oid + + # track latest request state + dialogs[reqid].maps.append(msg) + except BrokerError as be: + await ems_order_stream.send( + BrokerdError( + oid=msg['oid'], + symbol=msg['symbol'], + reason=( + '`binance` request failed:\n' + f'{be}' + )) + ) + continue case _: account = msg.get('account') @@ -157,7 +174,7 @@ async def handle_order_requests( oid=msg['oid'], symbol=msg['symbol'], reason=( - 'Invalid `binance` broker request msg:\n{msg}' + f'Invalid `binance` broker request msg:\n{msg}' )) ) @@ -230,7 +247,7 @@ async def open_trade_dialog( # 'feeTier': 0} if 'account' in req: alias: str = resp['accountAlias'] - accounts['binance.usdtm_futes'] = alias + accounts['binance.usdtm'] = alias # @balance response: # {'accountAlias': 'sRFzFzAuuXsR',