Add initial symbol search api for kraken

symbol_search
Tyler Goodlet 2021-05-05 10:12:23 -04:00
parent be39e9bdf5
commit 4b818ea2f2
1 changed files with 33 additions and 2 deletions

View File

@ -196,9 +196,12 @@ class Client:
async def cache_symbols( async def cache_symbols(
self, self,
) -> None: ) -> dict:
if not self._pairs:
self._pairs = await self.symbol_info() self._pairs = await self.symbol_info()
return self._pairs
async def search_stocks( async def search_stocks(
self, self,
pattern: str, pattern: str,
@ -279,10 +282,38 @@ class Client:
@asynccontextmanager @asynccontextmanager
async def get_client() -> Client: async def get_client() -> Client:
client = Client() client = Client()
# load all symbols locally for fast search
await client.cache_symbols() await client.cache_symbols()
yield client yield client
async def open_symbol_search(
ctx: tractor.Context,
) -> Client:
async with open_cached_client('kraken') as client:
# load all symbols locally for fast search
cache = await client.cache_symbols()
await ctx.started(cache)
async with ctx.open_stream() as stream:
async for pattern in stream:
matches = fuzzy.extractBests(
pattern,
cache,
score_cutoff=50,
)
# repack in dict form
await stream.send(
{item[0]['altname']: item[0]
for item in matches}
)
async def stream_messages(ws): async def stream_messages(ws):
too_slow_count = last_hb = 0 too_slow_count = last_hb = 0