Start input handling **after** order mode is up

asyncify_input_modes
Tyler Goodlet 2021-06-16 08:28:57 -04:00
parent c971997f1a
commit ca23825aff
2 changed files with 115 additions and 93 deletions

View File

@ -158,18 +158,20 @@ class GodWidget(QtGui.QWidget):
# self.toolbar_layout.addWidget(self.strategy_box)
def load_symbol(
self,
providername: str,
symbol_key: str,
loglevel: str,
ohlc: bool = True,
reset: bool = False,
) -> None:
"""Load a new contract into the charting app.
) -> trio.Event:
'''Load a new contract into the charting app.
Expects a ``numpy`` structured array containing all the ohlcv fields.
"""
'''
# our symbol key style is always lower case
symbol_key = symbol_key.lower()
@ -178,6 +180,8 @@ class GodWidget(QtGui.QWidget):
linkedsplits = self.get_chart_symbol(fqsn)
order_mode_started = trio.Event()
if not self.vbox.isEmpty():
# XXX: this is CRITICAL especially with pixel buffer caching
self.linkedsplits.hide()
@ -200,10 +204,15 @@ class GodWidget(QtGui.QWidget):
providername,
symbol_key,
loglevel,
order_mode_started,
)
self.set_chart_symbol(fqsn, linkedsplits)
else:
# symbol is already loaded and ems ready
order_mode_started.set()
self.vbox.addWidget(linkedsplits)
# chart is already in memory so just focus it
@ -223,6 +232,8 @@ class GodWidget(QtGui.QWidget):
f'tick:{symbol.tick_size}'
)
return order_mode_started
class LinkedSplits(QtGui.QWidget):
'''
@ -1527,6 +1538,8 @@ async def display_symbol_data(
sym: str,
loglevel: str,
order_mode_started: trio.Event,
) -> None:
'''Spawn a real-time displayed and updated chart for provider symbol.
@ -1623,7 +1636,6 @@ async def display_symbol_data(
},
})
# load initial fsp chain (otherwise known as "indicators")
n.start_soon(
spawn_fsps,
@ -1644,6 +1656,7 @@ async def display_symbol_data(
wap_in_history,
)
# TODO: instead we should start based on instrument trading hours?
# wait for a first quote before we start any update tasks
# quote = await feed.receive()
# log.info(f'Received first quote {quote}')
@ -1651,24 +1664,11 @@ async def display_symbol_data(
n.start_soon(
check_for_new_bars,
feed,
# delay,
ohlcv,
linkedsplits
)
# interactive testing
# n.start_soon(
# test_bed,
# ohlcv,
# chart,
# linkedsplits,
# )
# start async input handling for chart's view
# await godwidget._task_stack.enter_async_context(
async with chart._vb.open_async_input_handler():
await start_order_mode(chart, symbol, provider)
await start_order_mode(chart, symbol, provider, order_mode_started)
async def load_providers(
@ -1773,7 +1773,7 @@ async def _async_main(
symbol, _, provider = sym.rpartition('.')
# this internally starts a ``display_symbol_data()`` task above
godwidget.load_symbol(provider, symbol, loglevel)
order_mode_ready = godwidget.load_symbol(provider, symbol, loglevel)
# spin up a search engine for the local cached symbol set
async with _search.register_symbol_search(
@ -1791,6 +1791,8 @@ async def _async_main(
# the chart's select cache
root_n.start_soon(load_providers, brokernames, loglevel)
await order_mode_ready.wait()
# start handling search bar kb inputs
async with (

View File

@ -26,8 +26,9 @@ from typing import Optional, Dict, Callable, Any
import uuid
import pyqtgraph as pg
import trio
from pydantic import BaseModel
import trio
# from trio_typing import TaskStatus
from ._graphics._lines import LevelLine, position_line
from ._editors import LineEditor, ArrowEditor, _order_lines
@ -108,6 +109,10 @@ class OrderMode:
"""Set execution mode.
"""
# not initialized yet
if not self.chart._cursor:
return
self._action = action
self.lines.stage_line(
@ -306,10 +311,14 @@ async def open_order_mode(
async def start_order_mode(
chart: 'ChartPlotWidget', # noqa
symbol: Symbol,
brokername: str,
# task_status: TaskStatus[trio.Event] = trio.TASK_STATUS_IGNORED,
started: trio.Event,
) -> None:
'''Activate chart-trader order mode loop:
- connect to emsd
@ -322,7 +331,11 @@ async def start_order_mode(
# spawn EMS actor-service
async with (
open_ems(brokername, symbol) as (book, trades_stream, positions),
open_order_mode(symbol, chart, book) as order_mode
open_order_mode(symbol, chart, book) as order_mode,
# # start async input handling for chart's view
# # await godwidget._task_stack.enter_async_context(
# chart._vb.open_async_input_handler(),
):
# update any exising positions
@ -345,6 +358,13 @@ async def start_order_mode(
# Begin order-response streaming
done()
# start async input handling for chart's view
async with chart._vb.open_async_input_handler():
# signal to top level symbol loading task we're ready
# to handle input since the ems connection is ready
started.set()
# this is where we receive **back** messages
# about executions **from** the EMS actor
async for msg in trades_stream: