Skip to content

Reinforcement learning

Market making as a reinforcement-learning problem (a Gymnasium environment).

Install the optional dependency with pip install "microstructure-sim[rl]".

MarketMakingEnv puts one learning agent into the closed-loop market of build_market(). Every decision_interval time steps the agent chooses where to quote; the market then runs until the next decision.

Action MultiDiscrete([max_offset + 1, max_offset + 1]): for the bid and the ask, how many ticks behind the most aggressive passive price to quote (0 = as close to the mid as the grid allows without crossing). Quotes are post-only, unchanged quotes keep their queue position, and the side that would push |inventory| past inv_limit is not quoted.

Observation (float32, shape (6,)) ====== ================================================================== 0 inventory / inv_limit 1 spread, in ticks 2 depth imbalance of the top 3 levels, in [-1, 1] 3 mid change over the last interval, in ticks 4 mean absolute mid change per interval over the last 20, in ticks 5 fraction of the episode remaining ====== ==================================================================

Reward The change in mark-to-mid PnL over the interval minus inventory_penalty * inventory**2, divided by the tick size (so a reward of 1 is one tick on one share). The episode ends after horizon steps.

Each reset draws a fresh market seed from the environment's RNG, so reset(seed=s) makes a whole episode reproducible.

MarketMakingEnv

MarketMakingEnv(market: MarketConfig | None = None, *, horizon: int = 5000, decision_interval: int = 10, warmup: int = 200, max_offset: int = 5, size: int = 5, inv_limit: int = 50, inventory_penalty: float = 0.0, latency: int = 0, render_mode: str | None = None)

Bases: Env[Obs, Act]

Gymnasium environment: quote in the closed-loop market (see module docstring).

metadata class-attribute instance-attribute

metadata: dict[str, Any] = {'render_modes': ['ansi']}

Gymnasium metadata: the supported render modes.

reset

reset(*, seed: int | None = None, options: dict[str, Any] | None = None) -> tuple[Obs, dict[str, Any]]

Start an episode in a fresh market; returns the first observation and an info dict.

step

step(action: Act) -> tuple[Obs, float, bool, bool, dict[str, Any]]

Quote at the chosen offsets for one decision interval.

Returns (observation, reward, terminated, truncated, info).

render

render() -> str | None

A one-line text summary when render_mode is "ansi", otherwise None.