From 77db2fa7c8160311007cb2c45c7f038503b97bb8 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 19 Jun 2023 12:30:43 -0400 Subject: [PATCH] Support loading quarterly futes existing lives Do parsing of the `'symbol'` and check for an `_` suffix, in which case we re-format in capitalized FQME style, do the `Client._pairs[str, Pair]` lookup and then send the `Pair.bs_fqme` in the `Order.fqme: str` field. --- piker/brokers/binance/api.py | 33 ++++++++++++++++++--------------- piker/brokers/binance/broker.py | 8 +++----- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/piker/brokers/binance/api.py b/piker/brokers/binance/api.py index 481c427c..dc3482cc 100644 --- a/piker/brokers/binance/api.py +++ b/piker/brokers/binance/api.py @@ -647,29 +647,32 @@ class Client: signed=True, action='get', ) + # figure out which venue (in FQME terms) we're using + # since that normally maps 1-to-1 with the account (right?) + venue: str = self.mkt_mode.rstrip('_futes') + orders: list[Order] = [] for entry in resp: oid: str = entry['clientOrderId'] + symbol: str = entry['symbol'] - # XXX TODO XXX: it appears as though entries have no - # indicator from the symbology system which market - # / venue the order is from.. which normally isn't - # a huge deal since you could assume based on the - # endpoint you made the request to, BUT the futes USD-M - # endpoints have multiple contracts for the same - # symbols (eg. BTCUSDT.PERP, BTCUSDT.230630.. etc.) - # NOTE: for now until we have a better system we're - # going to assume orders that don't have some kind of - # further info in the order resp dict are perps though - # likely this will need to change in the future.. - venue: str = self.mkt_mode.rstrip('_futes') - bs_mktid: str = entry['symbol'] - fqme: str = f'{bs_mktid.lower()}.{venue}.perp' + # build out a fqme-styled key that should map to a pair + # entry in `._pairs` cross-venue table. + bs_mktid, _, expiry = entry['symbol'].partition('_') + bs_mktid += f'.{venue.upper()}' + + if expiry: + bs_mktid += f'.{expiry}' + else: + bs_mktid += '.PERP' + + # should never key error if we've got it right B) + pair: Pair = self._pairs[bs_mktid] orders.append( Order( oid=oid, - symbol=fqme, + symbol=pair.bs_fqme.lower(), action=entry['side'].lower(), price=float(entry['price']), diff --git a/piker/brokers/binance/broker.py b/piker/brokers/binance/broker.py index 6011aa9d..77670816 100644 --- a/piker/brokers/binance/broker.py +++ b/piker/brokers/binance/broker.py @@ -373,11 +373,9 @@ async def open_trade_dialog( ctx.open_stream() as ems_stream, ): # deliver all pre-exist open orders to EMS thus syncing - # state with the binance existing live limit set. - open_orders: list[Order] = await client.get_open_orders() - - # fill out `Status` with boxed `Order`s and sync the EMS. - for order in open_orders: + # state with existing live limits reported by them. + order: Order + for order in await client.get_open_orders(): status_msg = Status( time_ns=time.time_ns(), resp='open',