From 23ae71089fc04e868ed04e6ab99b5dd4d560c38f Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 16 Apr 2018 02:13:16 -0400 Subject: [PATCH] Handle dynamic symbol subscriptions in QT backend --- piker/brokers/questrade.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/piker/brokers/questrade.py b/piker/brokers/questrade.py index ee0886cb..f88f3f0b 100644 --- a/piker/brokers/questrade.py +++ b/piker/brokers/questrade.py @@ -288,12 +288,30 @@ async def get_client() -> Client: async def quoter(client: Client, tickers: [str]): """Quoter context. """ - t2ids = await client.tickers2ids(tickers) - ids = ','.join(map(str, t2ids.values())) + t2ids = {} + ids = '' + + def filter_symbols(quotes_dict): + nonlocal t2ids + for symbol, quote in quotes_dict.items(): + if quote['low52w'] is None: + log.warn( + f"{symbol} seems to be defunct discarding from tickers") + t2ids.pop(symbol) async def get_quote(tickers): """Query for quotes using cached symbol ids. """ + if not tickers: + return {} + nonlocal ids, t2ids + new, current = set(tickers), set(t2ids.keys()) + if new != current: + # update ticker ids cache + log.info(f"Tickers set changed {new - current}") + t2ids = await client.tickers2ids(tickers) + ids = ','.join(map(str, t2ids.values())) + try: quotes_resp = await client.api.quotes(ids=ids) except QuestradeError as qterr: @@ -315,10 +333,7 @@ async def quoter(client: Client, tickers: [str]): return quotes first_quotes_dict = await get_quote(tickers) - for symbol, quote in first_quotes_dict.items(): - if quote['low52w'] is None: - log.warn(f"{symbol} seems to be defunct discarding from tickers") - t2ids.pop(symbol) + filter_symbols(first_quotes_dict) # re-save symbol ids cache ids = ','.join(map(str, t2ids.values()))