From dafd5a3ca545624b5b863f73b7f69eb345afb8ed Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 19 Nov 2024 21:14:11 -0500 Subject: [PATCH] Bit more `cryptofeed` adapter formatting and typing for clarity.. --- piker/brokers/deribit/api.py | 106 ++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 0450b9eb..5945e634 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -123,14 +123,20 @@ def str_to_cb_sym(name: str) -> Symbol: type=OPTION, strike_price=strike_price, option_type=option_type, - expiry_date=new_expiry_date) + expiry_date=new_expiry_date + ) def piker_sym_to_cb_sym(name: str) -> Symbol: - base, expiry_date, strike_price, option_type = tuple( + ( + base, + expiry_date, + strike_price, + option_type, + )= tuple( name.upper().split('-')) - quote = base + quote: str = base if option_type == 'P': option_type = PUT @@ -145,7 +151,8 @@ def piker_sym_to_cb_sym(name: str) -> Symbol: type=OPTION, strike_price=strike_price, option_type=option_type, - expiry_date=expiry_date) + expiry_date=expiry_date + ) def cb_sym_to_deribit_inst(sym: Symbol): @@ -208,7 +215,10 @@ def get_config() -> dict[str, Any]: class Client: + ''' + Hi-level interface for the jsron-RPC over websocket API. + ''' def __init__( self, @@ -609,43 +619,59 @@ async def aio_price_feed_relay( from_trio: asyncio.Queue, to_trio: trio.abc.SendChannel, ) -> None: - async def _trade(data: dict, receipt_timestamp): - to_trio.send_nowait(('trade', { - 'symbol': cb_sym_to_deribit_inst( - str_to_cb_sym(data.symbol)).lower(), - 'last': data, - 'broker_ts': time.time(), - 'data': data.to_dict(), - 'receipt': receipt_timestamp - })) - async def _l1(data: dict, receipt_timestamp): - to_trio.send_nowait(('l1', { - 'symbol': cb_sym_to_deribit_inst( - str_to_cb_sym(data.symbol)).lower(), - 'ticks': [ - { - 'type': 'bid', - 'price': float(data.bid_price), - 'size': float(data.bid_size) - }, - { - 'type': 'bsize', - 'price': float(data.bid_price), - 'size': float(data.bid_size) - }, - { - 'type': 'ask', - 'price': float(data.ask_price), - 'size': float(data.ask_size) - }, - { - 'type': 'asize', - 'price': float(data.ask_price), - 'size': float(data.ask_size) - } - ] - })) + async def _trade( + data: dict, + receipt_timestamp: int, + ) -> None: + ''' + Send `cryptofeed.FeedHandler` quotes to `piker`-side + `trio.Task`. + + ''' + to_trio.send_nowait(( + 'trade', { + 'symbol': cb_sym_to_deribit_inst( + str_to_cb_sym(data.symbol)).lower(), + 'last': data, + 'broker_ts': time.time(), + 'data': data.to_dict(), + 'receipt': receipt_timestamp, + }, + )) + + async def _l1( + data: dict, + receipt_timestamp: int, + ) -> None: + to_trio.send_nowait(( + 'l1', { + 'symbol': cb_sym_to_deribit_inst( + str_to_cb_sym(data.symbol)).lower(), + 'ticks': [ + { + 'type': 'bid', + 'price': float(data.bid_price), + 'size': float(data.bid_size) + }, + { + 'type': 'bsize', + 'price': float(data.bid_price), + 'size': float(data.bid_size) + }, + { + 'type': 'ask', + 'price': float(data.ask_price), + 'size': float(data.ask_size) + }, + { + 'type': 'asize', + 'price': float(data.ask_price), + 'size': float(data.ask_size) + } + ] + }, + )) sym: Symbol = piker_sym_to_cb_sym(instrument) fh.add_feed( DERIBIT,