added first calcs for max_pain

max_pain_deribit
Nelson Torres 2024-12-03 14:35:57 -03:00
parent 6e08c60d53
commit 5de14bc3f9
1 changed files with 67 additions and 13 deletions

View File

@ -793,6 +793,9 @@ 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]]]]], open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]],
losses_cache: 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:
@ -817,12 +820,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 max_losses
nonlocal max_pain
nonlocal open_interests nonlocal open_interests
print('>>>> Open Interest...')
print(oi)
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)
print(f'{piker_sym}') data: dict = oi.raw['params']['data']
( (
base, base,
expiry_date, expiry_date,
@ -836,24 +840,65 @@ async def aio_open_interest_feed_relay(
f'{strike_price}': { f'{strike_price}': {
'C': [], 'C': [],
'P': [], 'P': [],
'strike_losses': Decimal(0),
} }
} }
if not f'{strike_price}' in open_interests[f'{expiry_date}']: if not f'{strike_price}' in open_interests[f'{expiry_date}']:
open_interests[f'{expiry_date}'][f'{strike_price}'] = { open_interests[f'{expiry_date}'][f'{strike_price}'] = {
'C': [], 'C': [],
'P': [], 'P': [],
'strike_losses': Decimal(0),
} }
open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'].append( index_price: Decimal = data['index_price']
{f'{oi.timestamp}': oi.open_interest} price_delta: Decimal = abs(index_price - Decimal(strike_price))
open_interest: Decimal = oi.open_interest
losses: Decimal = price_delta * open_interest
if not f'{strike_price}' in losses_cache:
losses_cache[f'{strike_price}'] = {
'C': Decimal(0),
'P': Decimal(0),
}
losses_cache[f'{strike_price}'][f'{option_type}'] = losses
strike_losses: Decimal = (
losses_cache[f'{strike_price}']['C']
+
losses_cache[f'{strike_price}']['P']
) )
print(f'open_interests:')
print(f'{open_interests}') print(f'>>>> Open Interest...')
print(f'open_interests expiry dates: {len(open_interests)}') print(f'max_losses: {max_losses}\n')
to_trio.send_nowait(('oi', oi)) 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,
'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:
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('-----------------------------------------------')
channels = [TRADES, OPEN_INTEREST] channels = [TRADES, OPEN_INTEREST]
callbacks = {TRADES: _trade, OPEN_INTEREST: _oi} callbacks={TRADES: _trade, OPEN_INTEREST: _oi}
fh.add_feed( fh.add_feed(
DERIBIT, DERIBIT,
channels=channels, channels=channels,
@ -878,16 +923,22 @@ 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'
instruments: list[Symbol] = [] instruments: list[Symbol] = []
async with get_client( async with get_client(
) as client: ) as client:
# to get all currencies available in deribit # to get all currencies available in deribit
# currencies = await client.get_currencies() # currencies = await client.get_currencies()
instruments = await client.get_instruments( instruments = await client.get_instruments(
expiry_date='6DEC24' expiry_date=expiry_date,
) )
losses_cache: dict[str, Decimal] = { # {'<strike_price>': <value>}
open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]] = {} 'C': Decimal(0),
'P': Decimal(0),
}
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
@ -899,6 +950,9 @@ async def open_oi_feed(
fh, fh,
instruments, instruments,
open_interests, open_interests,
losses_cache,
max_losses,
max_pain,
) )
) as (first, chan) ) as (first, chan)
): ):