diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 22d78c67..99325f7d 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -793,6 +793,9 @@ async def aio_open_interest_feed_relay( fh: FeedHandler, instruments: list, open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]], + losses_cache: dict[str, Decimal], + max_losses: Decimal, + max_pain: Decimal, from_trio: asyncio.Queue, to_trio: trio.abc.SendChannel, ) -> None: @@ -817,12 +820,13 @@ async def aio_open_interest_feed_relay( Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side. ''' + nonlocal losses_cache + nonlocal max_losses + nonlocal max_pain nonlocal open_interests - print('>>>> Open Interest...') - print(oi) symbol: Symbol = str_to_cb_sym(oi.symbol) piker_sym: str = cb_sym_to_deribit_inst(symbol) - print(f'{piker_sym}') + data: dict = oi.raw['params']['data'] ( base, expiry_date, @@ -836,24 +840,65 @@ async def aio_open_interest_feed_relay( f'{strike_price}': { 'C': [], 'P': [], + 'strike_losses': Decimal(0), } } if not f'{strike_price}' in open_interests[f'{expiry_date}']: open_interests[f'{expiry_date}'][f'{strike_price}'] = { 'C': [], 'P': [], + 'strike_losses': Decimal(0), } - open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'].append( - {f'{oi.timestamp}': oi.open_interest} + index_price: Decimal = data['index_price'] + price_delta: Decimal = abs(index_price - Decimal(strike_price)) + open_interest: Decimal = oi.open_interest + + losses: Decimal = price_delta * open_interest + + if not f'{strike_price}' in losses_cache: + losses_cache[f'{strike_price}'] = { + 'C': Decimal(0), + 'P': Decimal(0), + } + + losses_cache[f'{strike_price}'][f'{option_type}'] = losses + + strike_losses: Decimal = ( + losses_cache[f'{strike_price}']['C'] + + + losses_cache[f'{strike_price}']['P'] ) - print(f'open_interests:') - print(f'{open_interests}') - print(f'open_interests expiry dates: {len(open_interests)}') - to_trio.send_nowait(('oi', oi)) + + print(f'>>>> Open Interest...') + print(f'max_losses: {max_losses}\n') + print(f'max_pain: {max_pain}') + print('-----------------------------------------------') + open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'] = { + 'date': oi.timestamp, + 'open_interest': open_interest, + 'index_price': index_price, + 'strike_price': strike_price, + 'price_delta': price_delta, + 'losses': losses, # this updates the global value call_losses and put_losses + } + # calculate with latest values stored in call_losses and put_losses global cache. + open_interests[f'{expiry_date}'][f'{strike_price}']['strike_losses'] = strike_losses + + for strike in open_interests[f'{expiry_date}']: + if open_interests[f'{expiry_date}'][strike]['strike_losses'] > max_losses: + max_losses = open_interests[f'{expiry_date}'][strike]['strike_losses'] + max_pain = strike + print('-----------------------------------------------') + print(f'strike_price: {strike_price}') + print(f'strike_losses: {open_interests[f'{expiry_date}'][strike]['strike_losses']}') + print(f'{pformat(open_interests[f'{expiry_date}'][strike])}') + print('-----------------------------------------------') + channels = [TRADES, OPEN_INTEREST] - callbacks = {TRADES: _trade, OPEN_INTEREST: _oi} + callbacks={TRADES: _trade, OPEN_INTEREST: _oi} + fh.add_feed( DERIBIT, channels=channels, @@ -878,16 +923,22 @@ async def aio_open_interest_feed_relay( async def open_oi_feed( ) -> to_asyncio.LinkedTaskChannel: + expiry_date: str = '6DEC24' # '6DEC24' '26SEP25' '27JUN25' '13DEC24' 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( - expiry_date='6DEC24' + expiry_date=expiry_date, ) - - open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]] = {} + losses_cache: dict[str, Decimal] = { # {'': } + 'C': Decimal(0), + 'P': Decimal(0), + } + max_losses: Decimal = Decimal(0) + max_pain: Decimal = Decimal(0) + open_interests: dict[str, dict[str, dict[str, list[dict]]]] = {} fh: FeedHandler first: None chan: to_asyncio.LinkedTaskChannel @@ -899,6 +950,9 @@ async def open_oi_feed( fh, instruments, open_interests, + losses_cache, + max_losses, + max_pain, ) ) as (first, chan) ):