2020-06-14 19:09:32 +00:00
|
|
|
"""
|
2020-06-15 14:48:00 +00:00
|
|
|
High level Qt chart widgets.
|
2020-06-14 19:09:32 +00:00
|
|
|
"""
|
2020-09-09 14:47:44 +00:00
|
|
|
from typing import Tuple, Dict, Any, Optional
|
2020-06-19 12:01:10 +00:00
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
from PyQt5 import QtCore, QtGui
|
2020-06-15 14:48:00 +00:00
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
2020-07-17 13:06:20 +00:00
|
|
|
import tractor
|
|
|
|
import trio
|
2020-06-14 19:09:32 +00:00
|
|
|
|
2020-06-15 14:48:00 +00:00
|
|
|
from ._axes import (
|
2020-06-19 12:01:10 +00:00
|
|
|
DynamicDateAxis,
|
2020-06-15 14:48:00 +00:00
|
|
|
PriceAxis,
|
|
|
|
)
|
2020-10-21 14:46:56 +00:00
|
|
|
from ._graphics import (
|
|
|
|
CrossHair,
|
|
|
|
BarItems,
|
|
|
|
h_line,
|
|
|
|
)
|
2020-08-30 16:29:29 +00:00
|
|
|
from ._axes import YSticky
|
2020-09-29 16:28:54 +00:00
|
|
|
from ._style import (
|
2020-10-20 12:43:51 +00:00
|
|
|
hcolor,
|
2020-09-29 16:28:54 +00:00
|
|
|
CHART_MARGINS,
|
2020-10-20 12:43:51 +00:00
|
|
|
_xaxis_at,
|
|
|
|
_min_points_to_show,
|
2020-10-15 19:08:56 +00:00
|
|
|
_bars_from_right_in_follow_mode,
|
2020-10-16 16:18:14 +00:00
|
|
|
_bars_to_left_in_follow_mode,
|
2020-10-19 18:18:06 +00:00
|
|
|
# _font,
|
2020-09-29 16:28:54 +00:00
|
|
|
)
|
2020-09-26 18:11:14 +00:00
|
|
|
from ..data._source import Symbol
|
2020-07-04 21:48:31 +00:00
|
|
|
from .. import brokers
|
2020-07-17 13:06:20 +00:00
|
|
|
from .. import data
|
2020-09-23 00:13:14 +00:00
|
|
|
from ..data import (
|
|
|
|
iterticks,
|
|
|
|
maybe_open_shm_array,
|
|
|
|
)
|
2020-07-17 13:06:20 +00:00
|
|
|
from ..log import get_logger
|
2020-08-03 00:10:06 +00:00
|
|
|
from ._exec import run_qtractor
|
2020-08-19 19:32:09 +00:00
|
|
|
from ._interaction import ChartView
|
2020-08-03 00:10:06 +00:00
|
|
|
from .. import fsp
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
|
2020-07-04 21:48:31 +00:00
|
|
|
log = get_logger(__name__)
|
|
|
|
|
2020-06-14 19:09:32 +00:00
|
|
|
|
2020-06-18 02:56:27 +00:00
|
|
|
class ChartSpace(QtGui.QWidget):
|
|
|
|
"""High level widget which contains layouts for organizing
|
|
|
|
lower level charts as well as other widgets used to control
|
|
|
|
or modify them.
|
|
|
|
"""
|
2020-06-14 19:09:32 +00:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
2020-06-16 17:32:03 +00:00
|
|
|
self.v_layout = QtGui.QVBoxLayout(self)
|
|
|
|
self.v_layout.setContentsMargins(0, 0, 0, 0)
|
2020-06-14 19:09:32 +00:00
|
|
|
self.toolbar_layout = QtGui.QHBoxLayout()
|
2020-08-31 21:18:35 +00:00
|
|
|
self.toolbar_layout.setContentsMargins(5, 5, 10, 0)
|
2020-06-16 17:32:03 +00:00
|
|
|
self.h_layout = QtGui.QHBoxLayout()
|
2020-06-14 19:09:32 +00:00
|
|
|
|
|
|
|
# self.init_timeframes_ui()
|
|
|
|
# self.init_strategy_ui()
|
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
self.v_layout.addLayout(self.toolbar_layout)
|
|
|
|
self.v_layout.addLayout(self.h_layout)
|
2020-08-19 19:32:09 +00:00
|
|
|
self._chart_cache = {}
|
2020-06-14 19:09:32 +00:00
|
|
|
|
|
|
|
def init_timeframes_ui(self):
|
|
|
|
self.tf_layout = QtGui.QHBoxLayout()
|
|
|
|
self.tf_layout.setSpacing(0)
|
|
|
|
self.tf_layout.setContentsMargins(0, 12, 0, 0)
|
|
|
|
time_frames = ('1M', '5M', '15M', '30M', '1H', '1D', '1W', 'MN')
|
|
|
|
btn_prefix = 'TF'
|
|
|
|
for tf in time_frames:
|
|
|
|
btn_name = ''.join([btn_prefix, tf])
|
|
|
|
btn = QtGui.QPushButton(tf)
|
|
|
|
# TODO:
|
|
|
|
btn.setEnabled(False)
|
|
|
|
setattr(self, btn_name, btn)
|
|
|
|
self.tf_layout.addWidget(btn)
|
|
|
|
self.toolbar_layout.addLayout(self.tf_layout)
|
|
|
|
|
|
|
|
# XXX: strat loader/saver that we don't need yet.
|
|
|
|
# def init_strategy_ui(self):
|
|
|
|
# self.strategy_box = StrategyBoxWidget(self)
|
|
|
|
# self.toolbar_layout.addWidget(self.strategy_box)
|
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
def load_symbol(
|
|
|
|
self,
|
|
|
|
symbol: str,
|
|
|
|
data: np.ndarray,
|
|
|
|
) -> None:
|
|
|
|
"""Load a new contract into the charting app.
|
|
|
|
"""
|
|
|
|
# XXX: let's see if this causes mem problems
|
2020-07-17 13:06:20 +00:00
|
|
|
self.window.setWindowTitle(f'piker chart {symbol}')
|
2020-08-19 19:32:09 +00:00
|
|
|
linkedcharts = self._chart_cache.setdefault(
|
|
|
|
symbol,
|
|
|
|
LinkedSplitCharts()
|
|
|
|
)
|
2020-06-16 17:32:03 +00:00
|
|
|
s = Symbol(key=symbol)
|
2020-06-14 19:09:32 +00:00
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
# remove any existing plots
|
|
|
|
if not self.h_layout.isEmpty():
|
2020-08-19 19:32:09 +00:00
|
|
|
self.h_layout.removeWidget(linkedcharts)
|
2020-06-16 17:32:03 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
main_chart = linkedcharts.plot_main(s, data)
|
|
|
|
self.h_layout.addWidget(linkedcharts)
|
|
|
|
return linkedcharts, main_chart
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
# TODO: add signalling painter system
|
|
|
|
# def add_signals(self):
|
|
|
|
# self.chart.add_signals()
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
|
2020-06-18 02:56:27 +00:00
|
|
|
class LinkedSplitCharts(QtGui.QWidget):
|
2020-06-19 12:01:10 +00:00
|
|
|
"""Widget that holds a central chart plus derived
|
|
|
|
subcharts computed from the original data set apart
|
|
|
|
by splitters for resizing.
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
A single internal references to the data is maintained
|
|
|
|
for each chart and can be updated externally.
|
|
|
|
"""
|
2020-06-15 14:48:00 +00:00
|
|
|
long_pen = pg.mkPen('#006000')
|
|
|
|
long_brush = pg.mkBrush('#00ff00')
|
|
|
|
short_pen = pg.mkPen('#600000')
|
|
|
|
short_brush = pg.mkBrush('#ff0000')
|
|
|
|
|
|
|
|
zoomIsDisabled = QtCore.pyqtSignal(bool)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2020-07-17 13:06:20 +00:00
|
|
|
self.signals_visible: bool = False
|
|
|
|
self._array: np.ndarray = None # main data source
|
|
|
|
self._ch: CrossHair = None # crosshair graphics
|
|
|
|
self.chart: ChartPlotWidget = None # main (ohlc) chart
|
2020-08-19 19:32:09 +00:00
|
|
|
self.subplots: Dict[Tuple[str, ...], ChartPlotWidget] = {}
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
self.xaxis = DynamicDateAxis(
|
2020-07-17 13:06:20 +00:00
|
|
|
orientation='bottom',
|
|
|
|
linked_charts=self
|
|
|
|
)
|
2020-06-19 12:01:10 +00:00
|
|
|
self.xaxis_ind = DynamicDateAxis(
|
2020-07-17 13:06:20 +00:00
|
|
|
orientation='bottom',
|
|
|
|
linked_charts=self
|
|
|
|
)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-09-29 16:28:54 +00:00
|
|
|
# if _xaxis_at == 'bottom':
|
|
|
|
# self.xaxis.setStyle(showValues=False)
|
2020-09-29 20:18:21 +00:00
|
|
|
# self.xaxis.hide()
|
2020-09-29 16:28:54 +00:00
|
|
|
# else:
|
|
|
|
# self.xaxis_ind.setStyle(showValues=False)
|
2020-09-29 20:18:21 +00:00
|
|
|
# self.xaxis.hide()
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
self.splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
|
2020-09-29 16:28:54 +00:00
|
|
|
self.splitter.setMidLineWidth(3)
|
|
|
|
self.splitter.setHandleWidth(0)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
self.layout = QtGui.QVBoxLayout(self)
|
|
|
|
self.layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
self.layout.addWidget(self.splitter)
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
def set_split_sizes(
|
|
|
|
self,
|
2020-10-20 12:43:51 +00:00
|
|
|
prop: float = 0.28 # proportion allocated to consumer subcharts
|
2020-06-19 12:01:10 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set the proportion of space allocated for linked subcharts.
|
|
|
|
"""
|
|
|
|
major = 1 - prop
|
2020-08-19 19:32:09 +00:00
|
|
|
min_h_ind = int((self.height() * prop) / len(self.subplots))
|
2020-06-19 12:01:10 +00:00
|
|
|
sizes = [int(self.height() * major)]
|
2020-07-17 13:06:20 +00:00
|
|
|
sizes.extend([min_h_ind] * len(self.subplots))
|
2020-06-15 14:48:00 +00:00
|
|
|
self.splitter.setSizes(sizes) # , int(self.height()*0.2)
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
def plot_main(
|
2020-06-16 17:32:03 +00:00
|
|
|
self,
|
|
|
|
symbol: Symbol,
|
2020-06-19 12:01:10 +00:00
|
|
|
array: np.ndarray,
|
|
|
|
ohlc: bool = True,
|
2020-08-19 19:32:09 +00:00
|
|
|
) -> 'ChartPlotWidget':
|
2020-06-19 12:01:10 +00:00
|
|
|
"""Start up and show main (price) chart and all linked subcharts.
|
2020-06-15 15:40:41 +00:00
|
|
|
"""
|
2020-06-16 17:32:03 +00:00
|
|
|
self.digits = symbol.digits()
|
2020-06-15 15:40:41 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
# TODO: this should eventually be a view onto shared mem or some
|
|
|
|
# higher level type / API
|
2020-06-19 12:01:10 +00:00
|
|
|
self._array = array
|
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
# add crosshairs
|
|
|
|
self._ch = CrossHair(
|
2020-08-26 18:15:52 +00:00
|
|
|
linkedsplitcharts=self,
|
2020-07-17 13:06:20 +00:00
|
|
|
digits=self.digits
|
2020-06-15 14:48:00 +00:00
|
|
|
)
|
2020-07-17 13:06:20 +00:00
|
|
|
self.chart = self.add_plot(
|
2020-08-26 18:15:52 +00:00
|
|
|
name=symbol.key,
|
2020-08-03 00:10:06 +00:00
|
|
|
array=array,
|
2020-07-17 13:06:20 +00:00
|
|
|
xaxis=self.xaxis,
|
|
|
|
ohlc=True,
|
2020-08-26 18:15:52 +00:00
|
|
|
_is_main=True,
|
2020-07-17 13:06:20 +00:00
|
|
|
)
|
2020-08-19 19:32:09 +00:00
|
|
|
# add crosshair graphic
|
2020-07-17 13:06:20 +00:00
|
|
|
self.chart.addItem(self._ch)
|
2020-09-29 16:28:54 +00:00
|
|
|
if _xaxis_at == 'bottom':
|
|
|
|
self.chart.hideAxis('bottom')
|
2020-06-19 12:01:10 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
# style?
|
|
|
|
self.chart.setFrameStyle(QtGui.QFrame.StyledPanel | QtGui.QFrame.Plain)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
return self.chart
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
def add_plot(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
array: np.ndarray,
|
|
|
|
xaxis: DynamicDateAxis = None,
|
|
|
|
ohlc: bool = False,
|
2020-08-26 18:15:52 +00:00
|
|
|
_is_main: bool = False,
|
2020-10-16 16:18:14 +00:00
|
|
|
**cpw_kwargs,
|
2020-07-17 13:06:20 +00:00
|
|
|
) -> 'ChartPlotWidget':
|
|
|
|
"""Add (sub)plots to chart widget by name.
|
|
|
|
|
|
|
|
If ``name`` == ``"main"`` the chart will be the the primary view.
|
|
|
|
"""
|
2020-08-26 18:15:52 +00:00
|
|
|
if self.chart is None and not _is_main:
|
2020-08-19 19:32:09 +00:00
|
|
|
raise RuntimeError(
|
|
|
|
"A main plot must be created first with `.plot_main()`")
|
|
|
|
|
|
|
|
# source of our custom interactions
|
2020-07-17 13:06:20 +00:00
|
|
|
cv = ChartView()
|
2020-08-19 19:32:09 +00:00
|
|
|
cv.linked_charts = self
|
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
# use "indicator axis" by default
|
|
|
|
xaxis = self.xaxis_ind if xaxis is None else xaxis
|
|
|
|
cpw = ChartPlotWidget(
|
2020-08-19 19:32:09 +00:00
|
|
|
array=array,
|
2020-07-17 13:06:20 +00:00
|
|
|
parent=self.splitter,
|
|
|
|
axisItems={'bottom': xaxis, 'right': PriceAxis()},
|
|
|
|
viewBox=cv,
|
2020-10-21 14:46:56 +00:00
|
|
|
cursor=self._ch,
|
2020-10-16 16:18:14 +00:00
|
|
|
**cpw_kwargs,
|
2020-07-17 13:06:20 +00:00
|
|
|
)
|
|
|
|
# this name will be used to register the primary
|
|
|
|
# graphics curve managed by the subchart
|
|
|
|
cpw.name = name
|
|
|
|
cpw.plotItem.vb.linked_charts = self
|
|
|
|
|
2020-09-29 20:18:21 +00:00
|
|
|
cpw.setFrameStyle(QtGui.QFrame.StyledPanel) # | QtGui.QFrame.Plain)
|
2020-07-17 13:06:20 +00:00
|
|
|
cpw.getPlotItem().setContentsMargins(*CHART_MARGINS)
|
2020-09-29 16:28:54 +00:00
|
|
|
cpw.hideButtons()
|
2020-07-17 13:06:20 +00:00
|
|
|
|
|
|
|
# link chart x-axis to main quotes chart
|
|
|
|
cpw.setXLink(self.chart)
|
|
|
|
|
2020-10-21 14:46:56 +00:00
|
|
|
# add to cross-hair's known plots
|
|
|
|
self._ch.add_plot(cpw)
|
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
# draw curve graphics
|
|
|
|
if ohlc:
|
2020-08-26 18:15:52 +00:00
|
|
|
cpw.draw_ohlc(name, array)
|
2020-07-17 13:06:20 +00:00
|
|
|
else:
|
2020-08-26 18:15:52 +00:00
|
|
|
cpw.draw_curve(name, array)
|
2020-07-17 13:06:20 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
if not _is_main:
|
2020-08-19 19:32:09 +00:00
|
|
|
# track by name
|
|
|
|
self.subplots[name] = cpw
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
# scale split regions
|
|
|
|
self.set_split_sizes()
|
2020-06-19 12:01:10 +00:00
|
|
|
|
2020-08-30 16:29:29 +00:00
|
|
|
# XXX: we need this right?
|
|
|
|
# self.splitter.addWidget(cpw)
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
return cpw
|
2020-06-15 15:40:41 +00:00
|
|
|
|
|
|
|
|
2020-06-15 14:48:00 +00:00
|
|
|
class ChartPlotWidget(pg.PlotWidget):
|
|
|
|
"""``GraphicsView`` subtype containing a single ``PlotItem``.
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
- The added methods allow for plotting OHLC sequences from
|
2020-07-04 21:48:31 +00:00
|
|
|
``np.ndarray``s with appropriate field names.
|
2020-06-19 12:01:10 +00:00
|
|
|
- Overrides a ``pyqtgraph.PlotWidget`` (a ``GraphicsView`` containing
|
|
|
|
a single ``PlotItem``) to intercept and and re-emit mouse enter/exit
|
|
|
|
events.
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
(Could be replaced with a ``pg.GraphicsLayoutWidget`` if we
|
2020-06-19 12:01:10 +00:00
|
|
|
eventually want multiple plots managed together?)
|
2020-06-15 14:48:00 +00:00
|
|
|
"""
|
|
|
|
sig_mouse_leave = QtCore.Signal(object)
|
|
|
|
sig_mouse_enter = QtCore.Signal(object)
|
|
|
|
|
2020-06-16 17:32:03 +00:00
|
|
|
# TODO: can take a ``background`` color setting - maybe there's
|
|
|
|
# a better one?
|
|
|
|
|
2020-06-15 14:48:00 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2020-08-19 19:32:09 +00:00
|
|
|
# the data view we generate graphics from
|
|
|
|
array: np.ndarray,
|
2020-10-16 16:18:14 +00:00
|
|
|
static_yrange: Optional[Tuple[float, float]] = None,
|
2020-10-21 14:46:56 +00:00
|
|
|
cursor: Optional[CrossHair] = None,
|
2020-06-15 14:48:00 +00:00
|
|
|
**kwargs,
|
|
|
|
):
|
|
|
|
"""Configure chart display settings.
|
|
|
|
"""
|
2020-08-31 21:18:35 +00:00
|
|
|
super().__init__(
|
|
|
|
background=hcolor('papas_special'),
|
|
|
|
# parent=None,
|
|
|
|
# plotItem=None,
|
2020-10-19 18:18:06 +00:00
|
|
|
# antialias=True,
|
2020-08-31 21:18:35 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
2020-08-19 19:32:09 +00:00
|
|
|
self._array = array # readonly view of data
|
|
|
|
self._graphics = {} # registry of underlying graphics
|
2020-09-11 23:33:05 +00:00
|
|
|
self._overlays = {} # registry of overlay curves
|
2020-08-26 18:15:52 +00:00
|
|
|
self._labels = {} # registry of underlying graphics
|
2020-08-30 16:29:29 +00:00
|
|
|
self._ysticks = {} # registry of underlying graphics
|
2020-09-09 14:47:44 +00:00
|
|
|
self._vb = self.plotItem.vb
|
2020-10-16 16:18:14 +00:00
|
|
|
self._static_yrange = static_yrange # for "known y-range style"
|
2020-10-20 12:43:51 +00:00
|
|
|
self._view_mode: str = 'follow'
|
2020-10-21 14:46:56 +00:00
|
|
|
self._cursor = cursor # placehold for mouse
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-06-15 14:48:00 +00:00
|
|
|
# show only right side axes
|
|
|
|
self.hideAxis('left')
|
|
|
|
self.showAxis('right')
|
|
|
|
|
|
|
|
# show background grid
|
|
|
|
self.showGrid(x=True, y=True, alpha=0.4)
|
|
|
|
|
2020-10-16 16:18:14 +00:00
|
|
|
# use cross-hair for cursor?
|
|
|
|
# self.setCursor(QtCore.Qt.CrossCursor)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
# Assign callback for rescaling y-axis automatically
|
|
|
|
# based on data contents and ``ViewBox`` state.
|
2020-06-19 12:01:10 +00:00
|
|
|
self.sigXRangeChanged.connect(self._set_yrange)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
# for mouse wheel which doesn't seem to emit XRangeChanged
|
2020-10-16 16:18:14 +00:00
|
|
|
self._vb.sigRangeChangedManually.connect(self._set_yrange)
|
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
# for when the splitter(s) are resized
|
2020-10-16 16:18:14 +00:00
|
|
|
self._vb.sigResized.connect(self._set_yrange)
|
2020-09-09 14:47:44 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
def last_bar_in_view(self) -> bool:
|
|
|
|
self._array[-1]['index']
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
def _update_contents_label(self, index: int) -> None:
|
2020-09-11 23:33:05 +00:00
|
|
|
if index >= 0 and index < len(self._array):
|
2020-08-26 18:15:52 +00:00
|
|
|
for name, (label, update) in self._labels.items():
|
|
|
|
update(index)
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
def _set_xlimits(
|
|
|
|
self,
|
|
|
|
xfirst: int,
|
|
|
|
xlast: int
|
|
|
|
) -> None:
|
|
|
|
"""Set view limits (what's shown in the main chart "pane")
|
2020-07-17 13:06:20 +00:00
|
|
|
based on max/min x/y coords.
|
2020-06-19 12:01:10 +00:00
|
|
|
"""
|
2020-06-15 14:48:00 +00:00
|
|
|
self.setLimits(
|
2020-06-17 15:45:43 +00:00
|
|
|
xMin=xfirst,
|
2020-07-04 21:48:31 +00:00
|
|
|
xMax=xlast,
|
2020-06-15 15:40:41 +00:00
|
|
|
minXRange=_min_points_to_show,
|
2020-06-15 14:48:00 +00:00
|
|
|
)
|
|
|
|
|
2020-07-04 21:48:31 +00:00
|
|
|
def view_range(self) -> Tuple[int, int]:
|
|
|
|
vr = self.viewRect()
|
|
|
|
return int(vr.left()), int(vr.right())
|
|
|
|
|
|
|
|
def bars_range(self) -> Tuple[int, int, int, int]:
|
2020-06-15 14:48:00 +00:00
|
|
|
"""Return a range tuple for the bars present in view.
|
|
|
|
"""
|
2020-07-04 21:48:31 +00:00
|
|
|
l, r = self.view_range()
|
|
|
|
lbar = max(l, 0)
|
2020-08-19 19:32:09 +00:00
|
|
|
rbar = min(r, len(self._array))
|
2020-07-04 21:48:31 +00:00
|
|
|
return l, lbar, rbar, r
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
def draw_ohlc(
|
|
|
|
self,
|
2020-08-26 18:15:52 +00:00
|
|
|
name: str,
|
2020-06-16 17:32:03 +00:00
|
|
|
data: np.ndarray,
|
2020-06-17 15:45:43 +00:00
|
|
|
# XXX: pretty sure this is dumb and we don't need an Enum
|
2020-08-19 19:32:09 +00:00
|
|
|
style: pg.GraphicsObject = BarItems,
|
|
|
|
) -> pg.GraphicsObject:
|
2020-06-15 14:48:00 +00:00
|
|
|
"""Draw OHLC datums to chart.
|
|
|
|
"""
|
2020-09-07 20:41:11 +00:00
|
|
|
graphics = style(self.plotItem)
|
2020-06-17 15:45:43 +00:00
|
|
|
# adds all bar/candle graphics objects for each data point in
|
|
|
|
# the np array buffer to be drawn on next render cycle
|
2020-06-16 17:32:03 +00:00
|
|
|
self.addItem(graphics)
|
2020-10-16 16:18:14 +00:00
|
|
|
|
2020-09-07 20:41:11 +00:00
|
|
|
# draw after to allow self.scene() to work...
|
|
|
|
graphics.draw_from_data(data)
|
2020-06-19 12:01:10 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
self._graphics[name] = graphics
|
|
|
|
|
|
|
|
# XXX: How to stack labels vertically?
|
2020-09-07 20:41:11 +00:00
|
|
|
# Ogi says: "use ..."
|
2020-08-26 18:15:52 +00:00
|
|
|
label = pg.LabelItem(
|
|
|
|
justify='left',
|
2020-10-19 18:18:06 +00:00
|
|
|
size='6px',
|
2020-08-26 18:15:52 +00:00
|
|
|
)
|
2020-10-19 18:18:06 +00:00
|
|
|
label.setParentItem(self._vb)
|
2020-08-26 18:15:52 +00:00
|
|
|
self.scene().addItem(label)
|
|
|
|
|
2020-10-19 18:18:06 +00:00
|
|
|
# keep close to top
|
|
|
|
label.anchor(itemPos=(0, 0), parentPos=(0, 0), offset=(0, -4))
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
def update(index: int) -> None:
|
|
|
|
label.setText(
|
2020-09-09 14:47:44 +00:00
|
|
|
"{name}[{index}] -> O:{} H:{} L:{} C:{} V:{}".format(
|
2020-09-26 18:12:33 +00:00
|
|
|
*self._array[index].item()[2:8],
|
2020-08-26 18:15:52 +00:00
|
|
|
name=name,
|
2020-09-09 14:47:44 +00:00
|
|
|
index=index,
|
2020-08-26 18:15:52 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self._labels[name] = (label, update)
|
2020-10-16 16:18:14 +00:00
|
|
|
self._update_contents_label(len(data) - 1)
|
2020-09-11 17:16:11 +00:00
|
|
|
label.show()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
self._add_sticky(name)
|
|
|
|
|
|
|
|
return graphics
|
|
|
|
|
|
|
|
def default_view(
|
|
|
|
self,
|
|
|
|
index: int = -1,
|
|
|
|
) -> None:
|
|
|
|
"""Set the view box to the "default" startup view of the scene.
|
|
|
|
|
|
|
|
"""
|
|
|
|
xlast = self._array[index]['index']
|
|
|
|
begin = xlast - _bars_to_left_in_follow_mode
|
|
|
|
end = xlast + _bars_from_right_in_follow_mode
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-10-15 19:08:56 +00:00
|
|
|
self.plotItem.vb.setXRange(
|
2020-10-20 12:43:51 +00:00
|
|
|
min=begin,
|
|
|
|
max=end,
|
|
|
|
padding=0,
|
2020-10-15 19:08:56 +00:00
|
|
|
)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
def increment_view(
|
|
|
|
self,
|
|
|
|
) -> None:
|
|
|
|
"""Increment the data view one step to the right thus "following"
|
|
|
|
the current time slot/step/bar.
|
2020-08-30 16:29:29 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
"""
|
|
|
|
l, r = self.view_range()
|
|
|
|
self._vb.setXRange(
|
|
|
|
min=l + 1,
|
|
|
|
max=r + 1,
|
|
|
|
# holy shit, wtf dude... why tf would this not be 0 by
|
|
|
|
# default... speechless.
|
|
|
|
padding=0,
|
|
|
|
)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
def draw_curve(
|
|
|
|
self,
|
2020-08-26 18:15:52 +00:00
|
|
|
name: str,
|
2020-06-15 14:48:00 +00:00
|
|
|
data: np.ndarray,
|
2020-09-11 17:16:11 +00:00
|
|
|
overlay: bool = False,
|
|
|
|
**pdi_kwargs,
|
2020-08-19 19:32:09 +00:00
|
|
|
) -> pg.PlotDataItem:
|
2020-06-15 14:48:00 +00:00
|
|
|
# draw the indicator as a plain curve
|
2020-09-11 17:16:11 +00:00
|
|
|
_pdi_defaults = {
|
|
|
|
'pen': pg.mkPen(hcolor('default_light')),
|
|
|
|
}
|
|
|
|
pdi_kwargs.update(_pdi_defaults)
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
curve = pg.PlotDataItem(
|
2020-10-20 12:43:51 +00:00
|
|
|
data[name],
|
2020-10-19 18:18:06 +00:00
|
|
|
# antialias=True,
|
2020-09-11 17:16:11 +00:00
|
|
|
name=name,
|
2020-08-19 19:32:09 +00:00
|
|
|
# TODO: see how this handles with custom ohlcv bars graphics
|
|
|
|
clipToView=True,
|
2020-09-11 17:16:11 +00:00
|
|
|
**pdi_kwargs,
|
2020-08-19 19:32:09 +00:00
|
|
|
)
|
2020-06-15 14:48:00 +00:00
|
|
|
self.addItem(curve)
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
# register overlay curve with name
|
|
|
|
self._graphics[name] = curve
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# XXX: How to stack labels vertically?
|
|
|
|
label = pg.LabelItem(
|
|
|
|
justify='left',
|
2020-10-16 16:18:14 +00:00
|
|
|
size='6px',
|
2020-08-26 18:15:52 +00:00
|
|
|
)
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# anchor to the viewbox
|
2020-09-11 17:16:11 +00:00
|
|
|
label.setParentItem(self._vb)
|
2020-09-29 16:28:54 +00:00
|
|
|
# label.setParentItem(self.getPlotItem())
|
2020-09-11 23:33:05 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
if overlay:
|
|
|
|
# position bottom left if an overlay
|
2020-10-19 18:18:06 +00:00
|
|
|
label.anchor(itemPos=(1, 1), parentPos=(1, 1), offset=(0, 3))
|
2020-09-11 23:33:05 +00:00
|
|
|
self._overlays[name] = curve
|
2020-09-11 17:16:11 +00:00
|
|
|
|
2020-10-19 18:18:06 +00:00
|
|
|
else:
|
|
|
|
label.anchor(itemPos=(0, 0), parentPos=(0, 0), offset=(0, -4))
|
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
def update(index: int) -> None:
|
|
|
|
data = self._array[index][name]
|
|
|
|
label.setText(f"{name}: {data:.2f}")
|
2020-10-19 18:18:06 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
label.show()
|
2020-08-26 18:15:52 +00:00
|
|
|
self.scene().addItem(label)
|
|
|
|
|
|
|
|
self._labels[name] = (label, update)
|
2020-10-16 16:18:14 +00:00
|
|
|
self._update_contents_label(len(data) - 1)
|
2020-08-26 18:15:52 +00:00
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
self._add_sticky(name)
|
|
|
|
|
2020-10-21 14:46:56 +00:00
|
|
|
if self._cursor:
|
|
|
|
self._cursor.add_curve_cursor(self, curve)
|
|
|
|
|
2020-06-17 15:45:43 +00:00
|
|
|
return curve
|
|
|
|
|
2020-08-30 16:29:29 +00:00
|
|
|
def _add_sticky(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
# retreive: Callable[None, np.ndarray],
|
|
|
|
) -> YSticky:
|
|
|
|
# add y-axis "last" value label
|
2020-09-09 14:47:44 +00:00
|
|
|
last = self._ysticks[name] = YSticky(
|
2020-08-30 16:29:29 +00:00
|
|
|
chart=self,
|
|
|
|
parent=self.getAxis('right'),
|
|
|
|
# digits=0,
|
|
|
|
opacity=1,
|
2020-09-23 00:57:05 +00:00
|
|
|
color=pg.mkPen(hcolor('pikers'))
|
2020-08-30 16:29:29 +00:00
|
|
|
)
|
|
|
|
return last
|
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
def update_ohlc_from_array(
|
2020-06-19 12:01:10 +00:00
|
|
|
self,
|
2020-08-19 19:32:09 +00:00
|
|
|
name: str,
|
2020-06-19 12:01:10 +00:00
|
|
|
array: np.ndarray,
|
2020-07-08 19:42:05 +00:00
|
|
|
**kwargs,
|
|
|
|
) -> pg.GraphicsObject:
|
2020-10-20 12:43:51 +00:00
|
|
|
"""Update the named internal graphics from ``array``.
|
|
|
|
|
|
|
|
"""
|
2020-09-26 18:12:33 +00:00
|
|
|
if name not in self._overlays:
|
|
|
|
self._array = array
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
graphics = self._graphics[name]
|
2020-07-08 19:42:05 +00:00
|
|
|
graphics.update_from_array(array, **kwargs)
|
|
|
|
return graphics
|
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
def update_curve_from_array(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
array: np.ndarray,
|
|
|
|
**kwargs,
|
|
|
|
) -> pg.GraphicsObject:
|
|
|
|
"""Update the named internal graphics from ``array``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if name not in self._overlays:
|
|
|
|
self._array = array
|
|
|
|
|
|
|
|
curve = self._graphics[name]
|
|
|
|
# TODO: we should instead implement a diff based
|
|
|
|
# "only update with new items" on the pg.PlotDataItem
|
|
|
|
curve.setData(array[name], **kwargs)
|
|
|
|
return curve
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
def _set_yrange(
|
|
|
|
self,
|
2020-09-11 17:16:11 +00:00
|
|
|
*,
|
|
|
|
yrange: Optional[Tuple[float, float]] = None,
|
2020-06-19 12:01:10 +00:00
|
|
|
) -> None:
|
2020-07-08 19:42:05 +00:00
|
|
|
"""Set the viewable y-range based on embedded data.
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
This adds auto-scaling like zoom on the scroll wheel such
|
|
|
|
that data always fits nicely inside the current view of the
|
|
|
|
data set.
|
2020-09-11 17:16:11 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
"""
|
2020-10-16 16:18:14 +00:00
|
|
|
if self._static_yrange is not None:
|
|
|
|
ylow, yhigh = self._static_yrange
|
|
|
|
|
|
|
|
else: # determine max, min y values in viewable x-range
|
|
|
|
|
|
|
|
l, lbar, rbar, r = self.bars_range()
|
|
|
|
|
|
|
|
# figure out x-range in view such that user can scroll "off"
|
|
|
|
# the data set up to the point where ``_min_points_to_show``
|
|
|
|
# are left.
|
|
|
|
view_len = r - l
|
2020-10-20 12:43:51 +00:00
|
|
|
|
2020-10-16 16:18:14 +00:00
|
|
|
# TODO: logic to check if end of bars in view
|
|
|
|
extra = view_len - _min_points_to_show
|
|
|
|
begin = 0 - extra
|
|
|
|
end = len(self._array) - 1 + extra
|
|
|
|
|
|
|
|
# XXX: test code for only rendering lines for the bars in view.
|
|
|
|
# This turns out to be very very poor perf when scaling out to
|
|
|
|
# many bars (think > 1k) on screen.
|
|
|
|
# name = self.name
|
|
|
|
# bars = self._graphics[self.name]
|
|
|
|
# bars.draw_lines(
|
|
|
|
# istart=max(lbar, l), iend=min(rbar, r), just_history=True)
|
|
|
|
|
|
|
|
# bars_len = rbar - lbar
|
|
|
|
# log.trace(
|
|
|
|
# f"\nl: {l}, lbar: {lbar}, rbar: {rbar}, r: {r}\n"
|
|
|
|
# f"view_len: {view_len}, bars_len: {bars_len}\n"
|
|
|
|
# f"begin: {begin}, end: {end}, extra: {extra}"
|
|
|
|
# )
|
|
|
|
self._set_xlimits(begin, end)
|
2020-09-11 17:16:11 +00:00
|
|
|
|
|
|
|
# TODO: this should be some kind of numpy view api
|
|
|
|
bars = self._array[lbar:rbar]
|
|
|
|
if not len(bars):
|
2020-10-16 16:18:14 +00:00
|
|
|
# likely no data loaded yet or extreme scrolling?
|
2020-09-11 17:16:11 +00:00
|
|
|
log.error(f"WTF bars_range = {lbar}:{rbar}")
|
|
|
|
return
|
|
|
|
|
|
|
|
# TODO: should probably just have some kinda attr mark
|
|
|
|
# that determines this behavior based on array type
|
|
|
|
try:
|
|
|
|
ylow = np.nanmin(bars['low'])
|
|
|
|
yhigh = np.nanmax(bars['high'])
|
2020-09-23 00:13:14 +00:00
|
|
|
except (IndexError, ValueError):
|
2020-09-11 17:16:11 +00:00
|
|
|
# must be non-ohlc array?
|
|
|
|
ylow = np.nanmin(bars)
|
|
|
|
yhigh = np.nanmax(bars)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-10-19 18:18:06 +00:00
|
|
|
# view margins: stay within a % of the "true range"
|
|
|
|
diff = yhigh - ylow
|
|
|
|
ylow = ylow - (diff * 0.04)
|
|
|
|
# yhigh = yhigh + (diff * 0.01)
|
2020-09-09 14:47:44 +00:00
|
|
|
|
|
|
|
# compute contents label "height" in view terms
|
2020-10-16 16:18:14 +00:00
|
|
|
# to avoid having data "contents" overlap with them
|
2020-09-09 14:47:44 +00:00
|
|
|
if self._labels:
|
|
|
|
label = self._labels[self.name][0]
|
2020-10-19 18:18:06 +00:00
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
rect = label.itemRect()
|
|
|
|
tl, br = rect.topLeft(), rect.bottomRight()
|
|
|
|
vb = self.plotItem.vb
|
2020-10-19 18:18:06 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
try:
|
|
|
|
# on startup labels might not yet be rendered
|
|
|
|
top, bottom = (vb.mapToView(tl).y(), vb.mapToView(br).y())
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# XXX: hack, how do we compute exactly?
|
|
|
|
label_h = (top - bottom) * 0.42
|
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
except np.linalg.LinAlgError:
|
|
|
|
label_h = 0
|
2020-09-09 14:47:44 +00:00
|
|
|
else:
|
|
|
|
label_h = 0
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-10-19 18:18:06 +00:00
|
|
|
# print(f'label height {self.name}: {label_h}')
|
|
|
|
|
2020-10-16 16:18:14 +00:00
|
|
|
if label_h > yhigh - ylow:
|
|
|
|
label_h = 0
|
|
|
|
|
|
|
|
self.setLimits(
|
2020-06-17 15:45:43 +00:00
|
|
|
yMin=ylow,
|
2020-09-09 14:47:44 +00:00
|
|
|
yMax=yhigh + label_h,
|
2020-06-17 15:45:43 +00:00
|
|
|
)
|
2020-10-16 16:18:14 +00:00
|
|
|
self.setYRange(ylow, yhigh + label_h)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
|
|
|
def enterEvent(self, ev): # noqa
|
|
|
|
# pg.PlotWidget.enterEvent(self, ev)
|
|
|
|
self.sig_mouse_enter.emit(self)
|
|
|
|
|
|
|
|
def leaveEvent(self, ev): # noqa
|
|
|
|
# pg.PlotWidget.leaveEvent(self, ev)
|
|
|
|
self.sig_mouse_leave.emit(self)
|
|
|
|
self.scene().leaveEvent(ev)
|
|
|
|
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
async def _async_main(
|
|
|
|
sym: str,
|
|
|
|
brokername: str,
|
|
|
|
|
|
|
|
# implicit required argument provided by ``qtractor_run()``
|
|
|
|
widgets: Dict[str, Any],
|
|
|
|
|
|
|
|
# all kwargs are passed through from the CLI entrypoint
|
|
|
|
loglevel: str = None,
|
|
|
|
) -> None:
|
|
|
|
"""Main Qt-trio routine invoked by the Qt loop with
|
|
|
|
the widgets ``dict``.
|
|
|
|
"""
|
|
|
|
chart_app = widgets['main']
|
|
|
|
|
|
|
|
# historical data fetch
|
|
|
|
brokermod = brokers.get_brokermod(brokername)
|
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
async with data.open_feed(
|
|
|
|
brokername,
|
|
|
|
[sym],
|
|
|
|
loglevel=loglevel,
|
2020-09-23 00:13:14 +00:00
|
|
|
) as feed:
|
2020-09-17 13:25:30 +00:00
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
ohlcv = feed.shm
|
2020-09-17 13:25:30 +00:00
|
|
|
bars = ohlcv.array
|
|
|
|
|
|
|
|
# load in symbol's ohlc data
|
|
|
|
linked_charts, chart = chart_app.load_symbol(sym, bars)
|
|
|
|
|
|
|
|
# plot historical vwap if available
|
|
|
|
vwap_in_history = False
|
|
|
|
if 'vwap' in bars.dtype.fields:
|
|
|
|
vwap_in_history = True
|
|
|
|
chart.draw_curve(
|
|
|
|
name='vwap',
|
2020-10-20 12:43:51 +00:00
|
|
|
data=bars,
|
2020-09-17 13:25:30 +00:00
|
|
|
overlay=True,
|
|
|
|
)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
chart._set_yrange()
|
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
async with trio.open_nursery() as n:
|
|
|
|
|
|
|
|
# load initial fsp chain (otherwise known as "indicators")
|
|
|
|
n.start_soon(
|
|
|
|
chart_from_fsp,
|
|
|
|
linked_charts,
|
|
|
|
'rsi', # eventually will be n-compose syntax
|
|
|
|
sym,
|
2020-09-23 00:13:14 +00:00
|
|
|
ohlcv,
|
2020-09-17 13:25:30 +00:00
|
|
|
brokermod,
|
|
|
|
loglevel,
|
|
|
|
)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
# update last price sticky
|
|
|
|
last_price_sticky = chart._ysticks[chart.name]
|
|
|
|
last_price_sticky.update_from_data(
|
|
|
|
*ohlcv.array[-1][['index', 'close']]
|
|
|
|
)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
# start graphics update loop(s)after receiving first live quote
|
|
|
|
n.start_soon(
|
|
|
|
chart_from_quotes,
|
|
|
|
chart,
|
2020-09-23 00:13:14 +00:00
|
|
|
feed.stream,
|
2020-09-17 13:25:30 +00:00
|
|
|
ohlcv,
|
|
|
|
vwap_in_history,
|
|
|
|
)
|
2020-09-23 00:13:14 +00:00
|
|
|
|
|
|
|
# wait for a first quote before we start any update tasks
|
|
|
|
quote = await feed.receive()
|
|
|
|
log.info(f'Received first quote {quote}')
|
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
n.start_soon(
|
|
|
|
check_for_new_bars,
|
2020-09-23 00:13:14 +00:00
|
|
|
feed,
|
2020-09-17 17:22:01 +00:00
|
|
|
# delay,
|
2020-09-17 13:25:30 +00:00
|
|
|
ohlcv,
|
|
|
|
linked_charts
|
|
|
|
)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
# probably where we'll eventually start the user input loop
|
|
|
|
await trio.sleep_forever()
|
2020-08-30 16:29:29 +00:00
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
async def chart_from_quotes(
|
|
|
|
chart: ChartPlotWidget,
|
|
|
|
stream,
|
|
|
|
ohlcv: np.ndarray,
|
|
|
|
vwap_in_history: bool = False,
|
|
|
|
) -> None:
|
|
|
|
"""The 'main' (price) chart real-time update loop.
|
|
|
|
"""
|
2020-09-17 17:22:01 +00:00
|
|
|
# TODO: bunch of stuff:
|
|
|
|
# - I'm starting to think all this logic should be
|
|
|
|
# done in one place and "graphics update routines"
|
|
|
|
# should not be doing any length checking and array diffing.
|
|
|
|
# - handle odd lot orders
|
|
|
|
# - update last open price correctly instead
|
|
|
|
# of copying it from last bar's close
|
|
|
|
# - 5 sec bar lookback-autocorrection like tws does?
|
2020-09-17 13:25:30 +00:00
|
|
|
last_price_sticky = chart._ysticks[chart.name]
|
|
|
|
|
2020-09-29 20:18:21 +00:00
|
|
|
def maxmin():
|
|
|
|
array = chart._array
|
|
|
|
last_bars_range = chart.bars_range()
|
|
|
|
l, lbar, rbar, r = last_bars_range
|
|
|
|
in_view = array[lbar:rbar]
|
|
|
|
mx, mn = np.nanmax(in_view['high']), np.nanmin(in_view['low'])
|
|
|
|
return last_bars_range, mx, mn
|
|
|
|
|
|
|
|
last_bars_range, last_mx, last_mn = maxmin()
|
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.default_view()
|
|
|
|
|
2020-09-17 13:25:30 +00:00
|
|
|
async for quotes in stream:
|
|
|
|
for sym, quote in quotes.items():
|
|
|
|
for tick in iterticks(quote, type='trade'):
|
2020-09-17 17:22:01 +00:00
|
|
|
array = ohlcv.array
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# update price sticky(s)
|
|
|
|
last = array[-1]
|
|
|
|
last_price_sticky.update_from_data(*last[['index', 'close']])
|
|
|
|
|
|
|
|
# update price bar
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.update_ohlc_from_array(
|
2020-09-17 13:25:30 +00:00
|
|
|
chart.name,
|
2020-09-17 17:22:01 +00:00
|
|
|
array,
|
2020-09-17 13:25:30 +00:00
|
|
|
)
|
2020-09-29 20:18:21 +00:00
|
|
|
|
|
|
|
# TODO: we need a streaming minmax algorithm here to
|
|
|
|
brange, mx, mn = maxmin()
|
|
|
|
if brange != last_bars_range:
|
|
|
|
if mx > last_mx or mn < last_mn:
|
|
|
|
# avoid running this every cycle.
|
|
|
|
chart._set_yrange()
|
|
|
|
|
|
|
|
# save for next cycle
|
|
|
|
last_mx, last_mn = mx, mn
|
2020-09-17 13:25:30 +00:00
|
|
|
|
2020-09-26 18:12:33 +00:00
|
|
|
if vwap_in_history:
|
2020-09-17 13:25:30 +00:00
|
|
|
# update vwap overlay line
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.update_curve_from_array('vwap', ohlcv.array)
|
2020-09-11 17:16:11 +00:00
|
|
|
|
2020-09-29 20:18:21 +00:00
|
|
|
# TODO:
|
|
|
|
# - eventually we'll want to update bid/ask labels and
|
|
|
|
# other data as subscribed by underlying UI consumers.
|
|
|
|
# - in theory we should be able to read buffer data
|
|
|
|
# faster then msgs arrive.. needs some tinkering and testing
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
async def chart_from_fsp(
|
|
|
|
linked_charts,
|
2020-10-19 18:18:06 +00:00
|
|
|
fsp_func_name,
|
2020-08-19 19:32:09 +00:00
|
|
|
sym,
|
2020-09-23 00:13:14 +00:00
|
|
|
src_shm,
|
2020-08-19 19:32:09 +00:00
|
|
|
brokermod,
|
|
|
|
loglevel,
|
|
|
|
) -> None:
|
|
|
|
"""Start financial signal processing in subactor.
|
|
|
|
|
|
|
|
Pass target entrypoint and historical data.
|
|
|
|
"""
|
2020-10-19 18:18:06 +00:00
|
|
|
name = f'fsp.{fsp_func_name}'
|
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
# TODO: load function here and introspect
|
|
|
|
# return stream type(s)
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# TODO: should `index` be a required internal field?
|
|
|
|
fsp_dtype = np.dtype([('index', int), (fsp_func_name, float)])
|
2020-09-23 00:13:14 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
async with tractor.open_nursery() as n:
|
2020-09-23 00:13:14 +00:00
|
|
|
key = f'{sym}.' + name
|
|
|
|
|
|
|
|
shm, opened = maybe_open_shm_array(
|
|
|
|
key,
|
|
|
|
# TODO: create entry for each time frame
|
|
|
|
dtype=fsp_dtype,
|
|
|
|
readonly=True,
|
|
|
|
)
|
2020-10-19 18:18:06 +00:00
|
|
|
|
2020-10-15 19:08:56 +00:00
|
|
|
# XXX: fsp may have been opened by a duplicate chart. Error for
|
|
|
|
# now until we figure out how to wrap fsps as "feeds".
|
|
|
|
assert opened, f"A chart for {key} likely already exists?"
|
2020-09-23 00:13:14 +00:00
|
|
|
|
|
|
|
# start fsp sub-actor
|
2020-08-19 19:32:09 +00:00
|
|
|
portal = await n.run_in_actor(
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# name as title of sub-chart
|
|
|
|
name,
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# subactor entrypoint
|
2020-09-23 00:13:14 +00:00
|
|
|
fsp.cascade,
|
2020-08-19 19:32:09 +00:00
|
|
|
brokername=brokermod.name,
|
2020-09-23 00:13:14 +00:00
|
|
|
src_shm_token=src_shm.token,
|
|
|
|
dst_shm_token=shm.token,
|
2020-08-19 19:32:09 +00:00
|
|
|
symbol=sym,
|
2020-10-19 18:18:06 +00:00
|
|
|
fsp_func_name=fsp_func_name,
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# tractor config
|
|
|
|
loglevel=loglevel,
|
|
|
|
)
|
|
|
|
|
|
|
|
stream = await portal.result()
|
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
# receive last index for processed historical
|
|
|
|
# data-array as first msg
|
|
|
|
_ = await stream.receive()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
chart = linked_charts.add_plot(
|
2020-10-19 18:18:06 +00:00
|
|
|
name=fsp_func_name,
|
2020-10-20 12:43:51 +00:00
|
|
|
array=shm.array,
|
|
|
|
|
|
|
|
# curve by default
|
|
|
|
ohlc=False,
|
2020-10-16 16:18:14 +00:00
|
|
|
|
|
|
|
# settings passed down to ``ChartPlotWidget``
|
|
|
|
static_yrange=(0, 100),
|
2020-09-11 23:33:05 +00:00
|
|
|
)
|
|
|
|
|
2020-10-16 16:18:14 +00:00
|
|
|
# display contents labels asap
|
|
|
|
chart._update_contents_label(len(shm.array) - 1)
|
|
|
|
|
2020-10-19 18:18:06 +00:00
|
|
|
array = shm.array
|
|
|
|
value = array[fsp_func_name][-1]
|
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
last_val_sticky = chart._ysticks[chart.name]
|
|
|
|
last_val_sticky.update_from_data(-1, value)
|
2020-10-19 18:18:06 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.update_curve_from_array(fsp_func_name, array)
|
|
|
|
chart.default_view()
|
2020-10-19 18:18:06 +00:00
|
|
|
|
|
|
|
# TODO: figure out if we can roll our own `FillToThreshold` to
|
|
|
|
# get brush filled polygons for OS/OB conditions.
|
|
|
|
# ``pg.FillBetweenItems`` seems to be one technique using
|
|
|
|
# generic fills between curve types while ``PlotCurveItem`` has
|
|
|
|
# logic inside ``.paint()`` for ``self.opts['fillLevel']`` which
|
|
|
|
# might be the best solution?
|
|
|
|
# graphics = chart.update_from_array(chart.name, array[fsp_func_name])
|
|
|
|
# graphics.curve.setBrush(50, 50, 200, 100)
|
|
|
|
# graphics.curve.setFillLevel(50)
|
|
|
|
|
|
|
|
# add moveable over-[sold/bought] lines
|
|
|
|
chart.plotItem.addItem(h_line(30))
|
|
|
|
chart.plotItem.addItem(h_line(70))
|
2020-09-09 14:47:44 +00:00
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
chart._shm = shm
|
2020-10-16 16:18:14 +00:00
|
|
|
chart._set_yrange()
|
2020-09-23 00:13:14 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# update chart graphics
|
2020-08-19 19:32:09 +00:00
|
|
|
async for value in stream:
|
2020-10-19 18:18:06 +00:00
|
|
|
# p = pg.debug.Profiler(disabled=False, delayed=False)
|
|
|
|
array = shm.array
|
|
|
|
value = array[-1][fsp_func_name]
|
2020-09-09 14:47:44 +00:00
|
|
|
last_val_sticky.update_from_data(-1, value)
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.update_curve_from_array(fsp_func_name, array)
|
2020-10-19 18:18:06 +00:00
|
|
|
# p('rendered rsi datum')
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
async def check_for_new_bars(feed, ohlcv, linked_charts):
|
|
|
|
"""Task which updates from new bars in the shared ohlcv buffer every
|
|
|
|
``delay_s`` seconds.
|
|
|
|
"""
|
|
|
|
# TODO: right now we'll spin printing bars if the last time
|
|
|
|
# stamp is before a large period of no market activity.
|
|
|
|
# Likely the best way to solve this is to make this task
|
|
|
|
# aware of the instrument's tradable hours?
|
|
|
|
|
|
|
|
price_chart = linked_charts.chart
|
2020-10-20 12:43:51 +00:00
|
|
|
price_chart.default_view()
|
2020-09-23 00:13:14 +00:00
|
|
|
|
|
|
|
async for index in await feed.index_stream():
|
|
|
|
|
|
|
|
# update chart historical bars graphics
|
2020-10-20 12:43:51 +00:00
|
|
|
price_chart.update_ohlc_from_array(
|
2020-09-23 00:13:14 +00:00
|
|
|
price_chart.name,
|
|
|
|
ohlcv.array,
|
|
|
|
# When appending a new bar, in the time between the insert
|
|
|
|
# here and the Qt render call the underlying price data may
|
|
|
|
# have already been updated, thus make sure to also update
|
|
|
|
# the last bar if necessary on this render cycle which is
|
|
|
|
# why we **don't** set:
|
|
|
|
# just_history=True
|
|
|
|
)
|
|
|
|
# resize view
|
2020-09-29 20:18:21 +00:00
|
|
|
# price_chart._set_yrange()
|
2020-09-23 00:13:14 +00:00
|
|
|
|
|
|
|
for name, curve in price_chart._overlays.items():
|
|
|
|
# TODO: standard api for signal lookups per plot
|
|
|
|
if name in price_chart._array.dtype.fields:
|
|
|
|
# should have already been incremented above
|
2020-10-20 12:43:51 +00:00
|
|
|
price_chart.update_curve_from_array(
|
2020-09-23 00:13:14 +00:00
|
|
|
name,
|
2020-10-20 12:43:51 +00:00
|
|
|
price_chart._array,
|
2020-09-23 00:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for name, chart in linked_charts.subplots.items():
|
2020-10-20 12:43:51 +00:00
|
|
|
chart.update_curve_from_array(chart.name, chart._shm.array)
|
2020-09-29 20:18:21 +00:00
|
|
|
# chart._set_yrange()
|
2020-09-23 00:13:14 +00:00
|
|
|
|
2020-10-20 12:43:51 +00:00
|
|
|
price_chart.increment_view()
|
|
|
|
|
2020-09-23 00:13:14 +00:00
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
def _main(
|
|
|
|
sym: str,
|
|
|
|
brokername: str,
|
2020-08-19 19:32:09 +00:00
|
|
|
tractor_kwargs,
|
2020-07-17 13:06:20 +00:00
|
|
|
) -> None:
|
2020-08-03 00:10:06 +00:00
|
|
|
"""Sync entry point to start a chart app.
|
2020-06-17 15:45:43 +00:00
|
|
|
"""
|
2020-08-03 00:10:06 +00:00
|
|
|
# Qt entry point
|
|
|
|
run_qtractor(
|
2020-08-19 19:32:09 +00:00
|
|
|
func=_async_main,
|
2020-08-26 18:15:52 +00:00
|
|
|
args=(sym, brokername),
|
|
|
|
main_widget=ChartSpace,
|
|
|
|
tractor_kwargs=tractor_kwargs,
|
2020-08-03 00:10:06 +00:00
|
|
|
)
|