Add order update method to client

basic_orders
Tyler Goodlet 2021-03-07 10:56:09 -05:00
parent 2cabe1831c
commit a43ab1b983
1 changed files with 29 additions and 9 deletions

View File

@ -19,21 +19,32 @@ Orders and execution client API.
""" """
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Dict, Tuple from typing import Dict, Tuple, List
from pprint import pformat from pprint import pformat
from dataclasses import dataclass, field from dataclasses import dataclass, field
import trio import trio
import tractor import tractor
# import msgspec
from ..data._source import Symbol from ..data._source import Symbol
from ..log import get_logger from ..log import get_logger
from ._ems import _ems_main from ._ems import _emsd_main
log = get_logger(__name__) 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 @dataclass
class OrderBook: class OrderBook:
"""Buy-side (client-side ?) order book ctl and tracking. """Buy-side (client-side ?) order book ctl and tracking.
@ -57,7 +68,8 @@ class OrderBook:
def send( def send(
self, self,
uuid: str, uuid: str,
symbol: 'Symbol', symbol: str,
brokers: List[str],
price: float, price: float,
size: float, size: float,
action: str, action: str,
@ -67,8 +79,8 @@ class OrderBook:
'action': action, 'action': action,
'price': price, 'price': price,
'size': size, 'size': size,
'symbol': symbol.key, 'symbol': symbol,
'brokers': symbol.brokers, 'brokers': brokers,
'oid': uuid, 'oid': uuid,
'exec_mode': exec_mode, # dark or live 'exec_mode': exec_mode, # dark or live
} }
@ -76,8 +88,16 @@ class OrderBook:
self._to_ems.send_nowait(cmd) self._to_ems.send_nowait(cmd)
return 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: def cancel(self, uuid: str) -> bool:
"""Cancel an order (or alert) from the EMS. """Cancel an order (or alert) from the EMS.
@ -111,7 +131,7 @@ def get_orders(
if _orders is None: if _orders is None:
# setup local ui event streaming channels for request/resp # setup local ui event streaming channels for request/resp
# streamging with EMS daemon # streamging with EMS daemon
_orders = OrderBook(*trio.open_memory_channel(100)) _orders = OrderBook(*trio.open_memory_channel(1))
return _orders return _orders
@ -218,7 +238,7 @@ async def open_ems(
async with maybe_open_emsd() as portal: async with maybe_open_emsd() as portal:
trades_stream = await portal.run( trades_stream = await portal.run(
_ems_main, _emsd_main,
client_actor_name=actor.name, client_actor_name=actor.name,
broker=broker, broker=broker,
symbol=symbol.key, symbol=symbol.key,