Bar graphics update speed improvements
Use two separate `QPicture` instances: - one for the 3 lines for the last bar - one for all the historical bars lines On price changes update the last bar and only update historical bars when the current bar's period expires (when a new bar is "added"). Add a flag `just_history` for this `BarItems.draw_lines()`. Also, switch the internal lines array/buffer to a 2D numpy array to avoid the type-cast step and instead just flatten using `numpy.ravel()`. Overall this should avoid the problem of draws getting slower over time as new bars are added to the history since price updates only redraw a single bar to the "last" `QPicture` instance. Ideally in the future we can make the `history` `QPicture` a `QPixmap` but it looks like this will require some internal work in `pyqtgraph` to support it.bar_select
parent
b1591e3ee1
commit
9d8a867767
|
@ -345,18 +345,17 @@ class ChartPlotWidget(pg.PlotWidget):
|
||||||
) -> pg.GraphicsObject:
|
) -> pg.GraphicsObject:
|
||||||
"""Draw OHLC datums to chart.
|
"""Draw OHLC datums to chart.
|
||||||
"""
|
"""
|
||||||
# remember it's an enum type..
|
graphics = style(self.plotItem)
|
||||||
graphics = style()
|
|
||||||
|
|
||||||
# adds all bar/candle graphics objects for each data point in
|
# adds all bar/candle graphics objects for each data point in
|
||||||
# the np array buffer to be drawn on next render cycle
|
# the np array buffer to be drawn on next render cycle
|
||||||
graphics.draw_from_data(data)
|
|
||||||
self.addItem(graphics)
|
self.addItem(graphics)
|
||||||
|
# draw after to allow self.scene() to work...
|
||||||
|
graphics.draw_from_data(data)
|
||||||
|
|
||||||
self._graphics[name] = graphics
|
self._graphics[name] = graphics
|
||||||
|
|
||||||
# XXX: How to stack labels vertically?
|
# XXX: How to stack labels vertically?
|
||||||
# Ogi says: "
|
# Ogi says: "use ..."
|
||||||
label = pg.LabelItem(
|
label = pg.LabelItem(
|
||||||
justify='left',
|
justify='left',
|
||||||
size='5pt',
|
size='5pt',
|
||||||
|
@ -596,8 +595,13 @@ async def add_new_bars(delay_s, linked_charts):
|
||||||
# if last_quote == ohlc[-1]:
|
# if last_quote == ohlc[-1]:
|
||||||
# log.debug("Printing flat line for {sym}")
|
# log.debug("Printing flat line for {sym}")
|
||||||
|
|
||||||
# update chart graphics and resize view
|
# update chart historical bars graphics
|
||||||
price_chart.update_from_array(price_chart.name, ohlc)
|
price_chart.update_from_array(
|
||||||
|
price_chart.name,
|
||||||
|
ohlc,
|
||||||
|
just_history=True
|
||||||
|
)
|
||||||
|
# resize view
|
||||||
price_chart._set_yrange()
|
price_chart._set_yrange()
|
||||||
|
|
||||||
for name, chart in linked_charts.subplots.items():
|
for name, chart in linked_charts.subplots.items():
|
||||||
|
@ -611,6 +615,9 @@ async def add_new_bars(delay_s, linked_charts):
|
||||||
|
|
||||||
# TODO: should we update a graphics again time here?
|
# TODO: should we update a graphics again time here?
|
||||||
# Think about race conditions with data update task.
|
# Think about race conditions with data update task.
|
||||||
|
# UPDATE: don't think this should matter know since the last bar
|
||||||
|
# and the prior historical bars are being updated in 2 separate
|
||||||
|
# steps now.
|
||||||
|
|
||||||
|
|
||||||
async def _async_main(
|
async def _async_main(
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
"""
|
"""
|
||||||
Chart graphics for displaying a slew of different data types.
|
Chart graphics for displaying a slew of different data types.
|
||||||
"""
|
"""
|
||||||
import time
|
# import time
|
||||||
from typing import List
|
from typing import List
|
||||||
from itertools import chain
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
|
@ -197,17 +196,10 @@ class CrossHair(pg.GraphicsObject):
|
||||||
def _mk_lines_array(data: List, size: int) -> np.ndarray:
|
def _mk_lines_array(data: List, size: int) -> np.ndarray:
|
||||||
"""Create an ndarray to hold lines graphics objects.
|
"""Create an ndarray to hold lines graphics objects.
|
||||||
"""
|
"""
|
||||||
# TODO: might want to just make this a 2d array to be faster at
|
|
||||||
# flattening using .ravel(): https://stackoverflow.com/a/60089929
|
|
||||||
return np.zeros_like(
|
return np.zeros_like(
|
||||||
data,
|
data,
|
||||||
shape=(int(size),),
|
shape=(int(size), 3),
|
||||||
dtype=[
|
dtype=object,
|
||||||
('index', int),
|
|
||||||
('body', object),
|
|
||||||
('rarm', object),
|
|
||||||
('larm', object)
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -245,10 +237,13 @@ def bars_from_ohlc(
|
||||||
c = QLineF(index_start + w, close, index_start, close)
|
c = QLineF(index_start + w, close, index_start, close)
|
||||||
|
|
||||||
# indexing here is as per the below comments
|
# indexing here is as per the below comments
|
||||||
# lines[3*i:3*i+3] = (hl, o, c)
|
lines[i] = (hl, o, c)
|
||||||
lines[i] = (index, hl, o, c)
|
|
||||||
|
|
||||||
# if not _tina_mode: # piker mode
|
# XXX: in theory we could get a further speedup by using a flat
|
||||||
|
# array and avoiding the call to `np.ravel()` below?
|
||||||
|
# lines[3*i:3*i+3] = (hl, o, c)
|
||||||
|
|
||||||
|
# if not _tina_mode:
|
||||||
# else _tina_mode:
|
# else _tina_mode:
|
||||||
# self.lines = lines = np.concatenate(
|
# self.lines = lines = np.concatenate(
|
||||||
# [high_to_low, open_sticks, close_sticks])
|
# [high_to_low, open_sticks, close_sticks])
|
||||||
|
@ -284,30 +279,30 @@ class BarItems(pg.GraphicsObject):
|
||||||
# bull_brush = pg.mkPen('#00cc00')
|
# bull_brush = pg.mkPen('#00cc00')
|
||||||
# bear_brush = pg.mkPen('#fa0000')
|
# bear_brush = pg.mkPen('#fa0000')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(
|
||||||
|
self,
|
||||||
|
# scene: 'QGraphicsScene', # noqa
|
||||||
|
plotitem: 'pg.PlotItem', # noqa
|
||||||
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.picture = QtGui.QPicture()
|
self.last = QtGui.QPicture()
|
||||||
|
self.history = QtGui.QPicture()
|
||||||
# TODO: implement updateable pixmap solution
|
# TODO: implement updateable pixmap solution
|
||||||
# self.picture = QtGui.QPixmap()
|
self._pi = plotitem
|
||||||
|
# self._scene = plotitem.vb.scene()
|
||||||
# cache bounds dimensions
|
# self.picture = QtGui.QPixmap(1000, 300)
|
||||||
self._boundingRect = None
|
# plotitem.addItem(self.picture)
|
||||||
|
# self._pmi = None
|
||||||
|
# self._pmi = self._scene.addPixmap(self.picture)
|
||||||
|
|
||||||
# XXX: not sure this actually needs to be an array other
|
# XXX: not sure this actually needs to be an array other
|
||||||
# then for the old tina mode calcs for up/down bars below?
|
# then for the old tina mode calcs for up/down bars below?
|
||||||
# lines container
|
# lines container
|
||||||
self.lines = _mk_lines_array([], 50e3)
|
self.lines = _mk_lines_array([], 50e3)
|
||||||
|
|
||||||
# can't use this until we aren't copying the array passed
|
|
||||||
# to ``QPainter.drawLines()``.
|
|
||||||
self._cached_lines = None
|
|
||||||
|
|
||||||
# track the current length of drawable lines within the larger array
|
# track the current length of drawable lines within the larger array
|
||||||
self.index: int = 0
|
self.index: int = 0
|
||||||
|
|
||||||
def last_value(self) -> QLineF:
|
|
||||||
return self.lines[self.index - 1]['rarm']
|
|
||||||
|
|
||||||
@timeit
|
@timeit
|
||||||
def draw_from_data(
|
def draw_from_data(
|
||||||
self,
|
self,
|
||||||
|
@ -317,62 +312,69 @@ class BarItems(pg.GraphicsObject):
|
||||||
"""Draw OHLC datum graphics from a ``np.recarray``.
|
"""Draw OHLC datum graphics from a ``np.recarray``.
|
||||||
"""
|
"""
|
||||||
lines = bars_from_ohlc(data, self.w, start=start)
|
lines = bars_from_ohlc(data, self.w, start=start)
|
||||||
# xmx, xmn, ymx, ymn =
|
|
||||||
|
|
||||||
# save graphics for later reference and keep track
|
# save graphics for later reference and keep track
|
||||||
# of current internal "last index"
|
# of current internal "last index"
|
||||||
index = len(lines)
|
index = len(lines)
|
||||||
self.lines[:index] = lines
|
self.lines[:index] = lines
|
||||||
self.index = index
|
self.index = index
|
||||||
self.draw_lines()
|
self.draw_lines(just_history=True, iend=self.index)
|
||||||
|
|
||||||
def draw_lines(
|
def draw_lines(
|
||||||
self,
|
self,
|
||||||
istart=0,
|
istart=0,
|
||||||
use_cached=False,
|
iend=None,
|
||||||
|
just_history=False,
|
||||||
|
# TODO: could get even fancier and only update the single close line?
|
||||||
|
lines=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Draw the current line set using the painter.
|
"""Draw the current line set using the painter.
|
||||||
"""
|
"""
|
||||||
start = time.time()
|
# start = time.time()
|
||||||
# istart = self.index - 100
|
|
||||||
# if self._cached_lines is not None and (
|
|
||||||
# not (self.index > len(self._cached_lines))
|
|
||||||
# or use_cached
|
|
||||||
# ):
|
|
||||||
# to_draw = self._cached_lines
|
|
||||||
# else:
|
|
||||||
|
|
||||||
# TODO: might be better to use 2d array?
|
if just_history:
|
||||||
# try our fsp.rec2array() and a np.ravel() for speedup
|
istart = 0
|
||||||
# otherwise we might just have to go 2d ndarray of objects.
|
iend = iend or self.index - 1
|
||||||
# see conlusion on speed: # https://stackoverflow.com/a/60089929
|
pic = self.history
|
||||||
self._cached_lines = to_draw = np.ravel(rec2array(
|
else:
|
||||||
self.lines[['body', 'larm', 'rarm']][istart:self.index]
|
istart = self.index - 1
|
||||||
))
|
iend = self.index
|
||||||
|
pic = self.last
|
||||||
|
|
||||||
|
if iend is not None:
|
||||||
|
iend = iend
|
||||||
|
|
||||||
|
# use 2d array of lines objects, see conlusion on speed:
|
||||||
|
# https://stackoverflow.com/a/60089929
|
||||||
|
to_draw = np.ravel(self.lines[istart:iend])
|
||||||
|
|
||||||
# pre-computing a QPicture object allows paint() to run much
|
# pre-computing a QPicture object allows paint() to run much
|
||||||
# more quickly, rather than re-drawing the shapes every time.
|
# more quickly, rather than re-drawing the shapes every time.
|
||||||
p = QtGui.QPainter(self.picture)
|
p = QtGui.QPainter(pic)
|
||||||
p.setPen(self.bars_pen)
|
p.setPen(self.bars_pen)
|
||||||
|
|
||||||
# TODO: is there any way to not have to pass all the lines every
|
# TODO: is there any way to not have to pass all the lines every
|
||||||
# iteration? It seems they won't draw unless it's done this way..
|
# iteration? It seems they won't draw unless it's done this way..
|
||||||
p.drawLines(*to_draw)
|
p.drawLines(*to_draw)
|
||||||
# p.end()
|
p.end()
|
||||||
|
|
||||||
|
# XXX: if we ever try using `QPixmap` again...
|
||||||
|
# if self._pmi is None:
|
||||||
|
# self._pmi = self.scene().addPixmap(self.picture)
|
||||||
|
# else:
|
||||||
|
# self._pmi.setPixmap(self.picture)
|
||||||
|
|
||||||
# trigger re-render
|
# trigger re-render
|
||||||
# https://doc.qt.io/qt-5/qgraphicsitem.html#update
|
# https://doc.qt.io/qt-5/qgraphicsitem.html#update
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
diff = time.time() - start
|
# diff = time.time() - start
|
||||||
|
# print(f'{len(to_draw)} lines update took {diff}')
|
||||||
# print(f'cached: {use_cached} update took {diff}')
|
|
||||||
# print(f"start: {istart} -> drawing {len(to_draw)} lines")
|
|
||||||
# print(self.picture.data())
|
|
||||||
|
|
||||||
def update_from_array(
|
def update_from_array(
|
||||||
self,
|
self,
|
||||||
array: np.ndarray,
|
array: np.ndarray,
|
||||||
|
just_history=False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update the last datum's bar graphic from input data array.
|
"""Update the last datum's bar graphic from input data array.
|
||||||
|
|
||||||
|
@ -387,7 +389,7 @@ class BarItems(pg.GraphicsObject):
|
||||||
length = len(array)
|
length = len(array)
|
||||||
extra = length - index
|
extra = length - index
|
||||||
|
|
||||||
# start_bar_to_update = index - 1
|
# start_bar_to_update = index - 100
|
||||||
|
|
||||||
if extra > 0:
|
if extra > 0:
|
||||||
# generate new graphics to match provided array
|
# generate new graphics to match provided array
|
||||||
|
@ -398,13 +400,15 @@ class BarItems(pg.GraphicsObject):
|
||||||
self.index += bars_added
|
self.index += bars_added
|
||||||
|
|
||||||
# start_bar_to_update = index - bars_added
|
# start_bar_to_update = index - bars_added
|
||||||
|
if just_history:
|
||||||
# self.draw_lines(istart=self.index - 1)
|
self.draw_lines(just_history=True) # istart=self.index - 1)
|
||||||
|
return
|
||||||
|
|
||||||
# current bar update
|
# current bar update
|
||||||
i, time, open, close, = array[-1][['index', 'time', 'open', 'close']]
|
i, open, close, = array[-1][['index', 'open', 'close']]
|
||||||
last = close
|
last = close
|
||||||
i, body, larm, rarm = self.lines[index-1]
|
# i, body, larm, rarm = self.lines[index-1]
|
||||||
|
body, larm, rarm = self.lines[index-1]
|
||||||
|
|
||||||
# XXX: is there a faster way to modify this?
|
# XXX: is there a faster way to modify this?
|
||||||
# update right arm
|
# update right arm
|
||||||
|
@ -428,8 +432,7 @@ class BarItems(pg.GraphicsObject):
|
||||||
else:
|
else:
|
||||||
body.setLine(body.x1(), low, body.x2(), high)
|
body.setLine(body.x1(), low, body.x2(), high)
|
||||||
|
|
||||||
# self.draw_lines(use_cached=extra == 0)
|
self.draw_lines(just_history=False)
|
||||||
self.draw_lines() #istart=self.index - 1)
|
|
||||||
|
|
||||||
# be compat with ``pg.PlotCurveItem``
|
# be compat with ``pg.PlotCurveItem``
|
||||||
setData = update_from_array
|
setData = update_from_array
|
||||||
|
@ -437,18 +440,42 @@ class BarItems(pg.GraphicsObject):
|
||||||
# XXX: From the customGraphicsItem.py example:
|
# XXX: From the customGraphicsItem.py example:
|
||||||
# The only required methods are paint() and boundingRect()
|
# The only required methods are paint() and boundingRect()
|
||||||
def paint(self, p, opt, widget):
|
def paint(self, p, opt, widget):
|
||||||
# import pdb; pdb.set_trace()
|
# start = time.time()
|
||||||
|
|
||||||
|
# TODO: use to avoid drawing artefacts?
|
||||||
|
# self.prepareGeometryChange()
|
||||||
|
|
||||||
# p.setCompositionMode(0)
|
# p.setCompositionMode(0)
|
||||||
p.drawPicture(0, 0, self.picture)
|
|
||||||
|
p.drawPicture(0, 0, self.history)
|
||||||
|
p.drawPicture(0, 0, self.last)
|
||||||
|
|
||||||
|
# TODO: if we can ever make pixmaps work...
|
||||||
|
# p.drawPixmap(0, 0, self.picture)
|
||||||
|
# self._pmi.setPixmap(self.picture)
|
||||||
|
# print(self.scene())
|
||||||
|
|
||||||
|
# diff = time.time() - start
|
||||||
|
# print(f'draw time {diff}')
|
||||||
|
|
||||||
def boundingRect(self):
|
def boundingRect(self):
|
||||||
|
# TODO: can we do rect caching to make this faster?
|
||||||
|
|
||||||
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
||||||
# boundingRect _must_ indicate the entire area that will be
|
# boundingRect _must_ indicate the entire area that will be
|
||||||
# drawn on or else we will get artifacts and possibly crashing.
|
# drawn on or else we will get artifacts and possibly crashing.
|
||||||
# (in this case, QPicture does all the work of computing the
|
# (in this case, QPicture does all the work of computing the
|
||||||
# bounding rect for us)
|
# bounding rect for us).
|
||||||
br = self._boundingRect = QtCore.QRectF(self.picture.boundingRect())
|
|
||||||
return br
|
# compute aggregate bounding rectangle
|
||||||
|
lb = self.last.boundingRect()
|
||||||
|
hb = self.history.boundingRect()
|
||||||
|
return QtCore.QRectF(
|
||||||
|
# top left
|
||||||
|
QtCore.QPointF(hb.topLeft()),
|
||||||
|
# total size
|
||||||
|
QtCore.QSizeF(lb.size() + hb.size())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# XXX: when we get back to enabling tina mode for xb
|
# XXX: when we get back to enabling tina mode for xb
|
||||||
|
|
Loading…
Reference in New Issue