From a43ab1b983c43cbab4b6bf690c80a8e6da29d22f Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 7 Mar 2021 10:56:09 -0500 Subject: [PATCH] Add order update method to client --- piker/exchange/_client.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/piker/exchange/_client.py b/piker/exchange/_client.py index bebbf039..0fa23e60 100644 --- a/piker/exchange/_client.py +++ b/piker/exchange/_client.py @@ -19,21 +19,32 @@ Orders and execution client API. """ from contextlib import asynccontextmanager -from typing import Dict, Tuple +from typing import Dict, Tuple, List from pprint import pformat from dataclasses import dataclass, field import trio import tractor +# import msgspec from ..data._source import Symbol from ..log import get_logger -from ._ems import _ems_main +from ._ems import _emsd_main log = get_logger(__name__) +# class Order(msgspec.Struct): +# action: str +# price: float +# size: float +# symbol: str +# brokers: List[str] +# oid: str +# exec_mode: str + + @dataclass class OrderBook: """Buy-side (client-side ?) order book ctl and tracking. @@ -57,7 +68,8 @@ class OrderBook: def send( self, uuid: str, - symbol: 'Symbol', + symbol: str, + brokers: List[str], price: float, size: float, action: str, @@ -67,8 +79,8 @@ class OrderBook: 'action': action, 'price': price, 'size': size, - 'symbol': symbol.key, - 'brokers': symbol.brokers, + 'symbol': symbol, + 'brokers': brokers, 'oid': uuid, 'exec_mode': exec_mode, # dark or live } @@ -76,8 +88,16 @@ class OrderBook: self._to_ems.send_nowait(cmd) return cmd - async def modify(self, oid: str, price) -> bool: - ... + def update( + self, + uuid: str, + **data: dict, + ) -> dict: + cmd = self._sent_orders[uuid] + cmd.update(data) + self._sent_orders[uuid] = cmd + self._to_ems.send_nowait(cmd) + return cmd def cancel(self, uuid: str) -> bool: """Cancel an order (or alert) from the EMS. @@ -111,7 +131,7 @@ def get_orders( if _orders is None: # setup local ui event streaming channels for request/resp # streamging with EMS daemon - _orders = OrderBook(*trio.open_memory_channel(100)) + _orders = OrderBook(*trio.open_memory_channel(1)) return _orders @@ -218,7 +238,7 @@ async def open_ems( async with maybe_open_emsd() as portal: trades_stream = await portal.run( - _ems_main, + _emsd_main, client_actor_name=actor.name, broker=broker, symbol=symbol.key,