From ec2123175ad784526880838d10ccc030fff97a62 Mon Sep 17 00:00:00 2001 From: Nelson Torres Date: Wed, 4 Dec 2024 19:18:29 -0300 Subject: [PATCH] some refactor --- piker/brokers/deribit/api.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 99325f7d..07aed466 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -851,10 +851,16 @@ async def aio_open_interest_feed_relay( } index_price: Decimal = data['index_price'] - price_delta: Decimal = abs(index_price - Decimal(strike_price)) open_interest: Decimal = oi.open_interest + price_delta: Decimal + if f'{option_type}' == 'C': + price_delta = index_price - Decimal(strike_price) - losses: Decimal = price_delta * open_interest + elif f'{option_type}' == 'P': + price_delta = Decimal(strike_price) - index_price + + notional_value = open_interest * index_price + losses: Decimal = max(0, price_delta * open_interest) if not f'{strike_price}' in losses_cache: losses_cache[f'{strike_price}'] = { @@ -870,29 +876,27 @@ async def aio_open_interest_feed_relay( losses_cache[f'{strike_price}']['P'] ) - 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, + 'notional_value': notional_value, '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: + if 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(f'>>>> Open Interest...') + print(f'expiration: {expiry_date}') + print(f'>>>> max_pain: {max_pain}') + print(f'max_losses: {max_losses}') + print(f'{pformat(open_interests[f'{expiry_date}'][max_pain])}') print('-----------------------------------------------') @@ -923,7 +927,7 @@ async def aio_open_interest_feed_relay( async def open_oi_feed( ) -> to_asyncio.LinkedTaskChannel: - expiry_date: str = '6DEC24' # '6DEC24' '26SEP25' '27JUN25' '13DEC24' + expiry_date: str = '20DEC24' instruments: list[Symbol] = [] async with get_client( ) as client: @@ -936,6 +940,7 @@ async def open_oi_feed( 'C': Decimal(0), 'P': Decimal(0), } +# max_losses: Decimal = Decimal('Infinity') max_losses: Decimal = Decimal(0) max_pain: Decimal = Decimal(0) open_interests: dict[str, dict[str, dict[str, list[dict]]]] = {}