feat: Phase 3 — event-driven market-making simulator + 53 tests
New sim/ module — 7 files + init, replays stored L2/trade data
through a realistic market-making simulation:
sim/engine.py (SimulationEngine):
Event-driven core — processes L2 updates, trades, mark prices
sequentially. Orchestrates queue model, maker quotes, fill sim,
constraints, scenarios. Supports periodic re-quoting and
stale order cancellation.
sim/queue.py (QueueModel):
Price-time FIFO queue per price level. Tracks where maker orders
sit in queue. Simulates order eating by aggressor trades.
fill_probability() — Poisson thinning model for fill odds.
sim/maker.py:
AvellanedaStoikovMaker — stochastic control quoting with
aeta, k, tau parameters. Reservation price based on inventory.
quote() and quote_with_skew() with configurable inventory tilt.
GridMaker — evenly-spaced grid quoting at N levels.
sim/fills.py:
FillSimulator — partial fills, adverse selection probability,
cancel latency (gaussian RTT). FillEvent/CancelEvent tracking.
adverse_selection_intensity() — measures post-fill price moves.
sim/constraints.py:
InventoryConstraint — long/short/net/gross position limits.
FundingConstraint — hourly funding cost estimation.
FeeSchedule — maker/taker fee calculation.
LiquidationRisk — liquidation price and safety distance.
CircuitBreaker — PnL, trade count, toxic rate, slippage trips.
ConstraintManager — unified pre-trade constraint check.
sim/scenario.py:
ScenarioEngine — randomized exchange downtimes, latency spikes,
volatility bursts. State query per sim_time for spread/trade-rate.
sim/reporter.py:
PnLReporter — component-level PnL breakdown:
spread_capture, inventory_pnl, fees, funding, adverse_selection.
SimulationStats — trade counts, fill rates, drawdown, sharpe.
Equity curve tracking and max drawdown computation.
53 new tests across 4 files (all pass):
test_sim_queue.py (12) — order placement, FIFO, fills, cancels
test_sim_maker.py (9) — A-S quotes, inventory skew, grid maker
test_sim_constraints.py (14) — limits, funding, fees, liquidation, breakers
test_sim_reporter.py (12) — PnL components, equity curve, stats
test_sim_engine.py (6) — full engine integration
Total test suite: 134 tests, all passing.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
Event-driven market-making simulator.
|
||||
|
||||
Replays L2 and trade data, models queue position, fill probability,
|
||||
adverse selection, and generates component-level PnL breakdowns.
|
||||
"""
|
||||
|
||||
from sim.engine import SimulationEngine, SimConfig
|
||||
from sim.queue import QueueModel, QueuePosition, QueueLevel, fill_probability
|
||||
from sim.maker import (
|
||||
AvellanedaStoikovMaker,
|
||||
GridMaker,
|
||||
MakerConfig,
|
||||
Quote,
|
||||
)
|
||||
from sim.fills import (
|
||||
FillSimulator,
|
||||
FillModelConfig,
|
||||
FillEvent,
|
||||
CancelEvent,
|
||||
adverse_selection_intensity,
|
||||
)
|
||||
from sim.constraints import (
|
||||
ConstraintManager,
|
||||
InventoryConstraint,
|
||||
FeeSchedule,
|
||||
FundingConstraint,
|
||||
LiquidationRisk,
|
||||
CircuitBreaker,
|
||||
)
|
||||
from sim.scenario import (
|
||||
ScenarioEngine,
|
||||
ScenarioConfig,
|
||||
ExchangeDowntime,
|
||||
LatencySpike,
|
||||
VolatilityBurst,
|
||||
)
|
||||
from sim.reporter import (
|
||||
PnLReporter,
|
||||
PnLBreakdown,
|
||||
SimulationStats,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"SimulationEngine", "SimConfig",
|
||||
"QueueModel", "QueuePosition", "QueueLevel", "fill_probability",
|
||||
"AvellanedaStoikovMaker", "GridMaker", "MakerConfig", "Quote",
|
||||
"FillSimulator", "FillModelConfig", "FillEvent", "CancelEvent",
|
||||
"adverse_selection_intensity",
|
||||
"ConstraintManager", "InventoryConstraint", "FeeSchedule",
|
||||
"FundingConstraint", "LiquidationRisk", "CircuitBreaker",
|
||||
"ScenarioEngine", "ScenarioConfig", "ExchangeDowntime",
|
||||
"LatencySpike", "VolatilityBurst",
|
||||
"PnLReporter", "PnLBreakdown", "SimulationStats",
|
||||
]
|
||||
Reference in New Issue
Block a user