Skip to main content

Market Wave

Adaptive order flow. Exact price-time matching. No hidden price path.

PyPI Python Tests License

Market Wave is a seeded, in-memory continuous double auction driven by an adaptive ensemble of N predictive distributions. Orders arrive in continuous time, walk a live limit-order book, and match with price-time priority. Prices, spreads, and liquidity emerge only from those orders and executions—never from a latent price path or a post-generation correction.

The model represents aggregate market intent, not named traders. It is built for market-microstructure experiments and synthetic scenario generation, not for forecasting or calibrating a particular venue.

Market Wave symmetric order-book depth heatmap

A 300-second seeded run rendered by the public API. Time runs left to right; Ask L1 and Bid L1 meet at the center of the price-independent level ladder.

Install

Market Wave requires Python 3.10 or newer.

pip install market-wave

The renderer is optional, so simulation-only installs do not pull in Matplotlib:

pip install "market-wave[visualization]"

Quick start

Every configuration field is explicit. With identical Market Wave, Python, and NumPy versions, a fresh Market with the same configuration and seed produces exactly the same sequence.

from market_wave import Market, MarketConfig, Trade

market = Market(
    MarketConfig(
        initial_price=100_000,
        tick_size=1,
        step_seconds=1.0,
        order_rate=20.0,
        mean_price_offset_ticks=4.0,
        mean_order_size_lots=3.0,
        mean_order_lifetime_seconds=5.0,
        flow_component_count=64,
        seed=7,
    )
)

steps = tuple(market.stream(count=300))
last = steps[-1]
trade_count = sum(
    isinstance(event, Trade)
    for step in steps
    for event in step.events
)

print("best bid:", last.book.best_bid)
print("best ask:", last.book.best_ask)
print("trades:", trade_count)

flow_component_count=64 is an ensemble-resolution choice, not a calibrated market constant. Larger values sample the unit retention interval more densely and closer to both endpoints, at greater ensemble cost; values down to one are valid.

Visualize depth

Pass an already-produced, finite sequence of consecutive steps to the pure renderer:

from market_wave import render_depth_heatmap

path = render_depth_heatmap(
    steps,
    "artifacts/depth.png",
    level_count=12,
    title="Reference run · symmetric level ladder",
)
print(path)

The visualization contract is deliberately narrow:

  • x-axis: simulation time, with one step-end book snapshot per column;
  • y-axis: side-relative book rank, independent of absolute price;
  • row order: Ask L{N} ... Ask L1 | Bid L1 ... Bid L{N} from top to bottom;
  • color: a shared log(1 + resting quantity) scale;
  • input: a non-empty Sequence[Step] with consecutive indices and contiguous times;
  • output: a PNG file; missing parent directories are created and the resolved Path is returned.

Rendering never advances or mutates the market. A larger six-scenario comparison shows how activity, lifetime, placement width, seed, and N change the visible market.

How the engine works

N adaptive predictive laws
        │
        ├── combine side probabilities and price PMFs
        ├── combine quantity distributions
        └── combine lifetime distributions
        │
        ▼
sample each aggregate CDF directly
        │
        ▼
submit → match → rest → expire
        │
        ▼
completed Step feedback returns to every law

1. N memory scales, one completed observation

Every predictive law observes the same completed step. Law i retains a different fraction of its prior evidence:

rho_i = (i + 0.5) / N

rho_i is the evidence retained at each update, so larger values mean longer memory. The evenly spaced spectrum supplies multiple time scales without a hand-tuned decay schedule. Each law tracks order intensity, side probability, relative-price scale, order-size scale, and cancellation hazard.

2. Aggregate first, then sample

An order is never assigned to one component. The engine combines all N laws into side-conditional aggregate distributions and samples their aggregate CDFs directly. Price offsets use discrete-Laplace probability mass functions (PMFs), quantities use geometric components, and resting lifetimes use exponential components. Their support is unbounded except for the positive-price boundary.

3. Let visible liquidity reshape flow

When both book sides provide a support-preserving solution, the engine divides each price PMF into marketable, spread-improving, and neutral regions. It then applies the minimum-KL reweighting that balances predicted buy and sell quote impact while preserving the conditional shape inside each region. If such a projection is infeasible, the unconditioned aggregate distributions are used.

Likelihood-ratio correction keeps the resulting liquidity constraint from teaching the base price law its own selection bias. This feedback changes order flow; it never moves a price directly.

4. Match before learning

Crossing orders consume resting liquidity at maker prices under strict price-time priority. Only an unfilled remainder rests. Expiration clocks begin when orders rest, and fully filled orders cannot emit later cancellation events. The resulting submissions, sides, offsets, sizes, expirations, and live-order exposure feed every predictive law exactly once at the end of the half-open step.

Public contract

All MarketConfig fields are required:

Field Contract
initial_price positive integer and an exact multiple of tick_size
tick_size positive integer
step_seconds finite seconds greater than zero
order_rate finite expected orders/second, at least zero
mean_price_offset_ticks finite mean absolute offset in ticks, at least zero
mean_order_size_lots finite mean quantity in lots, at least one
mean_order_lifetime_seconds finite mean resting lifetime greater than zero
flow_component_count positive integer N
seed integer

The constructor rejects non-finite or numerically unrepresentable values. Prices and quantities remain exact Python integers.

The top-level API is intentionally small:

Surface Contract
Market.step() advances one feedback interval and returns one immutable Step
Market.stream(count) lazily advances the same market; count=None is unbounded
Step.events chronological Submission, Trade, and Cancellation values in [start_time, end_time)
Step.book immutable step-end BookSnapshot with ranked Level values
Market.book current immutable book snapshot
Market.buy_distribution, sell_distribution current aggregate price laws
EntryDistribution.probability(), .cdf() exact public aggregate price PMF and CDF
render_depth_heatmap() optional, non-mutating PNG renderer

Calling step() or consuming stream() mutates only the market's forward simulation state. Returned steps and snapshots do not retain a back-reference to mutable engine state.

Quantitative checks

The test suite covers matching invariants, event lifecycles, exact seeded reproducibility, aggregate-CDF sampling, numerical boundaries, feedback, and visualization semantics. A separate fixed regression run is also compared with 256 permuted, Poisson, or Gaussian null samples: sign persistence, activity persistence, one-step absolute-return persistence, event-count dispersion, and return kurtosis must each exceed the 99th percentile of the relevant null. Its 10-step variance ratio must remain inside the central 98% of the permuted-return null.

Scope

Market Wave is Python-only and in memory. It intentionally provides no CLI, persistence layer, replay engine, hidden calibration state, named-agent model, or financial forecast. The engine retains only bounded predictive state and the live order book; callers choose which yielded results to keep.

Released under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

market_wave-2.0.0.tar.gz (41.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

market_wave-2.0.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file market_wave-2.0.0.tar.gz.

File metadata

  • Download URL: market_wave-2.0.0.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for market_wave-2.0.0.tar.gz
Algorithm Hash digest
SHA256 88260649511910ba24a2117c6d528b567080bf58b1e235dd14da0379eef1271c
MD5 9af390fed852c15ee54331a0063a60c5
BLAKE2b-256 23e0513e54d149434b287175079189eef76a9d467e07502a0a53d76e67404d25

See more details on using hashes here.

Provenance

The following attestation bundles were made for market_wave-2.0.0.tar.gz:

Publisher: workflow.yml on smturtle2/market-wave

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file market_wave-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: market_wave-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for market_wave-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 307e0c33290291ed8d512e1c7b8a3b9fe4a2ca0277fa0be0d041fb4eff0b28f3
MD5 89af524e4488e0e5f3e59121df17f378
BLAKE2b-256 6ad587ed9a54d11ef85573af3d80490957f8597763dd0b4402e43241ed198e21

See more details on using hashes here.

Provenance

The following attestation bundles were made for market_wave-2.0.0-py3-none-any.whl:

Publisher: workflow.yml on smturtle2/market-wave

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

2.2.0

7 files

2.1.0

7 files

This release

2.0.0 This release

2 files

1.0.0

2 files

0.5.0

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page