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 time
|
||||||
import inspect
|
import inspect
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
|
from typing import AsyncContextManager
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
|
@ -41,6 +42,7 @@ async def quote(brokermod: ModuleType, tickers: [str]) -> dict:
|
||||||
|
|
||||||
async def poll_tickers(
|
async def poll_tickers(
|
||||||
client: 'Client',
|
client: 'Client',
|
||||||
|
quoter: AsyncContextManager,
|
||||||
tickers: [str],
|
tickers: [str],
|
||||||
q: trio.Queue,
|
q: trio.Queue,
|
||||||
rate: int = 3, # delay between quote requests
|
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``
|
"""Stream quotes for a sequence of tickers at the given ``rate``
|
||||||
per second.
|
per second.
|
||||||
"""
|
"""
|
||||||
t2ids = await client.tickers2ids(tickers)
|
|
||||||
ids = ','.join(map(str, t2ids.values()))
|
|
||||||
sleeptime = round(1. / rate, 3)
|
sleeptime = round(1. / rate, 3)
|
||||||
_cache = {}
|
_cache = {}
|
||||||
|
|
||||||
|
async with quoter(client, tickers) as get_quotes:
|
||||||
while True: # use an event here to trigger exit?
|
while True: # use an event here to trigger exit?
|
||||||
prequote_start = time.time()
|
prequote_start = time.time()
|
||||||
try:
|
quotes = await get_quotes(tickers)
|
||||||
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
|
|
||||||
|
|
||||||
postquote_start = time.time()
|
postquote_start = time.time()
|
||||||
quotes = quotes_resp['quotes']
|
|
||||||
payload = []
|
payload = []
|
||||||
for quote in quotes:
|
for quote in quotes:
|
||||||
|
|
||||||
|
|
|
@ -271,3 +271,26 @@ async def get_client() -> Client:
|
||||||
yield client
|
yield client
|
||||||
finally:
|
finally:
|
||||||
write_conf(client)
|
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