A professional-grade quoting simulator using the Avellaneda-Stoikov model on live Binance data and historical backtesting.
This repository features a comprehensive market making simulation suite that illustrates the intersection of deterministic infrastructure and stochastic control theory. The simulator includes both live trading demonstrations and historical backtesting capabilities.
The live simulator connects to real-time crypto markets (via free Binance WebSockets), computes high-frequency bid/ask quotes dynamically based on inventory exposure, and visualizes live P&L data using a sleek Streamlit dashboard.
The backtesting module allows for offline analysis using synthetic or historical data, enabling strategy optimization and performance evaluation across different market conditions.
It replicates the fundamental architecture of what quantitative developers handle at firms like Jane Streetโfrom maintaining low-latency data feeds to implementing risk limits and automated kill switches against drawdowns.
- Live Order Book Sync: Streams real
BTCUSDTandETHUSDTtick data over WebSockets (latency-optimized background threading). - Dynamic Quoting (Avellaneda-Stoikov): Calculates optimal bid and ask spreads using stochastic control parameters.
- Generates a tailored Reservation Price factoring in your current inventory skew.
- Dynamically widens or narrows Optimal Spread based on market volatility and set liquidity density.
- Advanced Market Regime Detection: Real-time classification into CALM, TRENDING, and VOLATILE states with confidence scoring.
- Volatility Estimation: Rolling window standard deviation with 100-tick history for adaptive risk management.
- Microprice Calculator: Order book weighting for divergence signals and reservation price adjustments.
- Dynamic Position Sizing: Kelly-inspired sizing with multi-factor adjustment (confidence, volatility, inventory utilization).
- Adverse Selection Analysis: Toxicity detection measuring quote fill quality and execution costs.
- Simulated Real-time Fills: Top-of-book crossover deterministic matching system. Watch your inventory spike as market swings trigger fill conditions against your quotes!
- Strict Risk Management Layer: Built-in "Kill Switch" circuitry that bounds maximum inventory ($BTC/$ETH) and caps unrealized drawdowns.
- Live Analytical Dashboard: Instant visualization of realized/unrealized P&L, quote distribution relative to mid-price, and inventory positioning using responsive Plotly charts.
- Historical Backtesting: Run the strategy on synthetic or historical data to evaluate performance, optimize parameters, and analyze different market regimes.
- Synthetic Data Generation: Generate realistic market data with configurable volatility regimes, order flow imbalance, and microprice dynamics.
- Backtest Reporting: Automated HTML reports with detailed P&L analysis, trade logs, and performance metrics.
- Parameter Sensitivity Analysis: Grid search optimization across gamma and liquidity parameters.
- Performance Analytics: Sharpe/Sortino ratios, max drawdown, win rate, and rolling statistics.
flowchart TD
%% Styling
classDef external fill:#f3ba2f,stroke:#333,stroke-width:2px,color:#000
classDef layer fill:#2b2b2b,stroke:#00a8ff,stroke-width:2px,color:#fff
classDef presentation fill:#ff4b4b,stroke:#333,stroke-width:2px,color:#fff
%% External Data Source
Binance{{"Binance WebSocket<br/>(Live Order Book)"}}:::external
%% Simulator Components
subgraph Simulator Data Flow
Streamer["Data Layer<br/>(stream.py)"]:::layer
Model["Strategy Layer<br/>(model.py)"]:::layer
Engine["Execution Engine<br/>(engine.py)"]:::layer
Risk["Risk Manager<br/>(risk.py)"]:::layer
end
%% Presentation
Dashboard["Streamlit Dashboard<br/>(dashboard.py)"]:::presentation
Charts[("Plotly Real-time Charts")]:::layer
%% Edges
Binance -- "Tick Data (bestBid/Ask)" --> Streamer
Streamer -- "Live Top-of-Book" --> Dashboard
Dashboard -- "Mid Price" --> Model
Engine -. "Current Position" .-> Model
Model -- "Optimal Quotes & Spread" --> Dashboard
Dashboard -- "Evaluate Quotes vs Market" --> Engine
Engine -- "Simulated Fills (Inventory + P&L)" --> Dashboard
Engine -- "Risk Metrics" --> Risk
Risk -- "Kill Switch / Halts" --> Dashboard
Dashboard -- "Metrics Render" --> Charts
The simulator is built entirely in Python, reflecting a modular, micro-service-like component design:
๐ mm-simulator/
โโโ stream.py # WebSocket Manager feeding order-book top states to shared memory
โโโ model.py # Core Math (Avellaneda-Stoikov parameterizations + AI prediction)
โโโ engine.py # Virtual matching engine assessing real-market hits against our quotes
โโโ risk.py # Independent observer enforcing threshold logic to halt trading
โโโ dashboard.py # Streamlit GUI coordinating threads, state loops, and Plotly UI
โโโ backtest.py # Historical backtesting engine for strategy evaluation
โโโ generate_history.py # Synthetic market data generator with regime shifts
โโโ volatility.py # Real-time volatility estimation and regime detection
โโโ sizing.py # Dynamic position sizing with Kelly-inspired logic
โโโ analytics.py # Adverse selection analysis and performance metrics
โโโ dashboard_utils.py # Professional visualization components for Streamlit
โโโ history.csv # Generated historical/synthetic market data
โโโ backtest_report.html # Automated backtest performance report
โโโ COMPLETION_REPORT.md # Detailed implementation checklist and status
โโโ ENHANCEMENTS.md # Comprehensive enhancement documentation
โโโ requirements.txt # Python dependencies
โโโ README.md
The simulator has been significantly enhanced with four new specialized modules:
volatility.py: ImplementsVolatilityEstimator,RegimeDetector, andMicropriceCalculatorclasses for real-time market analysis.sizing.py: ContainsDynamicSizerfor Kelly-inspired position sizing with multi-factor risk adjustment.analytics.py: ProvidesAdverseSelectionAnalyzer,ParameterSensitivityAnalyzer, andPerformanceAnalyticsfor comprehensive strategy evaluation.dashboard_utils.py: Professional visualization utilities includingSessionAnalyticsand multiple chart creation functions.
COMPLETION_REPORT.md: Detailed checklist of all implemented features and validation results.ENHANCEMENTS.md: Comprehensive documentation of all enhancements, code changes, and testing outcomes.
flowchart LR
%% Styling
classDef actor fill:#8e44ad,stroke:#fff,stroke-width:2px,color:#fff
classDef usecase fill:#e67e22,stroke:#d35400,stroke-width:2px,color:#fff
User([Trader / Quant]):::actor
API([Binance WebSocket]):::actor
Data([Historical Data]):::actor
subgraph MM Simulator
UC1([Start / Stop Live Engine]):::usecase
UC2([Adjust Risk & Volatility Params]):::usecase
UC3([Monitor Live P&L & Inventory]):::usecase
UC4([Receive Live Order Book Ticks]):::usecase
UC5([Execute Simulated Fills]):::usecase
UC6([Halt on Risk Limit Exceeded]):::usecase
UC7([Generate Synthetic Data]):::usecase
UC8([Run Historical Backtest]):::usecase
UC9([Analyze Backtest Results]):::usecase
end
User --> UC1
User --> UC2
User --> UC3
User --> UC7
User --> UC8
User --> UC9
API --> UC4
UC4 -.-> UC5
UC5 -.-> UC3
UC6 -.-> UC1
Data --> UC8
UC8 -.-> UC9
stateDiagram-v2
%% Styling
classDef idleState fill:#27ae60,color:#fff,stroke:#fff,stroke-width:2px
classDef actionState fill:#2980b9,color:#fff,stroke:#fff,stroke-width:2px
classDef dangerState fill:#c0392b,color:#fff,stroke:#fff,stroke-width:2px
classDef streamState fill:#2c3e50,color:#fff,stroke:#3498db,stroke-width:2px
classDef backtestState fill:#9b59b6,color:#fff,stroke:#fff,stroke-width:2px
[*] --> Idle
Idle --> DataStream : User clicks 'Start MM'
Idle --> GenerateData : User runs backtest
state DataStream {
[*] --> AwaitTick
AwaitTick --> CheckRisk : Tick Received
CheckRisk --> Halt : Limits Breached
CheckRisk --> CalcQuotes : Safe
CalcQuotes --> CheckFills : Bid & Ask Calculated
CheckFills --> UpdateEngine : Market crosses Quotes
CheckFills --> AwaitTick : No Fill
UpdateEngine --> AwaitTick
}
state Backtest {
[*] --> LoadData
LoadData --> ProcessTick : Data Loaded
ProcessTick --> CheckRiskBT : Tick Processed
CheckRiskBT --> HaltBT : Limits Breached
CheckRiskBT --> CalcQuotesBT : Safe
CalcQuotesBT --> CheckFillsBT : Quotes Calculated
CheckFillsBT --> UpdateEngineBT : Fill Occurred
CheckFillsBT --> ProcessTick : No Fill
UpdateEngineBT --> ProcessTick
HaltBT --> GenerateReport
ProcessTick --> GenerateReport : End of Data
GenerateReport --> [*]
}
GenerateData --> Backtest
Halt --> Idle : Auto-Stopped / User Resets
class Idle idleState
class AwaitTick actionState
class CheckRisk actionState
class CalcQuotes actionState
class CheckFills actionState
class UpdateEngine actionState
class Halt dangerState
class DataStream streamState
class GenerateData backtestState
class LoadData backtestState
class ProcessTick backtestState
class CheckRiskBT backtestState
class CalcQuotesBT backtestState
class CheckFillsBT backtestState
class UpdateEngineBT backtestState
class HaltBT backtestState
class GenerateReport backtestState
The core algorithm dynamically alters the mid-price to a "Reservation Price"
And it defines the "Optimal Spread"
Where:
-
$s$ : Current Mid Price -
$q$ : Inventory position -
$\gamma$ : Risk Aversion factor -
$\sigma$ : Volatility factor -
$k$ : Market liquidity density
(We treat $(T-t)$ as $1.0$ for a continuous approximation).
- Python (Core engine)
websocket-client(Real-time Binance connections)- Streamlit (State-managed frontend and user interaction loop)
- Pandas & NumPy (Fast numeric arrays and rolling dataframes)
- Plotly (High-performance charting)
Ensure you have Python 3.11 or higher installed. (Note: Some pre-releases like 3.14 may not support pre-compiled pandas/streamlit binaries).
Clone the repo and navigate to the project directory. Install the necessary dependencies:
cd mm-simulator
python -m pip install -r requirements.txtStart the Streamlit dashboard loop:
python -m streamlit run dashboard.pyOnce the browser window opens:
- Hit Start MM in the left sidebar to connect to the Binance feed.
- The UI will establish a connection, and high-frequency quote generation will begin!
- Play with Risk Aversion (
$\gamma$ ), Volatility, and Liquidity Density on the fly to see how the engine instantly transforms your quoting behavior!
Create realistic historical data for backtesting:
python generate_history.pyThis generates history.csv with configurable market conditions including volatility regimes and order flow imbalance.
Execute the backtesting engine on the generated data:
python backtest.pyThe script will:
- Load historical data from
history.csv - Run the Avellaneda-Stoikov strategy with dynamic parameters
- Generate an HTML report (
backtest_report.html) with detailed analysis - Display key performance metrics including Sharpe ratio, max drawdown, and trade statistics
- Perform adverse selection analysis and parameter sensitivity sweeps
Modify parameters in backtest.py:
gamma: Risk aversion factork: Liquidity densityofi_weight: Order flow imbalance sensitivity- Adjust risk limits in the
RiskManagerinitialization
This section documents the latest code stabilizations and bug fixes applied in this session:
- UI emoji cleanup: removed decorative section icons, retained status indicators (
๐ข,๐ก,๐ด). dashboard_utils.create_statistics_panel()variable fix: correctedtotal_pnlusage.- Risk logic event thresholds tightened for safe comparison (inventory
> max_inventory, drawdowntotal_pnl < max_drawdown). - AI confidence model enhanced in
model.pyfor warm-up and model-quality scoring. - AI return display scale adjusted in dashboard to avoid
0.000bpsfor small epsilon signals. - README.md updated with all enhanced features, new modules, and complete file structure.
This is a simulated trading environment designed for robust quantitative testing, portfolio planning, and demonstrating low-latency Python system design. It is completely sandbox-based and does not actually place orders or risk real capital on Binance. Use responsibly.