Factor out QT quoting specifics into the backend
parent
42e9296b36
commit
04fa3c7ca4
|
@ -4,6 +4,7 @@ Core broker-daemon tasks and API.
|
|||
import time
|
||||
import inspect
|
||||
from types import ModuleType
|
||||
from typing import AsyncContextManager
|
||||
|
||||
import trio
|
||||
|
||||
|
@ -41,6 +42,7 @@ async def quote(brokermod: ModuleType, tickers: [str]) -> dict:
|
|||
|
||||
async def poll_tickers(
|
||||
client: 'Client',
|
||||
quoter: AsyncContextManager,
|
||||
tickers: [str],
|
||||
q: trio.Queue,
|
||||
rate: int = 3, # delay between quote requests
|
||||
|
@ -49,25 +51,14 @@ async def poll_tickers(
|
|||
"""Stream quotes for a sequence of tickers at the given ``rate``
|
||||
per second.
|
||||
"""
|
||||
t2ids = await client.tickers2ids(tickers)
|
||||
ids = ','.join(map(str, t2ids.values()))
|
||||
sleeptime = round(1. / rate, 3)
|
||||
_cache = {}
|
||||
|
||||
async with quoter(client, tickers) as get_quotes:
|
||||
while True: # use an event here to trigger exit?
|
||||
prequote_start = time.time()
|
||||
try:
|
||||
quotes_resp = await client.api.quotes(ids=ids)
|
||||
except QuestradeError as qterr:
|
||||
if "Access token is invalid" in str(qterr.args[0]):
|
||||
# out-of-process piker may have renewed already
|
||||
client._reload_config()
|
||||
quotes_resp = await client.api.quotes(ids=ids)
|
||||
else:
|
||||
raise
|
||||
|
||||
quotes = await get_quotes(tickers)
|
||||
postquote_start = time.time()
|
||||
quotes = quotes_resp['quotes']
|
||||
payload = []
|
||||
for quote in quotes:
|
||||
|
||||
|
|
|
@ -271,3 +271,26 @@ async def get_client() -> Client:
|
|||
yield client
|
||||
finally:
|
||||
write_conf(client)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def quoter(client: Client, tickers: [str]):
|
||||
t2ids = await client.tickers2ids(tickers)
|
||||
ids = ','.join(map(str, t2ids.values()))
|
||||
|
||||
async def get_quote(tickers):
|
||||
"""Query for quotes using cached symbol ids.
|
||||
"""
|
||||
try:
|
||||
quotes_resp = await client.api.quotes(ids=ids)
|
||||
except QuestradeError as qterr:
|
||||
if "Access token is invalid" in str(qterr.args[0]):
|
||||
# out-of-process piker may have renewed already
|
||||
client._reload_config()
|
||||
quotes_resp = await client.api.quotes(ids=ids)
|
||||
else:
|
||||
raise
|
||||
|
||||
return quotes_resp['quotes']
|
||||
|
||||
yield get_quote
|
||||
|
|
Loading…
Reference in New Issue