Skip to content

Strategies

Ready-made market makers and an execution algorithm. Market makers derive from TwoSidedQuoter. Importable from microbook.sim.

TwoSidedQuoter

TwoSidedQuoter(name: str, *, tick_interval: int, latency: int = 0, portfolio: Portfolio | None = None)

Bases: Strategy

Base class for market makers that keep at most one bid and one ask.

Subclasses implement compute_quotes(). Quote management rules:

  • prices are rounded passively onto the book's grid (bids down, asks up);
  • quotes never cross the opposite touch: they are slid to one tick behind it and sent post-only, so a maker never pays to take liquidity;
  • an existing quote whose price is unchanged is left alone, preserving its queue priority; otherwise it is cancelled and replaced.

compute_quotes

compute_quotes(now: int, lob: LimitOrderBook) -> tuple[float | None, float | None, int, int] | None

Return (bid_px, ask_px, bid_qty, ask_qty); a None price pulls that side. Return None to leave all quotes untouched this tick.

on_tick

on_tick(now: int, lob: LimitOrderBook) -> list[Action]

Requote at most once every tick_interval.

Quotes come from compute_quotes().

on_report

on_report(now: int, report: ExecutionReport) -> list[Action]

Forget a quote the book rejected, so the next requote replaces it.

requote

requote(now: int, lob: LimitOrderBook, bid_px: float | None, ask_px: float | None, bid_qty: int, ask_qty: int) -> list[Action]

Actions that move the quotes to bid_px / ask_px (None pulls that side).

Prices are rounded passively and kept off the opposite touch; an unchanged quote keeps its queue position.

cancel_all

cancel_all(now: int) -> list[Action]

Cancel both quotes.

MarketMaker

MarketMaker(name: str = 'mm', *, tick_size: float | None = None, half_spread_ticks: int = 1, size: int = 5, tick_interval: int = 10, inventory_skew_ticks: int = 2, latency: int = 0)

Bases: TwoSidedQuoter

Baseline symmetric market maker.

Quotes mid +/- half_spread_ticks and, when carrying inventory, shifts both quotes by inventory_skew_ticks against the position (long -> lower, to attract buyers; short -> higher). tick_size is the quoting unit and defaults to the book's tick.

compute_quotes

compute_quotes(now: int, lob: LimitOrderBook) -> tuple[float | None, float | None, int, int] | None

Mid plus or minus the half-spread, shifted against any inventory.

AdaptiveMarketMaker

AdaptiveMarketMaker(cfg: AdaptiveConfig | None = None, name: str = 'amm')

Bases: TwoSidedQuoter

Heuristic market maker with three controls:

  • volatility-aware spread — the half-spread widens with the mean absolute mid change over the last vol_window refreshes;
  • inventory skew — both quotes shift against the position (long -> lower, to sell; short -> higher, to buy), plus a hard limit;
  • imbalance lean — both quotes shift toward the heavier side of the top-3 book (bid-heavy -> higher), anticipating the next price move.

Skews shift the quote centre; they never change the spread width, so the bid stays strictly below the ask.

compute_quotes

compute_quotes(now: int, lob: LimitOrderBook) -> Quotes | None

Volatility-widened quotes around the mid, shifted against inventory and toward the heavier book side.

AdaptiveConfig dataclass

AdaptiveConfig(tick_size: float | None = None, base_half_spread_ticks: float = 1.0, size: int = 5, tick_interval: int = 10, latency: int = 0, inv_target: int = 0, inv_limit: int = 25, inv_k: float = 0.08, max_skew_ticks: float = 6.0, vol_window: int = 30, vol_k: float = 3.0, imb_k: float = 2.0)

Parameters of AdaptiveMarketMaker.

tick_size class-attribute instance-attribute

tick_size: float | None = None

Quoting unit, in price; None uses the book's tick. The other *_ticks and *_k settings are in this unit.

base_half_spread_ticks class-attribute instance-attribute

base_half_spread_ticks: float = 1.0

Half-spread before volatility widening.

size class-attribute instance-attribute

size: int = 5

Shares quoted on each side.

tick_interval class-attribute instance-attribute

tick_interval: int = 10

Minimum time between requotes.

latency class-attribute instance-attribute

latency: int = 0

Order-entry latency, in time units.

inv_target class-attribute instance-attribute

inv_target: int = 0

Inventory the quotes skew toward.

inv_limit class-attribute instance-attribute

inv_limit: int = 25

Stop quoting the side that would take |inventory| beyond this many shares.

inv_k class-attribute instance-attribute

inv_k: float = 0.08

Quote shift per share of inventory away from inv_target.

max_skew_ticks class-attribute instance-attribute

max_skew_ticks: float = 6.0

Cap on the combined inventory and imbalance shift.

vol_window class-attribute instance-attribute

vol_window: int = 30

Refreshes in the volatility estimate (mean absolute mid change per refresh).

vol_k class-attribute instance-attribute

vol_k: float = 3.0

Half-spread widening per unit of the volatility estimate.

imb_k class-attribute instance-attribute

imb_k: float = 2.0

Shift toward the heavier side of the top-3 book at full imbalance.

AvellanedaStoikovMarketMaker

AvellanedaStoikovMarketMaker(cfg: AvellanedaStoikovConfig | None = None, name: str = 'as')

Bases: TwoSidedQuoter

Avellaneda & Stoikov (2008), High-frequency trading in a limit order book.

With inventory q, mid s, volatility sigma and time to horizon tau = T - t, the optimal quotes are centred on the reservation price::

r     = s - q * gamma * sigma^2 * tau
delta = gamma * sigma^2 * tau + (2 / gamma) * ln(1 + gamma / k)    # total spread

so inventory risk is managed by moving the centre (not by widening). In the finite-horizon form the skew fades as t -> T; with rolling_tau set, tau is held constant instead.

sigma

sigma() -> float | None

Configured sigma, or realized volatility per sqrt(time unit) of sampled mids.

tau

tau(now: int) -> int

Time left to the horizon, T - t, or the constant rolling_tau if set.

reservation_price

reservation_price(mid: float, inventory: int, now: int, sigma: float) -> float

Reservation price r = mid - q * gamma * sigma^2 * tau.

optimal_spread

optimal_spread(now: int, sigma: float) -> float

Optimal total spread gamma * sigma^2 * tau + (2 / gamma) * ln(1 + gamma / k).

compute_quotes

compute_quotes(now: int, lob: LimitOrderBook) -> Quotes | None

Quotes around the reservation price, at least min_half_spread_ticks from it on each side.

AvellanedaStoikovConfig dataclass

AvellanedaStoikovConfig(gamma: float = 0.1, k: float = 100.0, horizon: int = 10000, rolling_tau: int | None = None, sigma: float | None = None, vol_window: int = 50, size: int = 5, tick_interval: int = 10, inv_limit: int = 50, min_half_spread_ticks: float = 0.5, latency: int = 0)

Parameters of AvellanedaStoikovMarketMaker.

gamma class-attribute instance-attribute

gamma: float = 0.1

Risk aversion γ.

k class-attribute instance-attribute

k: float = 100.0

Fill-intensity decay: λ(δ) = A·exp(−kδ), with δ in price units.

horizon class-attribute instance-attribute

horizon: int = 10000

Terminal time T of the finite-horizon form.

rolling_tau class-attribute instance-attribute

rolling_tau: int | None = None

If set, a constant time to horizon τ used instead of T − t: the stationary "rolling horizon" variant, whose skew does not fade.

sigma class-attribute instance-attribute

sigma: float | None = None

Mid volatility per √(time unit); None estimates it online from sampled mids.

vol_window class-attribute instance-attribute

vol_window: int = 50

Mid samples in the online volatility estimate.

size class-attribute instance-attribute

size: int = 5

Shares quoted on each side.

tick_interval class-attribute instance-attribute

tick_interval: int = 10

Minimum time between requotes.

inv_limit class-attribute instance-attribute

inv_limit: int = 50

Stop quoting the side that would take |inventory| beyond this many shares.

min_half_spread_ticks class-attribute instance-attribute

min_half_spread_ticks: float = 0.5

Lower bound on the half-spread, in ticks.

latency class-attribute instance-attribute

latency: int = 0

Order-entry latency, in time units.

TWAPExecutor

TWAPExecutor(side: Side, total_qty: int, start: int, end: int, *, tick_interval: int = 10, name: str = 'twap', latency: int = 0)

Bases: Strategy

Time-weighted execution of total_qty between start and end.

Sends a market-order slice every tick_interval (driven by SNAPSHOT ticks). Slices are sized so the remainder is spread evenly over the slices left; the final slice sends everything outstanding. Quantity that a market order could not fill (thin book) is returned to the schedule. It waits for a two-sided market so every slice has a well-defined arrival price.

remaining property

remaining: int

Shares not yet sent, or sent but returned unfilled.

on_tick

on_tick(now: int, lob: LimitOrderBook) -> list[Action]

Send the next market-order slice when one is due.

on_report

on_report(now: int, report: ExecutionReport) -> list[Action]

Return the unfilled part of a market order to the schedule.