Make order-book-vars globals to persist across ems-dialog connections

open_order_loading
Tyler Goodlet 2022-08-14 16:16:48 -04:00
parent 665bb183f7
commit c4af706d51
1 changed files with 25 additions and 15 deletions

View File

@ -33,10 +33,10 @@ from bidict import bidict
import pendulum import pendulum
import trio import trio
import tractor import tractor
from dataclasses import dataclass
from .. import data from .. import data
from ..data._source import Symbol from ..data._source import Symbol
from ..data.types import Struct
from ..pp import ( from ..pp import (
Position, Position,
Transaction, Transaction,
@ -58,8 +58,7 @@ from ._messages import (
log = get_logger(__name__) log = get_logger(__name__)
@dataclass class PaperBoi(Struct):
class PaperBoi:
""" """
Emulates a broker order client providing the same API and Emulates a broker order client providing the same API and
delivering an order-event response stream but with methods for delivering an order-event response stream but with methods for
@ -73,8 +72,8 @@ class PaperBoi:
# map of paper "live" orders which be used # map of paper "live" orders which be used
# to simulate fills based on paper engine settings # to simulate fills based on paper engine settings
_buys: bidict _buys: dict
_sells: bidict _sells: dict
_reqids: bidict _reqids: bidict
_positions: dict[str, Position] _positions: dict[str, Position]
_trade_ledger: dict[str, Any] _trade_ledger: dict[str, Any]
@ -409,7 +408,6 @@ async def handle_order_requests(
# action = request_msg['action'] # action = request_msg['action']
match request_msg: match request_msg:
# if action in {'buy', 'sell'}:
case {'action': ('buy' | 'sell')}: case {'action': ('buy' | 'sell')}:
order = BrokerdOrder(**request_msg) order = BrokerdOrder(**request_msg)
account = order.account account = order.account
@ -427,12 +425,6 @@ async def handle_order_requests(
)) ))
continue continue
# validate
# order = BrokerdOrder(**request_msg)
# if order.reqid is None:
# reqid =
# else:
reqid = order.reqid or str(uuid.uuid4()) reqid = order.reqid or str(uuid.uuid4())
# deliver ack that order has been submitted to broker routing # deliver ack that order has been submitted to broker routing
@ -475,6 +467,23 @@ async def handle_order_requests(
log.error(f'Unknown order command: {request_msg}') log.error(f'Unknown order command: {request_msg}')
_reqids: bidict[str, tuple] = {}
_buys: dict[
str,
dict[
tuple[str, float],
tuple[float, str, str],
]
] = {}
_sells: dict[
str,
dict[
tuple[str, float],
tuple[float, str, str],
]
] = {}
@tractor.context @tractor.context
async def trades_dialogue( async def trades_dialogue(
@ -484,6 +493,7 @@ async def trades_dialogue(
loglevel: str = None, loglevel: str = None,
) -> None: ) -> None:
tractor.log.get_console_log(loglevel) tractor.log.get_console_log(loglevel)
async with ( async with (
@ -505,10 +515,10 @@ async def trades_dialogue(
client = PaperBoi( client = PaperBoi(
broker, broker,
ems_stream, ems_stream,
_buys={}, _buys=_buys,
_sells={}, _sells=_sells,
_reqids={}, _reqids=_reqids,
# TODO: load paper positions from ``positions.toml`` # TODO: load paper positions from ``positions.toml``
_positions={}, _positions={},