YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
Flash Crash Early Warning β Market Microstructure ML Detector
A real-time detector on limit-order-book (LOB) streams that flags microstructure anomalies (order-book imbalance, spoofing, liquidity evaporation) 50β500ms before price dislocation.
Built as a 5-stage hybrid detection cascade: Statistical pre-filter β Isolation Forest β Temporal Convolutional Network (TCN) β Cross-symbol correlation breakdown β Bayesian aggregator. The operating cascade is S1βS2βS3βS5 (Stage-4's Transformer is disabled in the shipped configs/pipeline.yml; the validated correlation-breakdown signal is wired as the real Stage-4 input). Operating point: models/stage3_tcn_prod.pt @ threshold 0.5, trailing-realized-vol gate β₯ 2 bps, 10 s cooldown. Held-out crash recall 0.38β1.0 with zero calm-day alerts.
β οΈ Status: MVP / research prototype. Not production trading software. See Risks & Disclaimer.
Architecture
Binance WebSocket ββ
βββ Feature Extractor ββ 5-Stage Cascade ββ Alert Router
FI-2010 (offline) ββ (~2 ms) Stage 1: Statistical (console/JSONL/
Stage 2: Isolation Forest Slack/PD/webhook)
Stage 3: TCN (~8 ms)
Stage 4: correlation (disabled-by-default transformer)
Stage 5: Bayesian agg (~1 ms)
The Rust proxy (proxy/) is an experimental ingest path that currently only opens a TCP port; the live feed uses the Node mini-service (binance-stream). See docs/ANALYSIS-2026-08-11.md for the full reality-vs-docs audit.
End-to-end p99: ~27 ms (measured, single-tick cascade) Β· Open-source first.
Quickstart
Prerequisites
- Python 3.11+
- Rust 1.75+ (optional β only for the high-performance proxy)
- 4 GB RAM, 5 GB disk
Option A β Python-only quickstart (recommended for first run)
# 1. Install Python deps
cd ml
pip install -r requirements.txt
# 2. Download a sample of Binance historical data (BTC/USDT, 1 day)
python -m flash_crash_watchdog.data.download_binance \
--symbol BTCUSDT --date 2021-05-19 --out ../data/
# 3. Run the offline backtest on the May 19, 2021 BTC flash crash
python -m flash_crash_watchdog.cli backtest \
--data ../data/BTCUSDT_2021-05-19.parquet \
--model configs/tcn_baseline.yml
# 4. Or: start the live detector against Binance WebSocket
python -m flash_crash_watchdog.cli live --symbol BTCUSDT
Option B β Full stack with Rust proxy (for sub-ms ingest)
# 1. Build the Rust proxy
cd proxy
cargo build --release
# 2. Run the proxy (ingests Binance WebSocket, publishes to localhost:5555)
./target/release/flash-crash-proxy --symbol BTCUSDT --out tcp://127.0.0.1:5555
# 3. In another terminal, run the Python detector consuming from the proxy
cd ../ml
python -m flash_crash_watchdog.cli live --source tcp://127.0.0.1:5555
Option C β Docker Compose (everything wired up)
docker-compose up -d
# Dashboard: http://localhost:3000
# Prometheus metrics: http://localhost:9090
Project Structure
flash-crash-watchdog/
βββ proxy/ # Rust WebSocket proxy (sub-ms ingest)
β βββ Cargo.toml
β βββ src/
β βββ main.rs
β βββ binance_client.rs
β βββ lob.rs # Limit order book reconstruction
β βββ publisher.rs
βββ ml/ # Python ML pipeline
β βββ requirements.txt
β βββ setup.py
β βββ flash_crash_watchdog/
β βββ __init__.py
β βββ cli.py # Command-line entrypoint
β βββ features/ # 20 features in 5 families
β β βββ price_action.py
β β βββ depth_imbalance.py
β β βββ flow_toxicity.py
β β βββ volatility.py
β β βββ cross_symbol.py
β βββ models/ # 5-stage cascade
β β βββ stage1_statistical.py
β β βββ stage2_isolation_forest.py
β β βββ stage3_tcn.py
β β βββ stage4_transformer.py
β β βββ stage5_bayesian.py
β β βββ cascade.py # Orchestrator
β βββ data/
β β βββ download_binance.py
β β βββ fi2010_loader.py
β β βββ labels.py
β βββ eval/
β β βββ backtest.py
β β βββ metrics.py
β βββ alert/
β βββ router.py
βββ dashboard/ # Next.js real-time dashboard
β βββ package.json
β βββ src/
βββ configs/ # Model + pipeline configs
β βββ tcn_baseline.yml
β βββ transformer_cross_symbol.yml
β βββ pipeline.yml
βββ scripts/ # Helper scripts
β βββ train_tcn.py
β βββ run_backtest.py
β βββ replay_crash.py
βββ data/ # Local data (gitignored)
βββ docs/ # Architecture + API docs
β βββ ARCHITECTURE.md
β βββ DATA_SOURCES.md
β βββ API.md
βββ tests/ # Test suite
βββ docker-compose.yml
βββ Makefile
βββ README.md
Data Sources
All datasets are free and publicly accessible.
| Dataset | Use | Access |
|---|---|---|
| Binance public data | Historical crashes (May 2021 BTC, May 2022 LUNA) + live WebSocket | data.binance.vision (CSV) Β· wss://stream.binance.com:9443 (live) |
| FI-2010 benchmark | Academic LOB benchmark, labeled mid-price movement | etsin.fairdata.fi |
| LOBSTER (academic) | NASDAQ TotalView reconstruction | lobsterdata.com β free with university email |
| NASDAQ TotalView-ITCH | Raw protocol parsing demo | data.nasdaq.com/databases/NTV |
See docs/DATA_SOURCES.md for full details.
Detection Cascade
| Stage | Algorithm | Latency | Pass-through | Catches |
|---|---|---|---|---|
| 1 | Statistical pre-filter (micro-price velocity, spread z-score) | < 0.1 ms | 5% | Obvious normal ticks |
| 2 | Isolation Forest (12 features) | ~1 ms | 20% of suspects | OBI shifts, cancellation spikes |
| 3 | Temporal Convolutional Network (8 dilated layers, 500ms receptive field) | ~8 ms | 40% | Collective temporal anomalies |
| 4 | Correlation breakdown (anchor-vs-basket return-corr collapse) β the Transformer variant is disabled in the shipped config | ~1 ms (corr only) | 60% | Correlation breakdown / decoupling |
| 5 | Bayesian aggregator | ~1 ms | β | Final alert decision |
See docs/ARCHITECTURE.md for full design.
Feature Engineering
20 features in 5 families, extracted per tick:
| Family | Features | Stage |
|---|---|---|
| F1 β Price & Action (5) | mid-price velocity, micro-price, spread, trade arrival rate, cancel-to-trade ratio | 1, 2 |
| F2 β Depth & Imbalance (5) | bid/ask depth L1-L10, OBI, weighted mid, depth slope, liquidity vacuum flag | 1, 2 |
| F3 β Flow & Toxicity (4) | VPIN, Kyle's Ξ», effective spread, realized spread | 3 |
| F4 β Volatility (3) | realized vol, micro-price variance ratio, Garman-Klass | 3 |
| F5 β Cross-Symbol (3) | pairwise return correlation, lead-lag, co-integration residual | 4 |
Evaluation
Three regimes (see docs/ARCHITECTURE.md Β§7):
- Offline backtest β replay 6 months of LOB data, inject controlled crashes, measure per-stage precision/recall/TTD
- Online shadow β run alongside production for 30 days, compare alerts to real market events
- Adversarial red team β inject 100 synthetic crash patterns quarterly
Target envelope: detect 80% of crashes with > 200ms early warning Β· false-positive rate < 2/hour Β· p99 latency < 50ms.
Tech Stack
| Layer | Choice | Status |
|---|---|---|
| Ingest | Node mini-service (Binance WS β Socket.io) β live; Rust proxy is an experimental TCP-only stub | live: Node |
| Stream processing | Python asyncio + Socket.io (MVP) β Flink was design-only | actual |
| Feature store | In-process rolling windows (no external store) | actual |
| Model serving | PyTorch (CPU) + ONNX export available | actual |
| Storage | SQLite (web) + Parquet (research data) β ClickHouse was design-only | actual |
| Dashboard | Next.js + shadcn/ui | actual |
| Monitoring | Prometheus /metrics (sidecar + stream) + Grafana-ready | actual |
Roadmap
- v0.1 β Python-only MVP: Binance ingest, 20 features, TCN, cascade, backtest
- [~] v0.2 β Rust proxy (experimental stub; live ingest uses the Node mini-service)
- [~] v0.3 β Stage 4 (replaced by the validated correlation-breakdown; transformer disabled)
- v0.4 β Next.js live dashboard + hardened auth (signed sessions, rate limits)
- β Corrected evaluation: event-based labels, shared rolling-z, canonical matching, Wilson CIs, ONNX, drift/calibration tooling
- v1.0 β Production hardening: GPU retrain, full6-day validation, multi-symbol depth
Risks & Disclaimer
This is a research prototype, not production trading software.
- False-positive cost: in live trading, false alerts trigger hedging cost. The alert threshold must be tuned per deployment.
- Concept drift: market microstructure evolves; weekly retraining required.
- Adversarial adaptation: spoofers evolve once they know detectors exist.
- Regulatory: any deployment that triggers automated trades requires Reg NMS / MiFID II compliance review.
- No financial advice: this software is for research and educational purposes only. The authors are not responsible for any financial losses incurred through its use.
License
Apache 2.0 β see LICENSE.
Citation
If you use this work, cite:
@software{flash_crash_watchdog,
title = {Flash Crash Early Warning: Market Microstructure ML Detector},
author = {Z.ai Quant Research},
year = {2026},
url = {https://github.com/yourusername/flash-crash-watchdog}
}
References
See the accompanying project brief PDF for the full bibliography (Easley & O'Hara, SEC/CFTC May 2010 report, Ntakaris et al. FI-2010, Vaswani et al. Transformer, etc.).