Switch msgspec struct ordering to always have required fields first and optionals last

dark_clearing_improvements
Guillermo Rodriguez 2023-01-09 18:43:50 -03:00
parent f218b804b4
commit 0474d66531
No known key found for this signature in database
GPG Key ID: EC3AB66D5D83B392
4 changed files with 30 additions and 31 deletions

View File

@ -43,6 +43,14 @@ SizeUnit = Enum(
class Allocator(Struct): class Allocator(Struct):
symbol: Symbol symbol: Symbol
# TODO: if we ever want ot support non-uniform entry-slot-proportion
# "sizes"
# disti_weight: str = 'uniform'
units_limit: float
currency_limit: float
slots: int
account: Optional[str] = 'paper' account: Optional[str] = 'paper'
_size_units: bidict[str, Optional[str]] = _size_units _size_units: bidict[str, Optional[str]] = _size_units
@ -66,14 +74,6 @@ class Allocator(Struct):
self._size_unit = v self._size_unit = v
return v return v
# TODO: if we ever want ot support non-uniform entry-slot-proportion
# "sizes"
# disti_weight: str = 'uniform'
units_limit: float
currency_limit: float
slots: int
def step_sizes( def step_sizes(
self, self,
) -> (float, float): ) -> (float, float):

View File

@ -107,9 +107,9 @@ class Cancel(Struct):
broker-submitted (live) trigger/order. broker-submitted (live) trigger/order.
''' '''
action: str = 'cancel'
oid: str # uuid4 oid: str # uuid4
symbol: str symbol: str
action: str = 'cancel'
# -------------- # --------------
@ -120,7 +120,6 @@ class Cancel(Struct):
class Status(Struct): class Status(Struct):
name: str = 'status'
time_ns: int time_ns: int
oid: str # uuid4 ems-order dialog id oid: str # uuid4 ems-order dialog id
@ -135,6 +134,8 @@ class Status(Struct):
'error', 'error',
] ]
name: str = 'status'
# this maps normally to the ``BrokerdOrder.reqid`` below, an id # this maps normally to the ``BrokerdOrder.reqid`` below, an id
# normally allocated internally by the backend broker routing system # normally allocated internally by the backend broker routing system
reqid: Optional[int | str] = None reqid: Optional[int | str] = None
@ -168,7 +169,6 @@ class Status(Struct):
class BrokerdCancel(Struct): class BrokerdCancel(Struct):
action: str = 'cancel'
oid: str # piker emsd order id oid: str # piker emsd order id
time_ns: int time_ns: int
@ -180,6 +180,7 @@ class BrokerdCancel(Struct):
# on the emsd order request stream as the ``BrokerdOrderAck.reqid`` # on the emsd order request stream as the ``BrokerdOrderAck.reqid``
# field # field
reqid: Optional[int | str] = None reqid: Optional[int | str] = None
action: str = 'cancel'
class BrokerdOrder(Struct): class BrokerdOrder(Struct):
@ -188,6 +189,10 @@ class BrokerdOrder(Struct):
account: str account: str
time_ns: int time_ns: int
symbol: str # fqsn
price: float
size: float
# TODO: if we instead rely on a +ve/-ve size to determine # TODO: if we instead rely on a +ve/-ve size to determine
# the action we more or less don't need this field right? # the action we more or less don't need this field right?
action: str = '' # {buy, sell} action: str = '' # {buy, sell}
@ -200,10 +205,6 @@ class BrokerdOrder(Struct):
# field # field
reqid: Optional[int | str] = None reqid: Optional[int | str] = None
symbol: str # fqsn
price: float
size: float
# --------------- # ---------------
# emsd <- brokerd # emsd <- brokerd
@ -218,7 +219,6 @@ class BrokerdOrderAck(Struct):
``.oid`` (which is a uuid4). ``.oid`` (which is a uuid4).
''' '''
name: str = 'ack'
# defined and provided by backend # defined and provided by backend
reqid: int | str reqid: int | str
@ -226,11 +226,11 @@ class BrokerdOrderAck(Struct):
# emsd id originally sent in matching request msg # emsd id originally sent in matching request msg
oid: str oid: str
account: str = '' account: str = ''
name: str = 'ack'
class BrokerdStatus(Struct): class BrokerdStatus(Struct):
name: str = 'status'
reqid: int | str reqid: int | str
time_ns: int time_ns: int
status: Literal[ status: Literal[
@ -242,6 +242,7 @@ class BrokerdStatus(Struct):
] ]
account: str account: str
name: str = 'status'
filled: float = 0.0 filled: float = 0.0
reason: str = '' reason: str = ''
remaining: float = 0.0 remaining: float = 0.0
@ -260,7 +261,11 @@ class BrokerdFill(Struct):
if avaiable. if avaiable.
''' '''
name: str = 'fill' # brokerd timestamp required for order mode arrow placement on x-axis
# TODO: maybe int if we force ns?
# we need to normalize this somehow since backends will use their
# own format and likely across many disparate epoch clocks...
broker_time: float
reqid: int | str reqid: int | str
time_ns: int time_ns: int
@ -268,16 +273,10 @@ class BrokerdFill(Struct):
size: float size: float
price: float price: float
name: str = 'fill'
action: Optional[str] = None action: Optional[str] = None
broker_details: dict = {} # meta-data (eg. commisions etc.) broker_details: dict = {} # meta-data (eg. commisions etc.)
# brokerd timestamp required for order mode arrow placement on x-axis
# TODO: maybe int if we force ns?
# we need to normalize this somehow since backends will use their
# own format and likely across many disparate epoch clocks...
broker_time: float
class BrokerdError(Struct): class BrokerdError(Struct):
''' '''
@ -286,15 +285,15 @@ class BrokerdError(Struct):
This is still a TODO thing since we're not sure how to employ it yet. This is still a TODO thing since we're not sure how to employ it yet.
''' '''
name: str = 'error'
oid: str oid: str
symbol: str
reason: str
# if no brokerd order request was actually submitted (eg. we errored # if no brokerd order request was actually submitted (eg. we errored
# at the ``pikerd`` layer) then there will be ``reqid`` allocated. # at the ``pikerd`` layer) then there will be ``reqid`` allocated.
reqid: Optional[int | str] = None reqid: Optional[int | str] = None
symbol: str name: str = 'error'
reason: str
broker_details: dict = {} broker_details: dict = {}
@ -302,7 +301,6 @@ class BrokerdPosition(Struct):
'''Position update event from brokerd. '''Position update event from brokerd.
''' '''
name: str = 'position'
broker: str broker: str
account: str account: str
@ -310,3 +308,4 @@ class BrokerdPosition(Struct):
size: float size: float
avg_price: float avg_price: float
currency: str = '' currency: str = ''
name: str = 'position'

View File

@ -165,8 +165,8 @@ over a NoBsWs.
class JSONRPCResult(Struct): class JSONRPCResult(Struct):
jsonrpc: str = '2.0'
id: int id: int
jsonrpc: str = '2.0'
result: Optional[dict] = None result: Optional[dict] = None
error: Optional[dict] = None error: Optional[dict] = None

View File

@ -338,6 +338,7 @@ class Flow(msgspec.Struct): # , frozen=True):
name: str name: str
plot: pg.PlotItem plot: pg.PlotItem
graphics: Union[Curve, BarItems] graphics: Union[Curve, BarItems]
_shm: ShmArray
yrange: tuple[float, float] = None yrange: tuple[float, float] = None
# in some cases a flow may want to change its # in some cases a flow may want to change its
@ -345,7 +346,6 @@ class Flow(msgspec.Struct): # , frozen=True):
# normally this is just a plain line. # normally this is just a plain line.
ds_graphics: Optional[Curve] = None ds_graphics: Optional[Curve] = None
_shm: ShmArray
is_ohlc: bool = False is_ohlc: bool = False
render: bool = True # toggle for display loop render: bool = True # toggle for display loop