1+ import enum
2+ from dataclasses import dataclass
13from datetime import datetime , timezone
24from decimal import Decimal
35from typing import Any , cast
46
57import pandas as pd
68from dateutil .parser import parse
9+ from fluid .utils .data import compact_dict
710from fluid .utils .http_client import AioHttpClient , HttpResponse , HttpResponseError
811
912from quantflow .options .surface import VolSecurityType , VolSurfaceLoader
@@ -14,47 +17,79 @@ def parse_maturity(v: str) -> datetime:
1417 return parse (v ).replace (tzinfo = timezone .utc , hour = 8 )
1518
1619
20+ class InstrumentKind (enum .StrEnum ):
21+ """Instrument kind for Deribit API."""
22+
23+ future = enum .auto ()
24+ option = enum .auto ()
25+ spot = enum .auto ()
26+ future_combo = enum .auto ()
27+ option_combo = enum .auto ()
28+
29+
30+ @dataclass
1731class Deribit (AioHttpClient ):
1832 """Deribit API client
1933
20- Fetch market and static data from `Deribit`_.
34+ Fetch market and static data from `Deribit`_ API .
2135
2236 .. _Deribit: https://docs.deribit.com/
2337 """
2438
25- url = "https://www.deribit.com/api/v2"
39+ url : str = "https://www.deribit.com/api/v2"
2640
27- async def get_book_summary_by_instrument (self , ** kw : Any ) -> list [dict ]:
28- kw .update (callback = self .to_result )
41+ async def get_book_summary_by_instrument (
42+ self ,
43+ instrument_name : str ,
44+ ** kw : Any ,
45+ ) -> list [dict ]:
46+ """Get the book summary for a given instrument."""
47+ kw .update (params = dict (instrument_name = instrument_name ), callback = self .to_result )
2948 return cast (
3049 list [dict ],
3150 await self .get_path ("public/get_book_summary_by_instrument" , ** kw ),
3251 )
3352
34- async def get_book_summary_by_currency (self , ** kw : Any ) -> list [dict ]:
35- kw .update (callback = self .to_result )
53+ async def get_book_summary_by_currency (
54+ self , currency : str , kind : InstrumentKind | None = None , ** kw : Any
55+ ) -> list [dict ]:
56+ """Get the book summary for a given currency."""
57+ kw .update (
58+ params = compact_dict (currency = currency , kind = kind ), callback = self .to_result
59+ )
3660 return cast (
3761 list [dict ], await self .get_path ("public/get_book_summary_by_currency" , ** kw )
3862 )
3963
40- async def get_instruments (self , ** kw : Any ) -> list [dict ]:
41- kw .update (callback = self .to_result )
64+ async def get_instruments (
65+ self ,
66+ currency : str ,
67+ kind : InstrumentKind | None = None ,
68+ expired : bool | None = None ,
69+ ** kw : Any ,
70+ ) -> list [dict ]:
71+ """Get the list of instruments for a given currency."""
72+ kw .update (
73+ params = compact_dict (currency = currency , kind = kind , expired = expired ),
74+ callback = self .to_result ,
75+ )
4276 return cast (list [dict ], await self .get_path ("public/get_instruments" , ** kw ))
4377
44- async def get_volatility (self , ** kw : Any ) -> pd .DataFrame :
45- kw .update (callback = self .to_df )
78+ async def get_volatility (self , currency : str , ** kw : Any ) -> pd .DataFrame :
79+ """Provides information about historical volatility for given cryptocurrency"""
80+ kw .update (params = dict (currency = currency ), callback = self .to_df )
4681 return await self .get_path ("public/get_historical_volatility" , ** kw )
4782
4883 async def volatility_surface_loader (self , currency : str ) -> VolSurfaceLoader :
4984 """Create a :class:`.VolSurfaceLoader` for a given crypto-currency"""
5085 loader = VolSurfaceLoader ()
5186 futures = await self .get_book_summary_by_currency (
52- params = dict ( currency = currency , kind = " future" )
87+ currency = currency , kind = InstrumentKind . future
5388 )
5489 options = await self .get_book_summary_by_currency (
55- params = dict ( currency = currency , kind = " option" )
90+ currency = currency , kind = InstrumentKind . option
5691 )
57- instruments = await self .get_instruments (params = dict ( currency = currency ) )
92+ instruments = await self .get_instruments (currency = currency )
5893 instrument_map = {i ["instrument_name" ]: i for i in instruments }
5994 min_tick_size = Decimal ("inf" )
6095 for future in futures :
0 commit comments