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-07-17 13:06:20 +00:00
|
|
|
import time
|
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-08-19 19:32:09 +00:00
|
|
|
from ._graphics import CrossHair, BarItems
|
2020-08-30 16:29:29 +00:00
|
|
|
from ._axes import YSticky
|
|
|
|
from ._style import _xaxis_at, _min_points_to_show, hcolor
|
2020-06-19 12:01:10 +00:00
|
|
|
from ._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
|
|
|
|
from ..log import get_logger
|
2020-08-03 00:10:06 +00:00
|
|
|
from ._exec import run_qtractor
|
2020-09-10 01:19:36 +00:00
|
|
|
from ._source import base_ohlc_dtype
|
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-15 14:48:00 +00:00
|
|
|
# margins
|
2020-08-31 21:18:35 +00:00
|
|
|
CHART_MARGINS = (0, 0, 5, 3)
|
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
|
|
|
|
|
|
|
if _xaxis_at == 'bottom':
|
|
|
|
self.xaxis.setStyle(showValues=False)
|
|
|
|
else:
|
|
|
|
self.xaxis_ind.setStyle(showValues=False)
|
|
|
|
|
|
|
|
self.splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
|
|
|
|
self.splitter.setHandleWidth(5)
|
|
|
|
|
|
|
|
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-07-17 13:06:20 +00:00
|
|
|
prop: float = 0.25 # 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-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-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,
|
|
|
|
)
|
|
|
|
# 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-08-19 19:32:09 +00:00
|
|
|
cpw.setFrameStyle(QtGui.QFrame.StyledPanel | QtGui.QFrame.Plain)
|
2020-07-17 13:06:20 +00:00
|
|
|
cpw.getPlotItem().setContentsMargins(*CHART_MARGINS)
|
|
|
|
|
|
|
|
# link chart x-axis to main quotes chart
|
|
|
|
cpw.setXLink(self.chart)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
# add to cross-hair's known plots
|
|
|
|
self._ch.add_plot(cpw)
|
|
|
|
|
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-09-09 14:47:44 +00:00
|
|
|
yrange: Optional[Tuple[float, float]] = 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-09-09 14:47:44 +00:00
|
|
|
# useOpenGL=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-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._yrange = yrange
|
|
|
|
self._vb = self.plotItem.vb
|
2020-09-11 17:16:11 +00:00
|
|
|
self._static_yrange = None
|
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-06-17 15:45:43 +00:00
|
|
|
self.plotItem.vb.setXRange(0, 0)
|
|
|
|
|
2020-06-15 14:48:00 +00:00
|
|
|
# use cross-hair for cursor
|
|
|
|
self.setCursor(QtCore.Qt.CrossCursor)
|
|
|
|
|
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
|
|
|
vb = self._vb
|
|
|
|
# for mouse wheel which doesn't seem to emit XRangeChanged
|
|
|
|
vb.sigRangeChangedManually.connect(self._set_yrange)
|
|
|
|
# for when the splitter(s) are resized
|
|
|
|
vb.sigResized.connect(self._set_yrange)
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
def _update_contents_label(self, index: int) -> None:
|
|
|
|
if index > 0 and index < len(self._array):
|
|
|
|
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-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-09-11 17:16:11 +00:00
|
|
|
size='4pt',
|
2020-08-26 18:15:52 +00:00
|
|
|
)
|
|
|
|
self.scene().addItem(label)
|
|
|
|
|
|
|
|
def update(index: int) -> None:
|
|
|
|
label.setText(
|
2020-09-09 14:47:44 +00:00
|
|
|
"{name}[{index}] -> O:{} H:{} L:{} C:{} V:{}".format(
|
2020-08-26 18:15:52 +00:00
|
|
|
*self._array[index].item()[2:],
|
|
|
|
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)
|
|
|
|
self._update_contents_label(index=-1)
|
2020-09-11 17:16:11 +00:00
|
|
|
label.show()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
# set xrange limits
|
2020-07-08 19:42:05 +00:00
|
|
|
xlast = data[-1]['index']
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
# show last 50 points on startup
|
|
|
|
self.plotItem.vb.setXRange(xlast - 50, xlast + 50)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-08-30 16:29:29 +00:00
|
|
|
self._add_sticky(name)
|
|
|
|
|
2020-06-17 15:45:43 +00:00
|
|
|
return graphics
|
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(
|
|
|
|
data,
|
|
|
|
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
|
|
|
|
if not self._graphics and name is None:
|
2020-08-26 18:15:52 +00:00
|
|
|
name = 'a_line_bby'
|
2020-07-08 19:42:05 +00:00
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
self._graphics[name] = curve
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# XXX: How to stack labels vertically?
|
|
|
|
label = pg.LabelItem(
|
|
|
|
justify='left',
|
2020-09-11 17:16:11 +00:00
|
|
|
size='4pt',
|
2020-08-26 18:15:52 +00:00
|
|
|
)
|
2020-09-11 17:16:11 +00:00
|
|
|
label.setParentItem(self._vb)
|
|
|
|
if overlay:
|
|
|
|
# position bottom left if an overlay
|
|
|
|
label.anchor(itemPos=(0, 1), parentPos=(0, 1), offset=(0, 25))
|
|
|
|
|
|
|
|
label.show()
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
self.scene().addItem(label)
|
|
|
|
|
|
|
|
def update(index: int) -> None:
|
|
|
|
data = self._array[index]
|
2020-09-09 14:47:44 +00:00
|
|
|
label.setText(f"{name} -> {data}")
|
2020-08-26 18:15:52 +00:00
|
|
|
|
|
|
|
self._labels[name] = (label, update)
|
|
|
|
self._update_contents_label(index=-1)
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
# set a "startup view"
|
2020-07-08 19:42:05 +00:00
|
|
|
xlast = len(data) - 1
|
2020-06-19 12:01:10 +00:00
|
|
|
|
|
|
|
# show last 50 points on startup
|
|
|
|
self.plotItem.vb.setXRange(xlast - 50, xlast + 50)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-07-08 19:42:05 +00:00
|
|
|
# TODO: we should instead implement a diff based
|
|
|
|
# "only update with new items" on the pg.PlotDataItem
|
|
|
|
curve.update_from_array = curve.setData
|
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
self._add_sticky(name)
|
|
|
|
|
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,
|
|
|
|
color=pg.mkPen(hcolor('gray'))
|
|
|
|
)
|
|
|
|
return last
|
|
|
|
|
2020-06-19 12:01:10 +00:00
|
|
|
def update_from_array(
|
|
|
|
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-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-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-07-04 21:48:31 +00:00
|
|
|
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.
|
|
|
|
# if l < lbar or r > rbar:
|
|
|
|
view_len = r - l
|
|
|
|
# TODO: logic to check if end of bars in view
|
|
|
|
extra = view_len - _min_points_to_show
|
|
|
|
begin = 0 - extra
|
2020-08-19 19:32:09 +00:00
|
|
|
end = len(self._array) - 1 + extra
|
2020-07-04 21:48:31 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
# 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}"
|
|
|
|
# )
|
2020-07-04 21:48:31 +00:00
|
|
|
self._set_xlimits(begin, end)
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
# yrange
|
|
|
|
if self._static_yrange is not None:
|
|
|
|
yrange = self._static_yrange
|
|
|
|
|
|
|
|
if yrange is not None:
|
|
|
|
ylow, yhigh = yrange
|
|
|
|
self._static_yrange = yrange
|
|
|
|
else:
|
|
|
|
|
|
|
|
# TODO: this should be some kind of numpy view api
|
|
|
|
bars = self._array[lbar:rbar]
|
|
|
|
if not len(bars):
|
|
|
|
# likely no data loaded yet
|
|
|
|
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'])
|
|
|
|
# std = np.std(bars['close'])
|
|
|
|
except IndexError:
|
|
|
|
# must be non-ohlc array?
|
|
|
|
ylow = np.nanmin(bars)
|
|
|
|
yhigh = np.nanmax(bars)
|
|
|
|
# std = np.std(bars)
|
2020-06-17 15:45:43 +00:00
|
|
|
|
2020-07-17 13:06:20 +00:00
|
|
|
# view margins: stay within 10% of the "true range"
|
2020-07-04 21:48:31 +00:00
|
|
|
diff = yhigh - ylow
|
2020-09-09 14:47:44 +00:00
|
|
|
ylow = ylow - (diff * 0.04)
|
|
|
|
yhigh = yhigh + (diff * 0.01)
|
|
|
|
|
|
|
|
# compute contents label "height" in view terms
|
|
|
|
if self._labels:
|
|
|
|
label = self._labels[self.name][0]
|
|
|
|
rect = label.itemRect()
|
|
|
|
tl, br = rect.topLeft(), rect.bottomRight()
|
|
|
|
vb = self.plotItem.vb
|
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())
|
|
|
|
label_h = top - bottom
|
|
|
|
except np.linalg.LinAlgError:
|
|
|
|
label_h = 0
|
2020-09-09 14:47:44 +00:00
|
|
|
# print(f'label height {self.name}: {label_h}')
|
|
|
|
else:
|
|
|
|
label_h = 0
|
2020-06-15 14:48:00 +00:00
|
|
|
|
2020-06-17 15:45:43 +00:00
|
|
|
chart = self
|
|
|
|
chart.setLimits(
|
|
|
|
yMin=ylow,
|
2020-09-09 14:47:44 +00:00
|
|
|
yMax=yhigh + label_h,
|
|
|
|
# minYRange=std
|
2020-06-17 15:45:43 +00:00
|
|
|
)
|
2020-09-09 14:47:44 +00:00
|
|
|
chart.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 add_new_bars(delay_s, linked_charts):
|
|
|
|
"""Task which inserts new bars into the ohlc 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?
|
|
|
|
|
|
|
|
# adjust delay to compensate for trio processing time
|
|
|
|
ad = delay_s - 0.002
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
price_chart = linked_charts.chart
|
|
|
|
ohlc = price_chart._array
|
2020-08-03 00:10:06 +00:00
|
|
|
|
|
|
|
async def sleep():
|
|
|
|
"""Sleep until next time frames worth has passed from last bar.
|
|
|
|
"""
|
|
|
|
last_ts = ohlc[-1]['time']
|
|
|
|
delay = max((last_ts + ad) - time.time(), 0)
|
|
|
|
await trio.sleep(delay)
|
|
|
|
|
|
|
|
# sleep for duration of current bar
|
|
|
|
await sleep()
|
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
def incr_ohlc_array(array: np.ndarray):
|
|
|
|
(index, t, close) = array[-1][['index', 'time', 'close']]
|
|
|
|
|
|
|
|
# this copies non-std fields (eg. vwap) from the last datum
|
|
|
|
_next = np.array(array[-1], dtype=array.dtype)
|
|
|
|
_next[
|
|
|
|
['index', 'time', 'volume', 'open', 'high', 'low', 'close']
|
|
|
|
] = (index + 1, t + delay_s, 0, close, close, close, close)
|
|
|
|
new_array = np.append(array, _next,)
|
|
|
|
return new_array
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
while True:
|
|
|
|
# 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.
|
|
|
|
# - don't keep appending, but instead increase the
|
|
|
|
# underlying array's size less frequently
|
|
|
|
# - 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-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# add new increment/bar
|
2020-09-09 14:47:44 +00:00
|
|
|
start = time.time()
|
2020-08-19 19:32:09 +00:00
|
|
|
ohlc = price_chart._array = incr_ohlc_array(ohlc)
|
2020-09-09 14:47:44 +00:00
|
|
|
diff = time.time() - start
|
|
|
|
print(f'array append took {diff}')
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# TODO: generalize this increment logic
|
|
|
|
for name, chart in linked_charts.subplots.items():
|
|
|
|
data = chart._array
|
|
|
|
chart._array = np.append(
|
|
|
|
data,
|
|
|
|
np.array(data[-1], dtype=data.dtype)
|
|
|
|
)
|
|
|
|
|
|
|
|
# read value at "open" of bar
|
2020-08-26 18:15:52 +00:00
|
|
|
# last_quote = ohlc[-1]
|
2020-08-19 19:32:09 +00:00
|
|
|
# XXX: If the last bar has not changed print a flat line and
|
|
|
|
# move to the next. This is a "animation" choice that we may not
|
|
|
|
# keep.
|
2020-08-26 18:15:52 +00:00
|
|
|
# if last_quote == ohlc[-1]:
|
|
|
|
# log.debug("Printing flat line for {sym}")
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-09-07 20:41:11 +00:00
|
|
|
# update chart historical bars graphics
|
|
|
|
price_chart.update_from_array(
|
|
|
|
price_chart.name,
|
|
|
|
ohlc,
|
2020-09-11 17:16:11 +00:00
|
|
|
# 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.
|
|
|
|
# just_history=True
|
2020-09-07 20:41:11 +00:00
|
|
|
)
|
|
|
|
# resize view
|
2020-08-26 18:15:52 +00:00
|
|
|
price_chart._set_yrange()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
for name, chart in linked_charts.subplots.items():
|
|
|
|
chart.update_from_array(chart.name, chart._array)
|
|
|
|
chart._set_yrange()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# We **don't** update the bar right now
|
|
|
|
# since the next quote that arrives should in the
|
|
|
|
# tick streaming task
|
|
|
|
await sleep()
|
2020-08-19 19:32:09 +00:00
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# TODO: should we update a graphics again time here?
|
|
|
|
# Think about race conditions with data update task.
|
2020-09-07 20:41:11 +00:00
|
|
|
# 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.
|
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)
|
|
|
|
|
|
|
|
async with brokermod.get_client() as client:
|
|
|
|
# figure out the exact symbol
|
|
|
|
bars = await client.bars(symbol=sym)
|
|
|
|
|
2020-09-10 01:19:36 +00:00
|
|
|
# allow broker to declare historical data fields
|
|
|
|
ohlc_dtype = getattr(brokermod, 'ohlc_dtype', base_ohlc_dtype)
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
# remember, msgpack-numpy's ``from_buffer` returns read-only array
|
|
|
|
bars = np.array(bars[list(ohlc_dtype.names)])
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# load in symbol's ohlc data
|
|
|
|
linked_charts, chart = chart_app.load_symbol(sym, bars)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
# plot historical vwap if available
|
|
|
|
vwap_in_history = False
|
|
|
|
if 'vwap' in bars.dtype.fields:
|
|
|
|
vwap_in_history = True
|
|
|
|
chart.draw_curve(
|
|
|
|
name='vwap',
|
|
|
|
data=bars['vwap'],
|
|
|
|
overlay=True,
|
|
|
|
)
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
# determine ohlc delay between bars
|
|
|
|
times = bars['time']
|
|
|
|
|
|
|
|
# find expected time step between datums
|
|
|
|
delay = times[-1] - times[times != times[-1]][-1]
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
async with trio.open_nursery() as n:
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
# load initial fsp chain (otherwise known as "indicators")
|
|
|
|
n.start_soon(
|
|
|
|
chart_from_fsp,
|
|
|
|
linked_charts,
|
2020-09-09 14:47:44 +00:00
|
|
|
'rsi',
|
2020-08-19 19:32:09 +00:00
|
|
|
sym,
|
|
|
|
bars,
|
|
|
|
brokermod,
|
|
|
|
loglevel,
|
|
|
|
)
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-08-30 16:29:29 +00:00
|
|
|
# update last price sticky
|
2020-09-09 14:47:44 +00:00
|
|
|
last_price_sticky = chart._ysticks[chart.name]
|
|
|
|
last_price_sticky.update_from_data(
|
|
|
|
*chart._array[-1][['index', 'close']]
|
|
|
|
)
|
2020-08-30 16:29:29 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
# graphics update loop
|
2020-08-03 00:10:06 +00:00
|
|
|
|
|
|
|
async with data.open_feed(
|
|
|
|
brokername,
|
|
|
|
[sym],
|
|
|
|
loglevel=loglevel,
|
|
|
|
) as (fquote, stream):
|
|
|
|
|
|
|
|
# wait for a first quote before we start any update tasks
|
|
|
|
quote = await stream.__anext__()
|
2020-08-30 16:29:29 +00:00
|
|
|
log.info(f'RECEIVED FIRST QUOTE {quote}')
|
2020-08-03 00:10:06 +00:00
|
|
|
|
|
|
|
# start graphics tasks after receiving first live quote
|
|
|
|
n.start_soon(add_new_bars, delay, linked_charts)
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
async for quotes in stream:
|
|
|
|
for sym, quote in quotes.items():
|
|
|
|
ticks = quote.get('ticks', ())
|
2020-08-03 00:10:06 +00:00
|
|
|
for tick in ticks:
|
|
|
|
if tick.get('type') == 'trade':
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# TODO: eventually we'll want to update
|
|
|
|
# bid/ask labels and other data as
|
|
|
|
# subscribed by underlying UI consumers.
|
|
|
|
# last = quote.get('last') or quote['close']
|
|
|
|
last = tick['price']
|
|
|
|
|
|
|
|
# update ohlc (I guess we're enforcing this
|
|
|
|
# for now?) overwrite from quote
|
|
|
|
high, low = chart._array[-1][['high', 'low']]
|
|
|
|
chart._array[['high', 'low', 'close']][-1] = (
|
|
|
|
max(high, last),
|
|
|
|
min(low, last),
|
|
|
|
last,
|
|
|
|
)
|
|
|
|
chart.update_from_array(
|
2020-08-26 18:15:52 +00:00
|
|
|
chart.name,
|
2020-08-19 19:32:09 +00:00
|
|
|
chart._array,
|
2020-08-03 00:10:06 +00:00
|
|
|
)
|
2020-08-30 16:29:29 +00:00
|
|
|
# update sticky(s)
|
2020-09-09 14:47:44 +00:00
|
|
|
last_price_sticky.update_from_data(
|
2020-08-30 16:29:29 +00:00
|
|
|
*chart._array[-1][['index', 'close']])
|
2020-08-26 18:15:52 +00:00
|
|
|
chart._set_yrange()
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
vwap = quote.get('vwap')
|
|
|
|
if vwap and vwap_in_history:
|
|
|
|
chart._array['vwap'][-1] = vwap
|
|
|
|
print(f"vwap: {quote['vwap']}")
|
|
|
|
# update vwap overlay line
|
|
|
|
chart.update_from_array(
|
|
|
|
'vwap',
|
|
|
|
chart._array['vwap'],
|
|
|
|
)
|
|
|
|
|
2020-08-03 00:10:06 +00:00
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
async def chart_from_fsp(
|
|
|
|
linked_charts,
|
2020-09-09 14:47:44 +00:00
|
|
|
func_name,
|
2020-08-19 19:32:09 +00:00
|
|
|
sym,
|
|
|
|
bars,
|
|
|
|
brokermod,
|
|
|
|
loglevel,
|
|
|
|
) -> None:
|
|
|
|
"""Start financial signal processing in subactor.
|
|
|
|
|
|
|
|
Pass target entrypoint and historical data.
|
|
|
|
"""
|
|
|
|
async with tractor.open_nursery() as n:
|
|
|
|
portal = await n.run_in_actor(
|
|
|
|
f'fsp.{func_name}', # name as title of sub-chart
|
|
|
|
|
|
|
|
# subactor entrypoint
|
2020-09-08 13:59:29 +00:00
|
|
|
fsp.stream_and_process,
|
2020-08-19 19:32:09 +00:00
|
|
|
bars=bars,
|
|
|
|
brokername=brokermod.name,
|
|
|
|
symbol=sym,
|
|
|
|
fsp_func_name=func_name,
|
|
|
|
|
|
|
|
# tractor config
|
|
|
|
loglevel=loglevel,
|
|
|
|
)
|
|
|
|
|
|
|
|
stream = await portal.result()
|
|
|
|
|
|
|
|
# receive processed historical data-array as first message
|
2020-09-09 14:47:44 +00:00
|
|
|
history = (await stream.__anext__())
|
2020-08-19 19:32:09 +00:00
|
|
|
|
|
|
|
# TODO: enforce type checking here
|
|
|
|
newbars = np.array(history)
|
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
# XXX: hack to get curves aligned with bars graphics: prepend a copy of
|
|
|
|
# the first datum..
|
|
|
|
# TODO: talk to ``pyqtgraph`` core about proper way to solve
|
|
|
|
newbars = np.append(
|
|
|
|
np.array(newbars[0], dtype=newbars.dtype),
|
|
|
|
newbars
|
|
|
|
)
|
|
|
|
newbars = np.append(
|
|
|
|
np.array(newbars[0], dtype=newbars.dtype),
|
|
|
|
newbars
|
|
|
|
)
|
|
|
|
|
2020-08-19 19:32:09 +00:00
|
|
|
chart = linked_charts.add_plot(
|
|
|
|
name=func_name,
|
|
|
|
array=newbars,
|
|
|
|
)
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# check for data length mis-allignment and fill missing values
|
|
|
|
diff = len(chart._array) - len(linked_charts.chart._array)
|
|
|
|
if diff < 0:
|
|
|
|
data = chart._array
|
|
|
|
chart._array = np.append(
|
|
|
|
data,
|
|
|
|
np.full(abs(diff), data[-1], dtype=data.dtype)
|
|
|
|
)
|
|
|
|
|
2020-09-09 14:47:44 +00:00
|
|
|
value = chart._array[-1]
|
|
|
|
last_val_sticky = chart._ysticks[chart.name]
|
|
|
|
last_val_sticky.update_from_data(-1, value)
|
|
|
|
|
2020-09-11 17:16:11 +00:00
|
|
|
chart._set_yrange(yrange=(0, 100))
|
|
|
|
|
2020-08-26 18:15:52 +00:00
|
|
|
# update chart graphics
|
2020-08-19 19:32:09 +00:00
|
|
|
async for value in stream:
|
|
|
|
chart._array[-1] = value
|
2020-09-09 14:47:44 +00:00
|
|
|
last_val_sticky.update_from_data(-1, value)
|
2020-08-26 18:15:52 +00:00
|
|
|
chart.update_from_array(chart.name, chart._array)
|
2020-09-11 17:16:11 +00:00
|
|
|
# chart._set_yrange()
|
2020-08-19 19:32:09 +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
|
|
|
)
|