Resize volume yaxis to in view range

basic_vlm_display
Tyler Goodlet 2021-09-21 09:35:37 -04:00
parent dd752927a2
commit 37eeb0d74b
1 changed files with 47 additions and 27 deletions

View File

@ -101,7 +101,7 @@ async def update_chart_from_quotes(
last_bars_range = chart.bars_range() last_bars_range = chart.bars_range()
l, lbar, rbar, r = last_bars_range l, lbar, rbar, r = last_bars_range
in_view = array[lbar - ifirst:rbar - ifirst] in_view = array[lbar - ifirst:rbar - ifirst + 1]
assert in_view.size assert in_view.size
@ -112,11 +112,20 @@ async def update_chart_from_quotes(
# sym = chart.name # sym = chart.name
# mx, mn = np.nanmax(in_view[sym]), np.nanmin(in_view[sym]) # mx, mn = np.nanmax(in_view[sym]), np.nanmin(in_view[sym])
return last_bars_range, mx, max(mn, 0) mx_vlm_in_view = 0
if vlm_chart:
mx_vlm_in_view = np.max(in_view['volume'])
return last_bars_range, mx, max(mn, 0), mx_vlm_in_view
chart.default_view() chart.default_view()
last_bars_range, last_mx, last_mn = maxmin() (
last_bars_range,
last_mx,
last_mn,
last_mx_vlm,
) = maxmin()
last, volume = ohlcv.array[-1][['close', 'volume']] last, volume = ohlcv.array[-1][['close', 'volume']]
@ -139,7 +148,7 @@ async def update_chart_from_quotes(
# present differently -> likely dark vlm # present differently -> likely dark vlm
tick_size = chart.linked.symbol.tick_size tick_size = chart.linked.symbol.tick_size
tick_margin = 2 * tick_size tick_margin = 3 * tick_size
last_ask = last_bid = last_clear = time.time() last_ask = last_bid = last_clear = time.time()
chart.show() chart.show()
@ -155,6 +164,36 @@ async def update_chart_from_quotes(
now = time.time() now = time.time()
# brange, mx_in_view, mn_in_view = maxmin()
(
brange,
mx_in_view,
mn_in_view,
mx_vlm_in_view,
) = maxmin()
l, lbar, rbar, r = brange
mx = mx_in_view + tick_margin
mn = mn_in_view - tick_margin
# NOTE: vlm may be written by the ``brokerd`` backend
# event though a tick sample is not emitted.
# TODO: show dark trades differently
# https://github.com/pikers/piker/issues/116
array = ohlcv.array
if vlm_chart:
# print(f"volume: {end['volume']}")
vlm_chart.update_curve_from_array('volume', array)
vlm_sticky.update_from_data(*array[-1][['index', 'volume']])
if (
mx_vlm_in_view != last_mx_vlm or
mx_vlm_in_view > last_mx_vlm
):
# print(f'mx vlm: {last_mx_vlm} -> {mx_vlm_in_view}')
vlm_chart._set_yrange(yrange=(0, mx_vlm_in_view * 1.375))
last_mx_vlm = mx_vlm_in_view
for tick in quote.get('ticks', ()): for tick in quote.get('ticks', ()):
# log.info( # log.info(
@ -180,16 +219,13 @@ async def update_chart_from_quotes(
# set time of last graphics update # set time of last graphics update
last_clear = now last_clear = now
array = ohlcv.array
# update price sticky(s) # update price sticky(s)
end = array[-1] end = array[-1]
last_price_sticky.update_from_data( last_price_sticky.update_from_data(
*end[['index', 'close']] *end[['index', 'close']]
) )
# plot bars # update ohlc sampled price bars
# update price bar
chart.update_ohlc_from_array( chart.update_ohlc_from_array(
chart.name, chart.name,
array, array,
@ -199,15 +235,6 @@ async def update_chart_from_quotes(
# update vwap overlay line # update vwap overlay line
chart.update_curve_from_array('bar_wap', ohlcv.array) chart.update_curve_from_array('bar_wap', ohlcv.array)
# TODO: show dark trades differently
# https://github.com/pikers/piker/issues/116
if vlm_chart:
# print(f"volume: {end['volume']}")
vlm_chart.update_curve_from_array(
'volume', ohlcv.array
)
vlm_sticky.update_from_data(*end[['index', 'volume']])
# l1 book events # l1 book events
# throttle the book graphics updates at a lower rate # throttle the book graphics updates at a lower rate
# since they aren't as critical for a manual user # since they aren't as critical for a manual user
@ -231,11 +258,6 @@ async def update_chart_from_quotes(
# compute max and min trade values to display in view # compute max and min trade values to display in view
# TODO: we need a streaming minmax algorithm here, see # TODO: we need a streaming minmax algorithm here, see
# def above. # def above.
brange, mx_in_view, mn_in_view = maxmin()
l, lbar, rbar, r = brange
mx = mx_in_view + tick_margin
mn = mn_in_view - tick_margin
# XXX: prettty sure this is correct? # XXX: prettty sure this is correct?
# if ticktype in ('trade', 'last'): # if ticktype in ('trade', 'last'):
@ -259,16 +281,14 @@ async def update_chart_from_quotes(
elif ticktype in ('bid', 'bsize'): elif ticktype in ('bid', 'bsize'):
l1.bid_label.update_fields({'level': price, 'size': size}) l1.bid_label.update_fields({'level': price, 'size': size})
# update min price in view to keep bid on screen # in view y-range checking for auto-scale
mn = min(price - tick_margin, mn) # update the max/min price in view to keep bid/ask on screen
# update max price in view to keep ask on screen
mx = max(price + tick_margin, mx) mx = max(price + tick_margin, mx)
mn = min(price - tick_margin, mn)
if (mx > last_mx) or ( if (mx > last_mx) or (
mn < last_mn mn < last_mn
): ):
# print(f'new y range: {(mn, mx)}') # print(f'new y range: {(mn, mx)}')
chart._set_yrange( chart._set_yrange(
yrange=(mn, mx), yrange=(mn, mx),
# TODO: we should probably scale # TODO: we should probably scale