Simulator¶
The event loop, the strategy interface and the actions strategies return. Importable from microbook.sim.
MarketSimulator
¶
MarketSimulator(lob: LimitOrderBook | None = None, strategies: list[Strategy] | None = None, *, record_snapshots: bool = True)
Discrete-event simulator hosting a book and any number of strategies.
Parameters:
-
lob(LimitOrderBook | None, default:None) –The order book. Defaults to a 0.01-tick book with self-trade prevention (
CANCEL_RESTING) so no participant can wash-trade. -
strategies(list[Strategy] | None, default:None) –Participants. Names must be unique (they key metrics and series).
-
record_snapshots(bool, default:True) –Keep an L2 snapshot dict for every SNAPSHOT event (disable for long runs).
SimResult
dataclass
¶
SimResult(fills: list[Fill] = list(), snapshots: list[dict[str, Any]] = list(), pnl_t: list[int] = list(), pnl_series: dict[str, list[float]] = dict(), inventory_series: dict[str, list[int]] = dict(), mid_path: list[tuple[int, float]] = list())
Everything recorded during a run. Accumulates across successive run calls.
fills
class-attribute
instance-attribute
¶
Every fill in the market, in execution order.
snapshots
class-attribute
instance-attribute
¶
L2 snapshots, one per SNAPSHOT event, when record_snapshots is on.
pnl_t
class-attribute
instance-attribute
¶
Times of the PnL and inventory samples (one per SNAPSHOT).
pnl_series
class-attribute
instance-attribute
¶
Total PnL of each strategy, by name, marked to mid at pnl_t; NaN while the book is one-sided.
inventory_series
class-attribute
instance-attribute
¶
Position of each strategy, by name, at pnl_t.
mid_path
class-attribute
instance-attribute
¶
(time, mid), appended whenever the midprice changes; the mid is NaN while the book is one-sided.
Strategy
¶
Base strategy. Override any of the hooks; each returns a list of actions.
=================== ==========================================================
on_start once, when the simulation starts
on_tick on every SNAPSHOT event
on_timer on a TIMER this strategy scheduled (Action.timer)
on_fill for each fill of one of this strategy's orders
on_report for each execution report of a message this strategy sent
=================== ==========================================================
latency delays every order message (submit/cancel/modify) by that many
time units between the decision and its arrival at the book.
Timers must be in the future unless a subclass sets same_time_timers:
then a timer at the current time is allowed and fires after everything
already queued for that time (the strategy must make progress each time,
or the run never ends).
same_time_timers
class-attribute
instance-attribute
¶
same_time_timers: bool = False
Allow timers at the current time; they fire after everything already queued for that time.
on_start
¶
on_start(now: int, lob: LimitOrderBook) -> list[Action]
Called once when the simulation starts.
on_timer
¶
on_timer(now: int, lob: LimitOrderBook) -> list[Action]
Called on a timer this strategy scheduled with Action.timer().
on_fill
¶
Called for each fill of one of this strategy's orders.
Runs before on_report() for the same message.
side is this strategy's side of the trade and is_maker whether its order was resting.
on_report
¶
on_report(now: int, report: ExecutionReport) -> list[Action]
Called with the execution report of each message this strategy sent.
Action
dataclass
¶
A request from a strategy: an order message or a timer.
time
instance-attribute
¶
time: int
Decision time. An order message reaches the book latency later; a timer fires at this time.
kwargs
class-attribute
instance-attribute
¶
Event fields of the message (order, order_id or modify), filled in by the constructors.
submit
classmethod
¶
Send order to the book at time (plus the strategy's latency).
modify
classmethod
¶
modify(order_id: str, time: int, *, new_price: float | None = None, new_qty: int | None = None) -> Action
Change a resting order's price and/or quantity.
reduce
classmethod
¶
Partial cancel of qty shares, resolved when the message reaches the book
(see LimitOrderBook.reduce()); keeps queue priority.
Event
dataclass
¶
Event(time: int, seq: int, etype: EventType, order: Order | None = None, order_id: str | None = None, modify: dict[str, Any] | None = None, owner: int | None = None)
A scheduled simulator event. Ordered by (time, seq); seq is the
global scheduling sequence, which makes ties deterministic (FIFO).
seq
instance-attribute
¶
seq: int
Global scheduling sequence number. It breaks ties at equal time in FIFO order.
order
class-attribute
instance-attribute
¶
The order, for SUBMIT.
order_id
class-attribute
instance-attribute
¶
Target order id, for CANCEL and MODIFY.
modify
class-attribute
instance-attribute
¶
new_price and/or new_qty, for MODIFY.