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

mkts_backup
Tyler Goodlet 2022-03-06 17:02:49 -05:00
parent 81c69c54ec
commit bcd0895a12
1 changed files with 255 additions and 193 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,8 +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')
@ -232,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():
( (
@ -275,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
@ -411,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