Skip to content
JunnyyyPublic

About

A live synthetic exchange

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Mercer stepped price mark

Mercer

An interactive market microstructure laboratory.

Mercer is a browser-based synthetic exchange. A seeded mathematical model creates limit orders, market orders, and cancellations; a price-time matching engine turns that flow into trades and prices; the interface exposes the process as it happens.

It is not a historical market feed, an investing product, or a trading venue. Mercer places an inventory-aware market maker inspired by Avellaneda and Stoikov inside a broader synthetic limit-order book so its behavior can be inspected.

How it works

seed + market rules
        |
        v
stochastic order flow
        |
        v
price-time matching
        |
        v
book state + trades + microprice
        |
        v
Liveline chart + exact FIFO ladder

Prices remain integer ticks inside the engine. Orders execute at the best price first and follow FIFO order within each price level. A native Web Worker owns the simulation clock and publishes immutable snapshots to the interface.

The math

Queue depth becomes price

Let $b$ and $a$ be the best bid and ask, with quantities $Q_b$ and $Q_a$. The midpoint is:

$$ m = \frac{a+b}{2} $$

Top-of-book imbalance measures which queue is heavier:

$$ I = \frac{Q_b-Q_a}{Q_b+Q_a} $$

Mercer uses the microprice as its primary live value:

$$ \mu = \frac{aQ_b+bQ_a}{Q_b+Q_a} $$

If the spread is $s=a-b$, the same relationship can be written as:

$$ \mu = m + \frac{s}{2}I $$

The microprice therefore leans toward the side whose best queue has more quantity, making pressure visible before a price level disappears.

Rates become events

Limit arrivals, market orders, and cancellations each have a state-dependent hazard $\lambda_i(x)$. Their total rate is:

$$ \Lambda(x) = \sum_i \lambda_i(x) $$

The waiting time to the next event is exponentially distributed:

$$ \Delta t = \frac{-\ln(1-U)}{\Lambda(x)}, \qquad U \sim \mathcal{U}(0,1) $$

The event type is selected by its share of the total rate:

$$ P(i \mid x) = \frac{\lambda_i(x)}{\Lambda(x)} $$

The simulated clock jumps directly from one order-book event to the next. Two seeded xoshiro128** streams keep scheduling and event details reproducible.

Inventory changes a maker's quotes

For midpoint $S$, inventory $q$, risk aversion $\gamma$, volatility $\sigma$, and remaining horizon $\tau=T-t$, the market maker's reservation price is:

$$ r = S - q\gamma\sigma^2\tau $$

Mercer uses the finite-horizon spread approximation:

$$ \delta = \gamma\sigma^2\tau + \frac{2}{\gamma}\ln\left(1+\frac{\gamma}{\kappa}\right) $$

Here $\kappa$ controls how quickly execution intensity falls with quote distance. At $\gamma=0$, the engine uses the continuous limit $\delta=2/\kappa$.

Quotes are placed around $r$ and rounded outward to valid ticks:

$$ p_b = r-\frac{\delta}{2}, \qquad p_a = r+\frac{\delta}{2} $$

A long position lowers the reservation price and encourages selling; a short position raises it and encourages buying. The maker submits through the same matching engine as every other participant.

Experiments

Mercer includes deterministic scenarios for balanced flow, liquidity shocks, one-sided pressure, cancellation spikes, large meta-orders, tick-size changes, and different market-maker risk policies.

Twin mode runs a control and one-parameter intervention from aligned seeded random streams. This makes the effect of a single market rule visible without changing the rest of the experiment.

Implementation

  • Next.js 16.3, React 19.2, and TypeScript.
  • A deterministic engine isolated under src/engine.
  • A native Web Worker for simulation and clock control.
  • Liveline 0.0.7 for the Canvas-rendered temporal chart.
  • An exact order-book ladder for price levels, aggregate depth, and FIFO queues.
  • Node's built-in test runner for matching, determinism, scenarios, and model invariants.

Run locally

This project uses pnpm.

pnpm install
pnpm dev

Open http://localhost:3000.

Run the release checks with:

pnpm test
pnpm typecheck
pnpm lint
pnpm build

Reference

The inventory-aware quoting policy is based on Marco Avellaneda and Sasha Stoikov's paper, High-frequency trading in a limit order book. Mercer's surrounding stochastic exchange is a qualitative synthetic model, not an empirical calibration or a reproduction of every assumption in the paper.

About

A live synthetic exchange

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages