oi max_pain major refactor
Now the max_pain is calculated taking into account all strike prices and all close prices to find the intrinsic value as deribit.max_pain_deribit
parent
c4600a0392
commit
bf86772d4b
|
@ -827,13 +827,15 @@ async def maybe_open_price_feed(
|
||||||
async def aio_open_interest_feed_relay(
|
async def aio_open_interest_feed_relay(
|
||||||
fh: FeedHandler,
|
fh: FeedHandler,
|
||||||
instruments: list,
|
instruments: list,
|
||||||
open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]],
|
intrinsic_values: dict[str, Decimal],
|
||||||
losses_cache: dict[str, Decimal],
|
oi_by_strikes: dict[str, dict[str, Decimal]],
|
||||||
max_losses: Decimal,
|
|
||||||
max_pain: Decimal,
|
|
||||||
from_trio: asyncio.Queue,
|
from_trio: asyncio.Queue,
|
||||||
to_trio: trio.abc.SendChannel,
|
to_trio: trio.abc.SendChannel,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
max_losses: Decimal = Decimal('Infinity')
|
||||||
|
max_pain: Decimal = Decimal(0)
|
||||||
|
|
||||||
async def _trade(
|
async def _trade(
|
||||||
trade: Trade, # cryptofeed, NOT ours from `.venues`!
|
trade: Trade, # cryptofeed, NOT ours from `.venues`!
|
||||||
receipt_timestamp: int,
|
receipt_timestamp: int,
|
||||||
|
@ -855,13 +857,13 @@ async def aio_open_interest_feed_relay(
|
||||||
Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side.
|
Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
nonlocal losses_cache
|
nonlocal intrinsic_values
|
||||||
|
nonlocal oi_by_strikes
|
||||||
nonlocal max_losses
|
nonlocal max_losses
|
||||||
nonlocal max_pain
|
nonlocal max_pain
|
||||||
nonlocal open_interests
|
|
||||||
symbol: Symbol = str_to_cb_sym(oi.symbol)
|
symbol: Symbol = str_to_cb_sym(oi.symbol)
|
||||||
piker_sym: str = cb_sym_to_deribit_inst(symbol)
|
piker_sym: str = cb_sym_to_deribit_inst(symbol)
|
||||||
data: dict = oi.raw['params']['data']
|
|
||||||
(
|
(
|
||||||
base,
|
base,
|
||||||
expiry_date,
|
expiry_date,
|
||||||
|
@ -870,69 +872,37 @@ async def aio_open_interest_feed_relay(
|
||||||
) = tuple(
|
) = tuple(
|
||||||
piker_sym.split('-')
|
piker_sym.split('-')
|
||||||
)
|
)
|
||||||
if not f'{expiry_date}' in open_interests:
|
|
||||||
open_interests[f'{expiry_date}'] = {
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
|
|
||||||
index_price: Decimal = data['index_price']
|
|
||||||
open_interest: Decimal = oi.open_interest
|
open_interest: Decimal = oi.open_interest
|
||||||
price_delta: Decimal
|
oi_by_strikes[f'{strike_price}'][f'{option_type}'] = open_interest
|
||||||
if f'{option_type}' == 'C':
|
|
||||||
price_delta = index_price - Decimal(strike_price)
|
|
||||||
|
|
||||||
elif f'{option_type}' == 'P':
|
is_ready = check_if_complete(oi_by_strikes)
|
||||||
price_delta = Decimal(strike_price) - index_price
|
if is_ready:
|
||||||
|
for strike in oi_by_strikes:
|
||||||
|
s: Decimal = Decimal(f'{strike}')
|
||||||
|
close_losses = Decimal(0)
|
||||||
|
closes: list[str] = sorted(oi_by_strikes.keys())
|
||||||
|
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'])
|
||||||
|
|
||||||
notional_value = open_interest * index_price
|
intrinsic_values[f'{strike}'] = {}
|
||||||
losses: Decimal = max(0, price_delta * open_interest)
|
intrinsic_values[f'{strike}']['C'] = call_cash
|
||||||
|
intrinsic_values[f'{strike}']['P'] = put_cash
|
||||||
|
intrinsic_values[f'{strike}']['total'] = (call_cash + put_cash)
|
||||||
|
|
||||||
if not f'{strike_price}' in losses_cache:
|
for strike in intrinsic_values:
|
||||||
losses_cache[f'{strike_price}'] = {
|
if intrinsic_values[f'{strike}']['total'] < max_losses:
|
||||||
'C': Decimal(0),
|
max_losses = intrinsic_values[f'{strike}']['total']
|
||||||
'P': Decimal(0),
|
max_pain = strike
|
||||||
}
|
|
||||||
|
|
||||||
losses_cache[f'{strike_price}'][f'{option_type}'] = losses
|
print('-----------------------------------------------')
|
||||||
|
print(f'time: {oi.timestamp}')
|
||||||
strike_losses: Decimal = (
|
print(f'max_pain: {max_pain}')
|
||||||
losses_cache[f'{strike_price}']['C']
|
print(f'max_losses: {max_losses}')
|
||||||
+
|
print('-----------------------------------------------')
|
||||||
losses_cache[f'{strike_price}']['P']
|
|
||||||
)
|
|
||||||
|
|
||||||
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 strike_losses > max_losses:
|
|
||||||
max_losses = open_interests[f'{expiry_date}'][strike]['strike_losses']
|
|
||||||
max_pain = 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('-----------------------------------------------')
|
|
||||||
|
|
||||||
|
|
||||||
channels = [TRADES, OPEN_INTEREST]
|
channels = [TRADES, OPEN_INTEREST]
|
||||||
|
@ -964,21 +934,16 @@ async def open_oi_feed(
|
||||||
|
|
||||||
expiry_date: str = '20DEC24'
|
expiry_date: str = '20DEC24'
|
||||||
instruments: list[Symbol] = []
|
instruments: list[Symbol] = []
|
||||||
|
intrinsic_values: dict[str, dict[str, Decimal]] = {}
|
||||||
|
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||||
|
|
||||||
async with get_client(
|
async with get_client(
|
||||||
) as client:
|
) as client:
|
||||||
# to get all currencies available in deribit
|
|
||||||
# currencies = await client.get_currencies()
|
|
||||||
instruments = await client.get_instruments(
|
instruments = await client.get_instruments(
|
||||||
expiry_date=expiry_date,
|
expiry_date=expiry_date,
|
||||||
)
|
)
|
||||||
losses_cache: dict[str, Decimal] = { # {'<strike_price>': <value>}
|
oi_by_strikes = client.get_strikes_dict(instruments)
|
||||||
'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]]]] = {}
|
|
||||||
fh: FeedHandler
|
fh: FeedHandler
|
||||||
first: None
|
first: None
|
||||||
chan: to_asyncio.LinkedTaskChannel
|
chan: to_asyncio.LinkedTaskChannel
|
||||||
|
@ -989,10 +954,8 @@ async def open_oi_feed(
|
||||||
aio_open_interest_feed_relay,
|
aio_open_interest_feed_relay,
|
||||||
fh,
|
fh,
|
||||||
instruments,
|
instruments,
|
||||||
open_interests,
|
intrinsic_values,
|
||||||
losses_cache,
|
oi_by_strikes,
|
||||||
max_losses,
|
|
||||||
max_pain,
|
|
||||||
)
|
)
|
||||||
) as (first, chan)
|
) as (first, chan)
|
||||||
):
|
):
|
||||||
|
|
Loading…
Reference in New Issue