2020-12-28 20:32:34 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
Super fast OHLC sampling graphics types.
|
2020-12-29 13:43:25 +00:00
|
|
|
|
2020-12-28 20:32:34 +00:00
|
|
|
"""
|
2022-03-09 20:00:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
from typing import (
|
|
|
|
Optional,
|
|
|
|
TYPE_CHECKING,
|
|
|
|
)
|
2020-12-28 20:32:34 +00:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
2022-11-23 18:58:01 +00:00
|
|
|
from PyQt5 import (
|
|
|
|
QtGui,
|
|
|
|
QtWidgets,
|
|
|
|
)
|
|
|
|
from PyQt5.QtCore import (
|
|
|
|
QLineF,
|
|
|
|
QRectF,
|
|
|
|
)
|
|
|
|
|
2022-06-03 17:55:34 +00:00
|
|
|
from PyQt5.QtGui import QPainterPath
|
2020-12-28 20:32:34 +00:00
|
|
|
|
2022-04-01 17:28:50 +00:00
|
|
|
from .._profile import pg_profile_enabled, ms_slower_then
|
2021-07-12 13:18:11 +00:00
|
|
|
from ._style import hcolor
|
2022-03-09 16:01:01 +00:00
|
|
|
from ..log import get_logger
|
2022-10-31 01:11:14 +00:00
|
|
|
from .._profile import Profiler
|
2022-03-09 16:01:01 +00:00
|
|
|
|
2022-03-09 20:00:17 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ._chart import LinkedSplits
|
|
|
|
|
2022-03-09 16:01:01 +00:00
|
|
|
|
|
|
|
log = get_logger(__name__)
|
2020-12-28 20:32:34 +00:00
|
|
|
|
|
|
|
|
2022-04-01 17:28:50 +00:00
|
|
|
def bar_from_ohlc_row(
|
2021-02-21 17:02:20 +00:00
|
|
|
row: np.ndarray,
|
2022-06-02 17:35:01 +00:00
|
|
|
# 0.5 is no overlap between arms, 1.0 is full overlap
|
2022-04-16 19:22:11 +00:00
|
|
|
w: float = 0.43
|
2022-03-09 20:00:17 +00:00
|
|
|
|
|
|
|
) -> tuple[QLineF]:
|
2022-04-01 17:28:50 +00:00
|
|
|
'''
|
|
|
|
Generate the minimal ``QLineF`` lines to construct a single
|
|
|
|
OHLC "bar" for use in the "last datum" of a series.
|
2021-02-21 17:02:20 +00:00
|
|
|
|
2022-04-01 17:28:50 +00:00
|
|
|
'''
|
2020-12-28 20:32:34 +00:00
|
|
|
open, high, low, close, index = row[
|
|
|
|
['open', 'high', 'low', 'close', 'index']]
|
|
|
|
|
2021-02-21 17:02:20 +00:00
|
|
|
# TODO: maybe consider using `QGraphicsLineItem` ??
|
|
|
|
# gives us a ``.boundingRect()`` on the objects which may make
|
|
|
|
# computing the composite bounding rect of the last bars + the
|
|
|
|
# history path faster since it's done in C++:
|
|
|
|
# https://doc.qt.io/qt-5/qgraphicslineitem.html
|
|
|
|
|
2020-12-28 20:32:34 +00:00
|
|
|
# high -> low vertical (body) line
|
|
|
|
if low != high:
|
|
|
|
hl = QLineF(index, low, index, high)
|
|
|
|
else:
|
|
|
|
# XXX: if we don't do it renders a weird rectangle?
|
|
|
|
# see below for filtering this later...
|
|
|
|
hl = None
|
|
|
|
|
|
|
|
# NOTE: place the x-coord start as "middle" of the drawing range such
|
|
|
|
# that the open arm line-graphic is at the left-most-side of
|
2021-02-21 17:02:20 +00:00
|
|
|
# the index's range according to the view mapping coordinates.
|
2020-12-28 20:32:34 +00:00
|
|
|
|
|
|
|
# open line
|
|
|
|
o = QLineF(index - w, open, index, open)
|
2021-02-21 17:02:20 +00:00
|
|
|
|
2020-12-28 20:32:34 +00:00
|
|
|
# close line
|
|
|
|
c = QLineF(index, close, index + w, close)
|
|
|
|
|
|
|
|
return [hl, o, c]
|
|
|
|
|
|
|
|
|
|
|
|
class BarItems(pg.GraphicsObject):
|
2022-03-09 16:01:01 +00:00
|
|
|
'''
|
|
|
|
"Price range" bars graphics rendered from a OHLC sampled sequence.
|
|
|
|
|
|
|
|
'''
|
2020-12-28 20:32:34 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-03-09 20:00:17 +00:00
|
|
|
linked: LinkedSplits,
|
2020-12-28 20:32:34 +00:00
|
|
|
plotitem: 'pg.PlotItem', # noqa
|
2022-11-14 21:17:24 +00:00
|
|
|
color: str = 'bracket',
|
2022-11-16 22:38:52 +00:00
|
|
|
last_bar_color: str = 'original',
|
2022-03-09 16:01:01 +00:00
|
|
|
|
|
|
|
name: Optional[str] = None,
|
|
|
|
|
2020-12-28 20:32:34 +00:00
|
|
|
) -> None:
|
2022-03-09 16:01:01 +00:00
|
|
|
super().__init__()
|
2022-04-01 17:28:50 +00:00
|
|
|
self.linked = linked
|
2021-09-21 19:27:45 +00:00
|
|
|
# XXX: for the mega-lulz increasing width here increases draw
|
|
|
|
# latency... so probably don't do it until we figure that out.
|
2022-11-14 21:17:24 +00:00
|
|
|
self._color = color
|
|
|
|
self.bars_pen = pg.mkPen(hcolor(color), width=1)
|
2021-09-20 17:41:53 +00:00
|
|
|
self.last_bar_pen = pg.mkPen(hcolor(last_bar_color), width=2)
|
2022-04-04 04:10:13 +00:00
|
|
|
self._name = name
|
2022-04-01 17:28:50 +00:00
|
|
|
|
2021-07-21 19:50:09 +00:00
|
|
|
self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
2022-06-03 17:55:34 +00:00
|
|
|
self.path = QPainterPath()
|
2022-03-09 20:00:17 +00:00
|
|
|
self._last_bar_lines: Optional[tuple[QLineF, ...]] = None
|
2020-12-28 20:32:34 +00:00
|
|
|
|
2022-04-04 21:28:56 +00:00
|
|
|
def x_uppx(self) -> int:
|
2022-06-02 17:35:01 +00:00
|
|
|
# we expect the downsample curve report this.
|
|
|
|
return 0
|
2022-04-04 21:28:56 +00:00
|
|
|
|
2022-11-23 18:58:01 +00:00
|
|
|
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
2020-12-28 20:32:34 +00:00
|
|
|
def boundingRect(self):
|
2022-11-23 19:15:06 +00:00
|
|
|
# profiler = Profiler(
|
|
|
|
# msg=f'BarItems.boundingRect(): `{self._name}`',
|
|
|
|
# disabled=not pg_profile_enabled(),
|
|
|
|
# ms_threshold=ms_slower_then,
|
|
|
|
# )
|
2020-12-28 20:32:34 +00:00
|
|
|
|
|
|
|
# TODO: Can we do rect caching to make this faster
|
|
|
|
# like `pg.PlotCurveItem` does? In theory it's just
|
|
|
|
# computing max/min stuff again like we do in the udpate loop
|
|
|
|
# anyway. Not really sure it's necessary since profiling already
|
|
|
|
# shows this method is faf.
|
|
|
|
|
|
|
|
# boundingRect _must_ indicate the entire area that will be
|
|
|
|
# drawn on or else we will get artifacts and possibly crashing.
|
|
|
|
# (in this case, QPicture does all the work of computing the
|
|
|
|
# bounding rect for us).
|
|
|
|
|
|
|
|
# apparently this a lot faster says the docs?
|
|
|
|
# https://doc.qt.io/qt-5/qpainterpath.html#controlPointRect
|
|
|
|
hb = self.path.controlPointRect()
|
2022-04-01 17:28:50 +00:00
|
|
|
hb_tl, hb_br = (
|
|
|
|
hb.topLeft(),
|
|
|
|
hb.bottomRight(),
|
|
|
|
)
|
2021-02-21 17:02:20 +00:00
|
|
|
mn_y = hb_tl.y()
|
2022-11-23 18:58:01 +00:00
|
|
|
mx_y = hb_br.y()
|
|
|
|
most_left = hb_tl.x()
|
|
|
|
most_right = hb_br.x()
|
2022-11-23 19:15:06 +00:00
|
|
|
# profiler('calc path vertices')
|
2020-12-28 20:32:34 +00:00
|
|
|
|
2022-11-23 18:58:01 +00:00
|
|
|
# need to include last bar height or BR will be off
|
|
|
|
# OHLC line segments: [hl, o, c]
|
|
|
|
last_lines: tuple[QLineF] | None = self._last_bar_lines
|
2022-03-21 22:51:59 +00:00
|
|
|
if last_lines:
|
2022-11-23 18:58:01 +00:00
|
|
|
(
|
|
|
|
hl,
|
|
|
|
o,
|
|
|
|
c,
|
|
|
|
) = last_lines
|
|
|
|
most_right = c.x2() + 1
|
|
|
|
ymx = ymn = c.y2()
|
|
|
|
|
|
|
|
if hl:
|
|
|
|
y1, y2 = hl.y1(), hl.y2()
|
|
|
|
ymn = min(y1, y2)
|
|
|
|
ymx = max(y1, y2)
|
|
|
|
mx_y = max(ymx, mx_y)
|
|
|
|
mn_y = min(ymn, mn_y)
|
2022-11-23 19:15:06 +00:00
|
|
|
# profiler('calc last bar vertices')
|
2022-11-23 18:58:01 +00:00
|
|
|
|
|
|
|
return QRectF(
|
|
|
|
most_left,
|
|
|
|
mn_y,
|
|
|
|
most_right - most_left + 1,
|
|
|
|
mx_y - mn_y,
|
2020-12-28 20:32:34 +00:00
|
|
|
)
|
2021-09-21 19:27:45 +00:00
|
|
|
|
|
|
|
def paint(
|
|
|
|
self,
|
|
|
|
p: QtGui.QPainter,
|
|
|
|
opt: QtWidgets.QStyleOptionGraphicsItem,
|
|
|
|
w: QtWidgets.QWidget
|
2022-03-09 16:01:01 +00:00
|
|
|
|
2021-09-21 19:27:45 +00:00
|
|
|
) -> None:
|
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
profiler = Profiler(
|
2022-03-09 16:01:01 +00:00
|
|
|
disabled=not pg_profile_enabled(),
|
2022-05-13 20:23:31 +00:00
|
|
|
ms_threshold=ms_slower_then,
|
2022-03-09 16:01:01 +00:00
|
|
|
)
|
2021-09-21 19:27:45 +00:00
|
|
|
|
|
|
|
# p.setCompositionMode(0)
|
|
|
|
|
|
|
|
# TODO: one thing we could try here is pictures being drawn of
|
|
|
|
# a fixed count of bars such that based on the viewbox indices we
|
|
|
|
# only draw the "rounded up" number of "pictures worth" of bars
|
|
|
|
# as is necesarry for what's in "view". Not sure if this will
|
|
|
|
# lead to any perf gains other then when zoomed in to less bars
|
|
|
|
# in view.
|
|
|
|
p.setPen(self.last_bar_pen)
|
2022-05-21 15:45:44 +00:00
|
|
|
if self._last_bar_lines:
|
|
|
|
p.drawLines(*tuple(filter(bool, self._last_bar_lines)))
|
|
|
|
profiler('draw last bar')
|
2021-09-21 19:27:45 +00:00
|
|
|
|
|
|
|
p.setPen(self.bars_pen)
|
|
|
|
p.drawPath(self.path)
|
2022-04-04 04:10:13 +00:00
|
|
|
profiler(f'draw history path: {self.path.capacity()}')
|
2022-06-03 17:55:34 +00:00
|
|
|
|
|
|
|
def draw_last_datum(
|
|
|
|
self,
|
|
|
|
path: QPainterPath,
|
|
|
|
src_data: np.ndarray,
|
|
|
|
render_data: np.ndarray,
|
|
|
|
reset: bool,
|
|
|
|
array_key: str,
|
|
|
|
|
2022-06-03 20:44:04 +00:00
|
|
|
fields: list[str] = [
|
|
|
|
'index',
|
|
|
|
'open',
|
|
|
|
'high',
|
|
|
|
'low',
|
|
|
|
'close',
|
|
|
|
],
|
|
|
|
|
2022-06-03 17:55:34 +00:00
|
|
|
) -> None:
|
2022-06-03 20:44:04 +00:00
|
|
|
|
|
|
|
# relevant fields
|
|
|
|
ohlc = src_data[fields]
|
2022-11-23 18:58:01 +00:00
|
|
|
# last_row = ohlc[-1:]
|
2022-06-03 20:44:04 +00:00
|
|
|
|
|
|
|
# individual values
|
|
|
|
last_row = i, o, h, l, last = ohlc[-1]
|
2022-06-03 17:55:34 +00:00
|
|
|
|
2022-11-23 18:58:01 +00:00
|
|
|
# times = src_data['time']
|
|
|
|
# if times[-1] - times[-2]:
|
|
|
|
# breakpoint()
|
|
|
|
|
2022-06-03 17:55:34 +00:00
|
|
|
# generate new lines objects for updatable "current bar"
|
2022-06-03 20:44:04 +00:00
|
|
|
self._last_bar_lines = bar_from_ohlc_row(last_row)
|
2022-06-03 17:55:34 +00:00
|
|
|
|
|
|
|
# assert i == graphics.start_index - 1
|
|
|
|
# assert i == last_index
|
|
|
|
body, larm, rarm = self._last_bar_lines
|
|
|
|
|
|
|
|
# XXX: is there a faster way to modify this?
|
|
|
|
rarm.setLine(rarm.x1(), last, rarm.x2(), last)
|
|
|
|
|
|
|
|
# writer is responsible for changing open on "first" volume of bar
|
|
|
|
larm.setLine(larm.x1(), o, larm.x2(), o)
|
|
|
|
|
|
|
|
if l != h: # noqa
|
|
|
|
|
|
|
|
if body is None:
|
|
|
|
body = self._last_bar_lines[0] = QLineF(i, l, i, h)
|
|
|
|
else:
|
|
|
|
# update body
|
|
|
|
body.setLine(i, l, i, h)
|
|
|
|
|
|
|
|
# XXX: pretty sure this is causing an issue where the
|
|
|
|
# bar has a large upward move right before the next
|
|
|
|
# sample and the body is getting set to None since the
|
|
|
|
# next bar is flat but the shm array index update wasn't
|
|
|
|
# read by the time this code runs. Iow we're doing this
|
|
|
|
# removal of the body for a bar index that is now out of
|
|
|
|
# date / from some previous sample. It's weird though
|
|
|
|
# because i've seen it do this to bars i - 3 back?
|
2022-06-03 20:44:04 +00:00
|
|
|
|
2022-11-23 18:58:01 +00:00
|
|
|
# return ohlc['time'], ohlc['close']
|
2022-06-03 20:44:04 +00:00
|
|
|
return ohlc['index'], ohlc['close']
|