Factor sync part of graphics update into func, add `trigger_update()``

marketstore_backup
Tyler Goodlet 2022-03-06 17:02:49 -05:00
parent 56451c42fd
commit f1ef6c3cc7
1 changed files with 267 additions and 202 deletions

View File

@ -23,7 +23,7 @@ graphics update methods via our custom ``pyqtgraph`` charting api.
''' '''
from functools import partial from functools import partial
import time import time
from typing import Optional from typing import Optional, Any
import numpy as np import numpy as np
import tractor import tractor
@ -109,6 +109,12 @@ def chart_maxmin(
return last_bars_range, mx, max(mn, 0), mx_vlm_in_view return last_bars_range, mx, max(mn, 0), mx_vlm_in_view
# actor-local graphics state that can be passed
# to a ``graphic_update_cycle()`` call by any task
# wishing to update the UI.
_ux_state: dict[str, Any] = {}
async def graphics_update_loop( async def graphics_update_loop(
linked: LinkedSplits, linked: LinkedSplits,
@ -147,7 +153,7 @@ async def graphics_update_loop(
if vlm_chart: if vlm_chart:
vlm_sticky = vlm_chart._ysticks['volume'] vlm_sticky = vlm_chart._ysticks['volume']
vlm_view = vlm_chart.view # vlm_view = vlm_chart.view
maxmin = partial(chart_maxmin, chart, vlm_chart) maxmin = partial(chart_maxmin, chart, vlm_chart)
chart.default_view() chart.default_view()
@ -183,7 +189,7 @@ async def graphics_update_loop(
tick_margin = 3 * tick_size tick_margin = 3 * tick_size
chart.show() chart.show()
view = chart.view # view = chart.view
last_quote = time.time() last_quote = time.time()
i_last = ohlcv.index i_last = ohlcv.index
@ -210,7 +216,27 @@ async def graphics_update_loop(
# async for quotes in iter_drain_quotes(): # async for quotes in iter_drain_quotes():
_ux_state.update({
'quotes': {},
'linked': linked,
'maxmin': maxmin,
'tick_margin': tick_margin,
'ohlcv': ohlcv,
'chart': chart,
'last_price_sticky': last_price_sticky,
'vlm_chart': vlm_chart,
'i_last': i_last,
'last_mx_vlm': last_mx_vlm,
'vlm_sticky': vlm_sticky,
'l1': l1,
'last_mx': last_mx,
'last_mn': last_mn,
})
async for quotes in stream: async for quotes in stream:
_ux_state['quotes'] = quotes
quote_period = time.time() - last_quote quote_period = time.time() - last_quote
quote_rate = round( quote_rate = round(
1/quote_period, 1) if quote_period > 0 else float('inf') 1/quote_period, 1) if quote_period > 0 else float('inf')
@ -231,6 +257,43 @@ async def graphics_update_loop(
chart.pause_all_feeds() chart.pause_all_feeds()
continue continue
# sync call to update all graphics/UX components.
graphics_update_cycle(**_ux_state)
def trigger_update() -> None:
'''
Manually trigger a graphics update from global state.
Generally used from remote actors who wish to trigger a UI refresh.
'''
assert _ux_state is not None, 'graphics engine not initialized?'
graphics_update_cycle(**_ux_state)
def graphics_update_cycle(
quotes,
linked,
maxmin,
tick_margin,
ohlcv,
chart,
last_price_sticky,
vlm_chart,
i_last,
last_mx_vlm,
vlm_sticky,
l1,
last_mx,
last_mn,
wap_in_history: bool = False,
# vlm_view,
) -> None:
for sym, quote in quotes.items(): for sym, quote in quotes.items():
( (
@ -274,7 +337,7 @@ async def graphics_update_loop(
mx_vlm_in_view > last_mx_vlm mx_vlm_in_view > last_mx_vlm
): ):
# print(f'mx vlm: {last_mx_vlm} -> {mx_vlm_in_view}') # print(f'mx vlm: {last_mx_vlm} -> {mx_vlm_in_view}')
vlm_view._set_yrange( vlm_chart.view._set_yrange(
yrange=(0, mx_vlm_in_view * 1.375) yrange=(0, mx_vlm_in_view * 1.375)
) )
last_mx_vlm = mx_vlm_in_view last_mx_vlm = mx_vlm_in_view
@ -410,7 +473,7 @@ async def graphics_update_loop(
and not chart._static_yrange == 'axis' and not chart._static_yrange == 'axis'
): ):
# print(f'new y range: {(mn, mx)}') # print(f'new y range: {(mn, mx)}')
view._set_yrange( chart.view._set_yrange(
yrange=(mn, mx), yrange=(mn, mx),
# TODO: we should probably scale # TODO: we should probably scale
# the view margin based on the size # the view margin based on the size
@ -479,8 +542,10 @@ async def display_symbol_data(
# clear_on_next=True, # clear_on_next=True,
# group_key=loading_sym_key, # group_key=loading_sym_key,
# ) # )
async with open_feed( async with open_feed(
['.'.join((sym, provider))], provider,
[sym],
loglevel=loglevel, loglevel=loglevel,
# limit to at least display's FPS # limit to at least display's FPS
@ -491,11 +556,10 @@ async def display_symbol_data(
ohlcv: ShmArray = feed.shm ohlcv: ShmArray = feed.shm
bars = ohlcv.array bars = ohlcv.array
symbol = feed.symbols[sym] symbol = feed.symbols[sym]
fqsn = symbol.front_fqsn()
# load in symbol's ohlc data # load in symbol's ohlc data
godwidget.window.setWindowTitle( godwidget.window.setWindowTitle(
f'{fqsn} ' f'{symbol.key}@{symbol.brokers} '
f'tick:{symbol.tick_size} ' f'tick:{symbol.tick_size} '
f'step:1s ' f'step:1s '
) )
@ -580,7 +644,8 @@ async def display_symbol_data(
open_order_mode( open_order_mode(
feed, feed,
chart, chart,
fqsn, symbol,
provider,
order_mode_started order_mode_started
) )
): ):