Skip to content

Commit

Permalink
deribit: switch to rapidfuzz API
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Sep 21, 2023
1 parent d4833eb commit 46d83e9
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions piker/brokers/deribit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@
)
from cryptofeed.symbols import Symbol

from piker.data.types import Struct
from piker.data import def_iohlcv_fields
from piker.data import (
def_iohlcv_fields,
match_from_pairs,
Struct,
)
from piker.data._web_bs import (
open_jsonrpc_session
)
Expand All @@ -79,7 +82,7 @@
class JSONRPCResult(Struct):
jsonrpc: str = '2.0'
id: int
result: Optional[dict] = None
result: Optional[list[dict]] = None
error: Optional[dict] = None
usIn: int
usOut: int
Expand Down Expand Up @@ -289,24 +292,29 @@ async def symbol_info(
currency: str = 'btc', # BTC, ETH, SOL, USDC
kind: str = 'option',
expired: bool = False
) -> dict[str, Any]:
"""Get symbol info for the exchange.

"""
) -> dict[str, dict]:
'''
Get symbol infos.
'''
if self._pairs:
return self._pairs

# will retrieve all symbols by default
params = {
params: dict[str, str] = {
'currency': currency.upper(),
'kind': kind,
'expired': str(expired).lower()
}

resp = await self.json_rpc('public/get_instruments', params)
results = resp.result

instruments = {
resp: JSONRPCResult = await self.json_rpc(
'public/get_instruments',
params,
)
# convert to symbol-keyed table
results: list[dict] | None = resp.result
instruments: dict[str, dict] = {
item['instrument_name'].lower(): item
for item in results
}
Expand All @@ -319,6 +327,7 @@ async def symbol_info(
async def cache_symbols(
self,
) -> dict:

if not self._pairs:
self._pairs = await self.symbol_info()

Expand All @@ -329,17 +338,23 @@ async def search_symbols(
pattern: str,
limit: int = 30,
) -> dict[str, Any]:
data = await self.symbol_info()

matches = fuzzy.extractBests(
pattern,
data,
'''
Fuzzy search symbology set for pairs matching `pattern`.
'''
pairs: dict[str, Any] = await self.symbol_info()
matches: dict[str, Pair] = match_from_pairs(
pairs=pairs,
query=pattern.upper(),
score_cutoff=35,
limit=limit
)
# repack in dict form
return {item[0]['instrument_name'].lower(): item[0]
for item in matches}

# repack in name-keyed table
return {
pair['instrument_name'].lower(): pair
for pair in matches.values()
}

async def bars(
self,
Expand Down

0 comments on commit 46d83e9

Please sign in to comment.