diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 86e6e0f4..686a42a8 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -788,6 +788,7 @@ async def maybe_open_price_feed( async def aio_open_interest_feed_relay( fh: FeedHandler, instruments: list, + open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]], from_trio: asyncio.Queue, to_trio: trio.abc.SendChannel, ) -> None: @@ -799,12 +800,6 @@ async def aio_open_interest_feed_relay( Proxy-thru `cryptofeed.FeedHandler` "trades" to `piker`-side. ''' - # Get timestamp and convert it to isoformat - date = (datetime.utcfromtimestamp(trade.timestamp)).isoformat() - print('Trade...') - print(date) - print(trade) - print('=======================') to_trio.send_nowait(('trade', trade)) # trade and oi are user defined functions that @@ -818,18 +813,46 @@ async def aio_open_interest_feed_relay( Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side. ''' - # Get timestamp and convert it to isoformat - date = (datetime.utcfromtimestamp(oi.timestamp)).isoformat() + nonlocal open_interests print('>>>> Open Interest...') - print(date) print(oi) - print('==========================') + symbol: Symbol = str_to_cb_sym(oi.symbol) + piker_sym: str = cb_sym_to_deribit_inst(symbol) + print(f'{piker_sym}') + ( + base, + expiry_date, + strike_price, + option_type + ) = tuple( + piker_sym.split('-') + ) + if not f'{expiry_date}' in open_interests: + open_interests[f'{expiry_date}'] = { + f'{strike_price}': { + 'C': [], + 'P': [], + } + } + if not f'{strike_price}' in open_interests[f'{expiry_date}']: + open_interests[f'{expiry_date}'][f'{strike_price}'] = { + 'C': [], + 'P': [], + } + + open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'].append( + {f'{oi.timestamp}': oi.open_interest} + ) + print(f'open_interests:') + print(f'{open_interests}') + print(f'open_interests expiry dates: {len(open_interests)}') to_trio.send_nowait(('oi', oi)) + channels = [TRADES, OPEN_INTEREST] callbacks = {TRADES: _trade, OPEN_INTEREST: _oi} fh.add_feed( DERIBIT, - channels=[TRADES, OPEN_INTEREST], + channels=channels, symbols=instruments, callbacks=callbacks ) @@ -851,13 +874,14 @@ async def aio_open_interest_feed_relay( async def open_oi_feed( ) -> to_asyncio.LinkedTaskChannel: - instruments: list[Symbol] + instruments: list[Symbol] = [] async with get_client( ) as client: # to get all currencies available in deribit # currencies = await client.get_currencies() instruments = await client.get_instruments() + open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]] = {} fh: FeedHandler first: None chan: to_asyncio.LinkedTaskChannel @@ -867,7 +891,8 @@ async def open_oi_feed( partial( aio_open_interest_feed_relay, fh, - instruments + instruments, + open_interests, ) ) as (first, chan) ):