99from fluid .utils .data import compact_dict
1010from fluid .utils .http_client import AioHttpClient , HttpResponse , HttpResponseError
1111
12+ from quantflow .options .inputs import OptionType
1213from quantflow .options .surface import VolSecurityType , VolSurfaceLoader
13- from quantflow .utils .numbers import round_to_step , to_decimal
14+ from quantflow .utils .numbers import (
15+ Number ,
16+ round_to_step ,
17+ to_decimal ,
18+ to_decimal_or_none ,
19+ )
1420
1521
1622def parse_maturity (v : str ) -> datetime :
@@ -80,9 +86,19 @@ async def get_volatility(self, currency: str, **kw: Any) -> pd.DataFrame:
8086 kw .update (params = dict (currency = currency ), callback = self .to_df )
8187 return await self .get_path ("public/get_historical_volatility" , ** kw )
8288
83- async def volatility_surface_loader (self , currency : str ) -> VolSurfaceLoader :
89+ async def volatility_surface_loader (
90+ self ,
91+ currency : str ,
92+ * ,
93+ exclude_open_interest : Number | None = None ,
94+ exclude_volume : Number | None = None ,
95+ ) -> VolSurfaceLoader :
8496 """Create a :class:`.VolSurfaceLoader` for a given crypto-currency"""
85- loader = VolSurfaceLoader ()
97+ loader = VolSurfaceLoader (
98+ asset = currency ,
99+ exclude_open_interest = to_decimal_or_none (exclude_open_interest ),
100+ exclude_volume = to_decimal_or_none (exclude_volume ),
101+ )
86102 futures = await self .get_book_summary_by_currency (
87103 currency = currency , kind = InstrumentKind .future
88104 )
@@ -92,9 +108,9 @@ async def volatility_surface_loader(self, currency: str) -> VolSurfaceLoader:
92108 instruments = await self .get_instruments (currency = currency )
93109 instrument_map = {i ["instrument_name" ]: i for i in instruments }
94110 min_tick_size = Decimal ("inf" )
95- for future in futures :
96- if (bid_ := future ["bid_price" ]) and (ask_ := future ["ask_price" ]):
97- name = future ["instrument_name" ]
111+ for entry in futures :
112+ if (bid_ := entry ["bid_price" ]) and (ask_ := entry ["ask_price" ]):
113+ name = entry ["instrument_name" ]
98114 meta = instrument_map [name ]
99115 tick_size = to_decimal (meta ["tick_size" ])
100116 min_tick_size = min (min_tick_size , tick_size )
@@ -105,8 +121,8 @@ async def volatility_surface_loader(self, currency: str) -> VolSurfaceLoader:
105121 VolSecurityType .spot ,
106122 bid = bid ,
107123 ask = ask ,
108- open_interest = int ( future ["open_interest" ]),
109- volume = int ( future ["volume_usd" ]),
124+ open_interest = to_decimal ( entry ["open_interest" ]),
125+ volume = to_decimal ( entry ["volume_usd" ]),
110126 )
111127 else :
112128 maturity = pd .to_datetime (
@@ -119,15 +135,15 @@ async def volatility_surface_loader(self, currency: str) -> VolSurfaceLoader:
119135 maturity = maturity ,
120136 bid = bid ,
121137 ask = ask ,
122- open_interest = int ( future ["open_interest" ]),
123- volume = int ( future ["volume_usd" ]),
138+ open_interest = to_decimal ( entry ["open_interest" ]),
139+ volume = to_decimal ( entry ["volume_usd" ]),
124140 )
125141 loader .tick_size_forwards = min_tick_size
126142
127143 min_tick_size = Decimal ("inf" )
128- for option in options :
129- if (bid_ := option ["bid_price" ]) and (ask_ := option ["ask_price" ]):
130- name = option ["instrument_name" ]
144+ for entry in options :
145+ if (bid_ := entry ["bid_price" ]) and (ask_ := entry ["ask_price" ]):
146+ name = entry ["instrument_name" ]
131147 meta = instrument_map [name ]
132148 tick_size = to_decimal (meta ["tick_size" ])
133149 min_tick_size = min (min_tick_size , tick_size )
@@ -139,9 +155,15 @@ async def volatility_surface_loader(self, currency: str) -> VolSurfaceLoader:
139155 unit = "ms" ,
140156 utc = True ,
141157 ).to_pydatetime (),
142- call = meta ["option_type" ] == "call" ,
158+ option_type = (
159+ OptionType .call
160+ if meta ["option_type" ] == "call"
161+ else OptionType .put
162+ ),
143163 bid = round_to_step (bid_ , tick_size ),
144164 ask = round_to_step (ask_ , tick_size ),
165+ open_interest = to_decimal (entry ["open_interest" ]),
166+ volume = to_decimal (entry ["volume_usd" ]),
145167 )
146168 loader .tick_size_options = min_tick_size
147169 return loader
0 commit comments