Add "live order" submission using ctl-<action key>

basic_orders
Tyler Goodlet 2021-01-19 20:48:58 -05:00
parent 7811119736
commit f82127de31
1 changed files with 69 additions and 29 deletions

View File

@ -217,7 +217,11 @@ class LineEditor:
_active_staged_line: LevelLine = None _active_staged_line: LevelLine = None
_stage_line: LevelLine = None _stage_line: LevelLine = None
def stage_line(self, color: str = 'alert_yellow') -> LevelLine: def stage_line(
self,
color: str = 'alert_yellow',
hl_on_hover: bool = False,
) -> LevelLine:
"""Stage a line at the current chart's cursor position """Stage a line at the current chart's cursor position
and return it. and return it.
@ -238,18 +242,24 @@ class LineEditor:
color=color, color=color,
# don't highlight the "staging" line # don't highlight the "staging" line
hl_on_hover=False, hl_on_hover=hl_on_hover,
) )
self._stage_line = line self._stage_line = line
else: else:
# use the existing staged line instead # use the existing staged line instead
# of allocating more mem / objects repeatedly # of allocating more mem / objects repeatedly
line.setValue(y) print(f'hl on hover: {hl_on_hover}')
line.show() line._hoh = hl_on_hover
line.color = color line.color = color
line.label.color = color line.setMouseHover(hl_on_hover)
line.label.show() line.setValue(y)
line.update()
line.show()
label = line.label
label.color = color
label.show()
self._active_staged_line = line self._active_staged_line = line
@ -421,18 +431,26 @@ class OrderMode:
'sell': 'sell_red', 'sell': 'sell_red',
} }
_action: str = 'alert' _action: str = 'alert'
_exec_mode: str = 'dark'
key_map: Dict[str, Callable] = field(default_factory=dict) key_map: Dict[str, Callable] = field(default_factory=dict)
def uuid(self) -> str: def uuid(self) -> str:
return str(uuid.uuid4()) return str(uuid.uuid4())
def set_exec(self, name: str) -> None: def set_exec(
self,
action: str,
# mode: str,
) -> None:
"""Set execution mode. """Set execution mode.
""" """
self._action = name self._action = action
self.lines.stage_line(color=self._colors[name]) self.lines.stage_line(
color=self._colors[action],
hl_on_hover=True if self._exec_mode == 'live' else False,
)
def on_submit(self, uuid: str) -> dict: def on_submit(self, uuid: str) -> dict:
"""On order submitted event, commit the order line """On order submitted event, commit the order line
@ -523,6 +541,7 @@ class OrderMode:
symbol=self.chart._lc._symbol, symbol=self.chart._lc._symbol,
price=y, price=y,
action=self._action, action=self._action,
exec_mode=self._exec_mode,
) )
@ -582,6 +601,7 @@ class ChartView(ViewBox):
# kb ctrls processing # kb ctrls processing
self._key_buffer = [] self._key_buffer = []
self._key_active: bool = False
@property @property
def chart(self) -> 'ChartPlotWidget': # type: ignore # noqa def chart(self) -> 'ChartPlotWidget': # type: ignore # noqa
@ -760,8 +780,9 @@ class ChartView(ViewBox):
elif button == QtCore.Qt.LeftButton: elif button == QtCore.Qt.LeftButton:
# when in order mode, submit execution # when in order mode, submit execution
ev.accept() if self._key_active:
self.mode.submit_exec() ev.accept()
self.mode.submit_exec()
def keyReleaseEvent(self, ev): def keyReleaseEvent(self, ev):
""" """
@ -774,18 +795,25 @@ class ChartView(ViewBox):
return return
ev.accept() ev.accept()
text = ev.text() # text = ev.text()
key = ev.key() key = ev.key()
# mods = ev.modifiers() mods = ev.modifiers()
if key == QtCore.Qt.Key_Shift: if key == QtCore.Qt.Key_Shift:
# if self.state['mouseMode'] == ViewBox.RectMode: # if self.state['mouseMode'] == ViewBox.RectMode:
self.setMouseMode(ViewBox.PanMode) self.setMouseMode(ViewBox.PanMode)
if text in {'a', 'f', 's'}: # if self.state['mouseMode'] == ViewBox.RectMode:
# draw "staged" line under cursor position # if key == QtCore.Qt.Key_Space:
if mods == QtCore.Qt.ControlModifier or key == QtCore.Qt.Key_Control:
self.mode._exec_mode = 'dark'
if key in {QtCore.Qt.Key_A, QtCore.Qt.Key_F, QtCore.Qt.Key_D}:
# remove "staged" level line under cursor position
self.mode.lines.unstage_line() self.mode.lines.unstage_line()
self._key_active = False
def keyPressEvent(self, ev): def keyPressEvent(self, ev):
""" """
This routine should capture key presses in the current view box. This routine should capture key presses in the current view box.
@ -805,41 +833,52 @@ class ChartView(ViewBox):
if self.state['mouseMode'] == ViewBox.PanMode: if self.state['mouseMode'] == ViewBox.PanMode:
self.setMouseMode(ViewBox.RectMode) self.setMouseMode(ViewBox.RectMode)
# ctl # ctrl
ctrl = False
if mods == QtCore.Qt.ControlModifier: if mods == QtCore.Qt.ControlModifier:
# TODO: ctrl-c as cancel? ctrl = True
# https://forum.qt.io/topic/532/how-to-catch-ctrl-c-on-a-widget/9
# if ev.text() == 'c': print(mods)
# self.rbScaleBox.hide() if mods == QtCore.Qt.ControlModifier:
print(f"CTRL + key:{key} + text:{text}") print('space')
self.mode._exec_mode = 'live'
self._key_active = True
# alt # alt
if mods == QtCore.Qt.AltModifier: if mods == QtCore.Qt.AltModifier:
pass pass
# esc # esc
if key == QtCore.Qt.Key_Escape: if key == QtCore.Qt.Key_Escape or (ctrl and key == QtCore.Qt.Key_C):
# ctrl-c as cancel
# https://forum.qt.io/topic/532/how-to-catch-ctrl-c-on-a-widget/9
self.select_box.clear() self.select_box.clear()
# delete any lines under the cursor
mode = self.mode
for line in mode.lines.lines_under_cursor():
mode.book.cancel(uuid=line.oid)
self._key_buffer.append(text) self._key_buffer.append(text)
# View modes # View modes
if text == 'r': if key == QtCore.Qt.Key_R:
self.chart.default_view() self.chart.default_view()
# Order modes # Order modes: stage orders at the current cursor level
# stage orders at the current cursor level
elif text == 's': elif key == QtCore.Qt.Key_D:
self.mode.set_exec('sell') self.mode.set_exec('sell')
elif text == 'f': elif key == QtCore.Qt.Key_F:
self.mode.set_exec('buy') self.mode.set_exec('buy')
elif text == 'a': elif key == QtCore.Qt.Key_A:
self.mode.set_exec('alert') self.mode.set_exec('alert')
# delete orders under cursor # delete orders under cursor
elif text == 'd': elif key == QtCore.Qt.Key_Delete:
# delete any lines under the cursor # delete any lines under the cursor
mode = self.mode mode = self.mode
@ -862,3 +901,4 @@ class ChartView(ViewBox):
# self.scaleHistory(len(self.axHistory)) # self.scaleHistory(len(self.axHistory))
else: else:
ev.ignore() ev.ignore()
self._key_active = False