microstructure-sim 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- microbook/__init__.py +30 -0
- microbook/book.py +553 -0
- microbook/order.py +86 -0
- microbook/py.typed +0 -0
- microbook/replay.py +673 -0
- microbook/rl.py +221 -0
- microbook/sim/__init__.py +93 -0
- microbook/sim/agents.py +495 -0
- microbook/sim/analytics.py +199 -0
- microbook/sim/events.py +41 -0
- microbook/sim/execution_metrics.py +217 -0
- microbook/sim/metrics.py +20 -0
- microbook/sim/orderflow.py +70 -0
- microbook/sim/portfolio.py +83 -0
- microbook/sim/scenarios.py +145 -0
- microbook/sim/simulator.py +274 -0
- microbook/sim/strategies_mm.py +190 -0
- microbook/sim/strategy.py +352 -0
- microbook/stylized.py +324 -0
- microbook/types.py +119 -0
- microstructure_sim-1.0.0.dist-info/METADATA +615 -0
- microstructure_sim-1.0.0.dist-info/RECORD +24 -0
- microstructure_sim-1.0.0.dist-info/WHEEL +4 -0
- microstructure_sim-1.0.0.dist-info/licenses/LICENSE +21 -0
microbook/types.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Core value types shared by the matching engine and the simulator."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from typing import NamedTuple
|
|
7
|
+
|
|
8
|
+
__all__ = ["ExecutionReport", "Fill", "OrderRejected", "OrderStatus", "OrderType", "STPMode", "Side", "TimeInForce"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Side(str, Enum):
|
|
12
|
+
"""Side of an order or trade."""
|
|
13
|
+
|
|
14
|
+
BUY = "BUY"
|
|
15
|
+
SELL = "SELL"
|
|
16
|
+
|
|
17
|
+
@property
|
|
18
|
+
def sign(self) -> int:
|
|
19
|
+
"""+1 for BUY, -1 for SELL. Handy for signed quantities and PnL."""
|
|
20
|
+
return 1 if self is Side.BUY else -1
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def opposite(self) -> Side:
|
|
24
|
+
"""The other side."""
|
|
25
|
+
return Side.SELL if self is Side.BUY else Side.BUY
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class OrderType(str, Enum):
|
|
29
|
+
"""Limit or market order."""
|
|
30
|
+
|
|
31
|
+
LIMIT = "LIMIT"
|
|
32
|
+
MARKET = "MARKET"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TimeInForce(str, Enum):
|
|
36
|
+
"""How long an order's unfilled remainder stays on the book."""
|
|
37
|
+
|
|
38
|
+
GTC = "GTC" # good-till-cancel: remainder rests on the book
|
|
39
|
+
IOC = "IOC" # immediate-or-cancel: fill what you can, cancel the rest
|
|
40
|
+
FOK = "FOK" # fill-or-kill: fill the whole quantity immediately or do nothing
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class OrderStatus(str, Enum):
|
|
44
|
+
"""Outcome of one order message, as reported in an [`ExecutionReport`][microbook.ExecutionReport]."""
|
|
45
|
+
|
|
46
|
+
NEW = "NEW" # resting, no fills on this message
|
|
47
|
+
PARTIALLY_FILLED = "PARTIALLY_FILLED" # some fills, remainder resting
|
|
48
|
+
FILLED = "FILLED" # fully executed
|
|
49
|
+
CANCELED = "CANCELED" # unfilled remainder removed (IOC/FOK/market/STP/cancel)
|
|
50
|
+
REJECTED = "REJECTED" # message refused; book unchanged
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class STPMode(str, Enum):
|
|
54
|
+
"""Self-trade prevention: what happens when an order would trade with a
|
|
55
|
+
resting order from the same ``owner``."""
|
|
56
|
+
|
|
57
|
+
NONE = "NONE" # allow self-trades
|
|
58
|
+
CANCEL_RESTING = "CANCEL_RESTING" # cancel the resting order, keep matching
|
|
59
|
+
CANCEL_INCOMING = "CANCEL_INCOMING" # stop matching, cancel the incoming remainder
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Fill(NamedTuple):
|
|
63
|
+
"""A single execution between an aggressing (taker) and a resting (maker) order.
|
|
64
|
+
|
|
65
|
+
Immutable record (a ``NamedTuple`` rather than a frozen dataclass: it is
|
|
66
|
+
created on the matching hot path, where frozen-dataclass construction costs
|
|
67
|
+
~2x more).
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
taker_order_id: str
|
|
71
|
+
"""Id of the incoming (aggressing) order."""
|
|
72
|
+
maker_order_id: str
|
|
73
|
+
"""Id of the resting order it traded against."""
|
|
74
|
+
price: float
|
|
75
|
+
"""Execution price: the resting order's price."""
|
|
76
|
+
qty: int
|
|
77
|
+
"""Shares traded."""
|
|
78
|
+
aggressor_side: Side | None = None
|
|
79
|
+
"""Side of the taker; `None` for fills built outside the book."""
|
|
80
|
+
ts: int = 0
|
|
81
|
+
"""Timestamp of the taker order (the simulator stamps orders with their arrival time)."""
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class ExecutionReport(NamedTuple):
|
|
85
|
+
"""Outcome of a single message (submit or modify) sent to the book."""
|
|
86
|
+
|
|
87
|
+
order_id: str
|
|
88
|
+
"""Id of the order the message was about."""
|
|
89
|
+
status: OrderStatus
|
|
90
|
+
"""Status of the order after the message."""
|
|
91
|
+
fills: tuple[Fill, ...] = ()
|
|
92
|
+
"""Fills the message produced, in execution order."""
|
|
93
|
+
resting_qty: int = 0
|
|
94
|
+
"""Quantity left on the book after the message."""
|
|
95
|
+
canceled_qty: int = 0
|
|
96
|
+
"""Quantity removed without trading (IOC or FOK remainder, unfilled market order, self-trade prevention)."""
|
|
97
|
+
stp_canceled: tuple[str, ...] = ()
|
|
98
|
+
"""Ids of resting orders removed by self-trade prevention."""
|
|
99
|
+
reason: str | None = None
|
|
100
|
+
"""Why the message was rejected; `None` unless `status` is `REJECTED`."""
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def accepted(self) -> bool:
|
|
104
|
+
"""Whether the book accepted the message (status is not ``REJECTED``)."""
|
|
105
|
+
return self.status is not OrderStatus.REJECTED
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def filled_qty(self) -> int:
|
|
109
|
+
"""Shares filled by this message."""
|
|
110
|
+
return sum(f.qty for f in self.fills)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class OrderRejected(ValueError):
|
|
114
|
+
"""Raised by the convenience entry points (``place_limit``/``place_market``)
|
|
115
|
+
when the book refuses an order. ``submit`` returns a REJECTED report instead."""
|
|
116
|
+
|
|
117
|
+
def __init__(self, report: ExecutionReport) -> None:
|
|
118
|
+
super().__init__(f"order {report.order_id!r} rejected: {report.reason}")
|
|
119
|
+
self.report = report
|