Compare commits
2 Commits
f26a42e76f
...
3526d5d564
Author | SHA1 | Date |
---|---|---|
Nelson Torres | 3526d5d564 | |
Nelson Torres | 1ac643018a |
66
max_pain.py
66
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]]
|
||||
|
@ -50,17 +57,15 @@ async def max_pain_daemon(
|
|||
# an amount greater than zero.
|
||||
total_intrinsic_value: Decimal = Decimal('Infinity')
|
||||
max_pain: Decimal = Decimal(0)
|
||||
|
||||
intrinsic_values: dict[str, dict[str, Decimal]] = {}
|
||||
closes: list[str] = sorted(oi_by_strikes.keys())
|
||||
for strike in oi_by_strikes:
|
||||
s: Decimal = Decimal(f'{strike}')
|
||||
call_cash: Decimal = Decimal(0)
|
||||
put_cash: Decimal = Decimal(0)
|
||||
for close in closes:
|
||||
c: Decimal = Decimal(f'{close}')
|
||||
call_cash += max(0, (s - c) * oi_by_strikes[f'{close}']['C'])
|
||||
put_cash += max(0, (c - s) * oi_by_strikes[f'{close}']['P'])
|
||||
intrinsic_values: dict[str, dict[str, Decimal]] = {}
|
||||
closes: list = sorted(Decimal(close) for close in oi_by_strikes)
|
||||
|
||||
for strike, oi in oi_by_strikes.items():
|
||||
s = Decimal(strike)
|
||||
call_cash = sum(max(0, (s - c) * oi_by_strikes[str(c)]['C']) for c in closes)
|
||||
put_cash = sum(max(0, (c - s) * oi_by_strikes[str(c)]['P']) for c in closes)
|
||||
|
||||
intrinsic_values[strike] = {
|
||||
'C': call_cash,
|
||||
|
@ -68,10 +73,9 @@ async def max_pain_daemon(
|
|||
'total': call_cash + put_cash,
|
||||
}
|
||||
|
||||
for strike in intrinsic_values:
|
||||
if intrinsic_values[f'{strike}']['total'] < total_intrinsic_value:
|
||||
total_intrinsic_value = intrinsic_values[f'{strike}']['total']
|
||||
max_pain = strike\
|
||||
if intrinsic_values[strike]['total'] < total_intrinsic_value:
|
||||
total_intrinsic_value = intrinsic_values[strike]['total']
|
||||
max_pain = s
|
||||
|
||||
return {
|
||||
'timestamp': timestamp,
|
||||
|
@ -80,7 +84,6 @@ async def max_pain_daemon(
|
|||
'max_pain': max_pain,
|
||||
}
|
||||
|
||||
|
||||
async with get_client(
|
||||
) as client:
|
||||
instruments = await client.get_instruments(
|
||||
|
@ -92,14 +95,11 @@ async def max_pain_daemon(
|
|||
instruments,
|
||||
) as oi_feed:
|
||||
async for msg in oi_feed:
|
||||
|
||||
update_oi_by_strikes(msg)
|
||||
if check_if_complete(oi_by_strikes):
|
||||
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
|
||||
|
||||
if check_if_complete(oi_by_strikes):
|
||||
max_pain = get_max_pain(oi_by_strikes)
|
||||
print('-----------------------------------------------')
|
||||
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
||||
|
|
Loading…
Reference in New Issue