Handle dynamic symbol subscriptions in QT backend

kivy_mainline_and_py3.8
Tyler Goodlet 2018-04-16 02:13:16 -04:00
parent d65bd78f5d
commit 23ae71089f
1 changed files with 21 additions and 6 deletions

View File

@ -288,12 +288,30 @@ async def get_client() -> Client:
async def quoter(client: Client, tickers: [str]): async def quoter(client: Client, tickers: [str]):
"""Quoter context. """Quoter context.
""" """
t2ids = await client.tickers2ids(tickers) t2ids = {}
ids = ','.join(map(str, t2ids.values())) 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): async def get_quote(tickers):
"""Query for quotes using cached symbol ids. """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: try:
quotes_resp = await client.api.quotes(ids=ids) quotes_resp = await client.api.quotes(ids=ids)
except QuestradeError as qterr: except QuestradeError as qterr:
@ -315,10 +333,7 @@ async def quoter(client: Client, tickers: [str]):
return quotes return quotes
first_quotes_dict = await get_quote(tickers) first_quotes_dict = await get_quote(tickers)
for symbol, quote in first_quotes_dict.items(): filter_symbols(first_quotes_dict)
if quote['low52w'] is None:
log.warn(f"{symbol} seems to be defunct discarding from tickers")
t2ids.pop(symbol)
# re-save symbol ids cache # re-save symbol ids cache
ids = ','.join(map(str, t2ids.values())) ids = ','.join(map(str, t2ids.values()))