some refactor

max_pain_deribit
Nelson Torres 2024-12-04 19:18:29 -03:00
parent 5de14bc3f9
commit ec2123175a
1 changed files with 17 additions and 12 deletions

View File

@ -851,10 +851,16 @@ async def aio_open_interest_feed_relay(
} }
index_price: Decimal = data['index_price'] index_price: Decimal = data['index_price']
price_delta: Decimal = abs(index_price - Decimal(strike_price))
open_interest: Decimal = oi.open_interest 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: if not f'{strike_price}' in losses_cache:
losses_cache[f'{strike_price}'] = { losses_cache[f'{strike_price}'] = {
@ -870,29 +876,27 @@ async def aio_open_interest_feed_relay(
losses_cache[f'{strike_price}']['P'] 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}'] = { open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'] = {
'date': oi.timestamp, 'date': oi.timestamp,
'open_interest': open_interest, 'open_interest': open_interest,
'index_price': index_price, 'index_price': index_price,
'strike_price': strike_price, 'strike_price': strike_price,
'price_delta': price_delta, 'price_delta': price_delta,
'notional_value': notional_value,
'losses': losses, # this updates the global value call_losses and put_losses '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. # 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 open_interests[f'{expiry_date}'][f'{strike_price}']['strike_losses'] = strike_losses
for strike in open_interests[f'{expiry_date}']: 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_losses = open_interests[f'{expiry_date}'][strike]['strike_losses']
max_pain = strike max_pain = strike
print('-----------------------------------------------') print(f'>>>> Open Interest...')
print(f'strike_price: {strike_price}') print(f'expiration: {expiry_date}')
print(f'strike_losses: {open_interests[f'{expiry_date}'][strike]['strike_losses']}') print(f'>>>> max_pain: {max_pain}')
print(f'{pformat(open_interests[f'{expiry_date}'][strike])}') print(f'max_losses: {max_losses}')
print(f'{pformat(open_interests[f'{expiry_date}'][max_pain])}')
print('-----------------------------------------------') print('-----------------------------------------------')
@ -923,7 +927,7 @@ async def aio_open_interest_feed_relay(
async def open_oi_feed( async def open_oi_feed(
) -> to_asyncio.LinkedTaskChannel: ) -> to_asyncio.LinkedTaskChannel:
expiry_date: str = '6DEC24' # '6DEC24' '26SEP25' '27JUN25' '13DEC24' expiry_date: str = '20DEC24'
instruments: list[Symbol] = [] instruments: list[Symbol] = []
async with get_client( async with get_client(
) as client: ) as client:
@ -936,6 +940,7 @@ async def open_oi_feed(
'C': Decimal(0), 'C': Decimal(0),
'P': Decimal(0), 'P': Decimal(0),
} }
# max_losses: Decimal = Decimal('Infinity')
max_losses: Decimal = Decimal(0) max_losses: Decimal = Decimal(0)
max_pain: Decimal = Decimal(0) max_pain: Decimal = Decimal(0)
open_interests: dict[str, dict[str, dict[str, list[dict]]]] = {} open_interests: dict[str, dict[str, dict[str, list[dict]]]] = {}