Update api.py

open_interest for each strike price and each expiry date
max_pain_deribit
Nelson Torres 2024-11-29 10:49:07 -03:00
parent 24bf19c1fb
commit 9abbd8ca1e
1 changed files with 38 additions and 13 deletions

View File

@ -788,6 +788,7 @@ 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]]]]],
from_trio: asyncio.Queue, from_trio: asyncio.Queue,
to_trio: trio.abc.SendChannel, to_trio: trio.abc.SendChannel,
) -> None: ) -> None:
@ -799,12 +800,6 @@ async def aio_open_interest_feed_relay(
Proxy-thru `cryptofeed.FeedHandler` "trades" to `piker`-side. Proxy-thru `cryptofeed.FeedHandler` "trades" to `piker`-side.
''' '''
# Get timestamp and convert it to isoformat
date = (datetime.utcfromtimestamp(trade.timestamp)).isoformat()
print('Trade...')
print(date)
print(trade)
print('=======================')
to_trio.send_nowait(('trade', trade)) to_trio.send_nowait(('trade', trade))
# trade and oi are user defined functions that # trade and oi are user defined functions that
@ -818,18 +813,46 @@ async def aio_open_interest_feed_relay(
Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side. Proxy-thru `cryptofeed.FeedHandler` "oi" to `piker`-side.
''' '''
# Get timestamp and convert it to isoformat nonlocal open_interests
date = (datetime.utcfromtimestamp(oi.timestamp)).isoformat()
print('>>>> Open Interest...') print('>>>> Open Interest...')
print(date)
print(oi) print(oi)
print('==========================') symbol: Symbol = str_to_cb_sym(oi.symbol)
piker_sym: str = cb_sym_to_deribit_inst(symbol)
print(f'{piker_sym}')
(
base,
expiry_date,
strike_price,
option_type
) = tuple(
piker_sym.split('-')
)
if not f'{expiry_date}' in open_interests:
open_interests[f'{expiry_date}'] = {
f'{strike_price}': {
'C': [],
'P': [],
}
}
if not f'{strike_price}' in open_interests[f'{expiry_date}']:
open_interests[f'{expiry_date}'][f'{strike_price}'] = {
'C': [],
'P': [],
}
open_interests[f'{expiry_date}'][f'{strike_price}'][f'{option_type}'].append(
{f'{oi.timestamp}': oi.open_interest}
)
print(f'open_interests:')
print(f'{open_interests}')
print(f'open_interests expiry dates: {len(open_interests)}')
to_trio.send_nowait(('oi', oi)) to_trio.send_nowait(('oi', oi))
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=[TRADES, OPEN_INTEREST], channels=channels,
symbols=instruments, symbols=instruments,
callbacks=callbacks callbacks=callbacks
) )
@ -851,13 +874,14 @@ async def aio_open_interest_feed_relay(
async def open_oi_feed( async def open_oi_feed(
) -> to_asyncio.LinkedTaskChannel: ) -> to_asyncio.LinkedTaskChannel:
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()
open_interests: dict[str, dict[str, dict[str, list[dict[str, Decimal]]]]] = {}
fh: FeedHandler fh: FeedHandler
first: None first: None
chan: to_asyncio.LinkedTaskChannel chan: to_asyncio.LinkedTaskChannel
@ -867,7 +891,8 @@ async def open_oi_feed(
partial( partial(
aio_open_interest_feed_relay, aio_open_interest_feed_relay,
fh, fh,
instruments instruments,
open_interests,
) )
) as (first, chan) ) as (first, chan)
): ):