added aux method for update and check if completed
parent
f26a42e76f
commit
1ac643018a
56
max_pain.py
56
max_pain.py
|
@ -11,6 +11,15 @@ from piker.brokers.deribit.api import (
|
|||
maybe_open_oi_feed,
|
||||
)
|
||||
|
||||
def check_if_complete(
|
||||
oi: dict[str, dict[str, Decimal | None]]
|
||||
) -> bool:
|
||||
return all(
|
||||
oi[strike]['C'] is not None
|
||||
and
|
||||
oi[strike]['P'] is not None for strike in oi
|
||||
)
|
||||
|
||||
|
||||
async def max_pain_daemon(
|
||||
) -> None:
|
||||
|
@ -18,19 +27,17 @@ async def max_pain_daemon(
|
|||
instruments: list[Symbol] = []
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||
|
||||
def check_if_complete(
|
||||
oi: dict[str, dict[str, Decimal | None]],
|
||||
|
||||
) -> bool:
|
||||
for strike in oi:
|
||||
if (
|
||||
oi[strike]['C'] == None
|
||||
or
|
||||
oi[strike]['P'] == None
|
||||
):
|
||||
return False
|
||||
|
||||
return True
|
||||
def update_oi_by_strikes(msg: tuple):
|
||||
nonlocal oi_by_strikes
|
||||
if 'oi' == msg[0]:
|
||||
strike_price = msg[1]['strike_price']
|
||||
option_type = msg[1]['option_type']
|
||||
open_interest = msg[1]['open_interest']
|
||||
oi_by_strikes.setdefault(
|
||||
strike_price, {}
|
||||
).update(
|
||||
{option_type: open_interest}
|
||||
)
|
||||
|
||||
def get_max_pain(
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||
|
@ -92,21 +99,18 @@ async def max_pain_daemon(
|
|||
instruments,
|
||||
) as oi_feed:
|
||||
async for msg in oi_feed:
|
||||
if 'oi' == msg[0]:
|
||||
timestamp = msg[1]['timestamp']
|
||||
strike_price = msg[1]['strike_price']
|
||||
option_type = msg[1]['option_type']
|
||||
open_interest = msg[1]['open_interest']
|
||||
oi_by_strikes[f'{strike_price}'][f'{option_type}'] = open_interest
|
||||
|
||||
update_oi_by_strikes(msg)
|
||||
if check_if_complete(oi_by_strikes):
|
||||
max_pain = get_max_pain(oi_by_strikes)
|
||||
print('-----------------------------------------------')
|
||||
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
||||
print(f'expiry_date: {max_pain['expiry_date']}')
|
||||
print(f'max_pain: {max_pain['max_pain']}')
|
||||
print(f'total intrinsic value: {max_pain['total_intrinsic_value']}')
|
||||
print('-----------------------------------------------')
|
||||
if 'oi' == msg[0]:
|
||||
timestamp = msg[1]['timestamp']
|
||||
max_pain = get_max_pain(oi_by_strikes)
|
||||
print('-----------------------------------------------')
|
||||
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
||||
print(f'expiry_date: {max_pain['expiry_date']}')
|
||||
print(f'max_pain: {max_pain['max_pain']}')
|
||||
print(f'total intrinsic value: {max_pain['total_intrinsic_value']}')
|
||||
print('-----------------------------------------------')
|
||||
|
||||
|
||||
async def main():
|
||||
|
|
Loading…
Reference in New Issue