From b2506b04f6a234c2f105fd192504103b0dbbc829 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 2 Aug 2020 12:18:53 -0400 Subject: [PATCH] Attempt more reliable chart startup Wait for a first actual real-time quote before starting graphics update tasks. Use the new normalized tick format brokers are expected to emit as a `quotes['ticks']` list. Auto detect time frame from historical bars. --- piker/ui/_chart.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/piker/ui/_chart.py b/piker/ui/_chart.py index 68439ecb..37ce6823 100644 --- a/piker/ui/_chart.py +++ b/piker/ui/_chart.py @@ -333,7 +333,7 @@ class ChartPlotWidget(pg.PlotWidget): # XXX: label setting doesn't seem to work? # likely custom graphics need special handling - # label = pg.LabelItem(justify='left') + # label = pg.LabelItem(justify='right') # self.addItem(label) # label.setText("Yo yoyo") # label.setText("x=") @@ -480,7 +480,7 @@ class ChartPlotWidget(pg.PlotWidget): bars = self.parent._array[lbar:rbar] if not len(bars): # likely no data loaded yet - print(f"WTF bars_range = {lbar}:{rbar}") + log.error(f"WTF bars_range = {lbar}:{rbar}") return elif lbar < 0: breakpoint() @@ -604,6 +604,7 @@ def _main( # historical data fetch brokermod = brokers.get_brokermod(brokername) + async with brokermod.get_client() as client: # figure out the exact symbol bars = await client.bars(symbol=sym) @@ -614,11 +615,18 @@ def _main( # determine ohlc delay between bars times = bars['time'] - delay = times[-1] - times[-2] + + # find expected time step between datums + delay = times[-1] - times[times != times[-1]][-1] async def add_new_bars(delay_s): """Task which inserts new bars into the ohlc every ``delay_s`` seconds. """ + # TODO: right now we'll spin printing bars if the last time + # stamp is before a large period of no market activity. + # Likely the best way to solve this is to make this task + # aware of the instrument's tradable hours? + # adjust delay to compensate for trio processing time ad = delay_s - 0.002 @@ -675,7 +683,7 @@ def _main( func, brokername=brokermod.name, sym=sym, - loglevel='info', + # loglevel='info', ) stream = await portal.result() @@ -691,23 +699,27 @@ def _main( async with trio.open_nursery() as n: from piker import fsp - async with data.open_feed(brokername, [sym]) as stream: - # start graphics tasks - n.start_soon(add_new_bars, delay) + async with data.open_feed( + brokername, + [sym], + loglevel=qtractor_kwargs['loglevel'], + ) as (fquote, stream): + # start downstream processor n.start_soon(stream_to_chart, fsp.broker_latency) + # wait for a first quote before we start any update tasks + quote = await stream.__anext__() + + # start graphics tasks after receiving first live quote + n.start_soon(add_new_bars, delay) + async for quote in stream: - # XXX: why are we getting both of these again? ticks = quote.get('ticks') if ticks: for tick in ticks: - if tick['tickType'] in (48, 77): + if tick.get('type') == 'trade': linked_charts.update_from_quote( {'last': tick['price']} ) - # else: - # linked_charts.update_from_quote( - # {'last': quote['close']} - # ) run_qtractor(_main, (), ChartSpace, **qtractor_kwargs)