OKX Trade Simulator Code Explanation
OKX Trade Simulator Code Explanation
Explanation
1. Overview
This simulator provides a real-time estimation of trading costs using OKX Level 2 order
book data. It estimates slippage, fees, market impact, and the probability of a trade being a
maker or taker. The dashboard is implemented using Streamlit and connects to a
WebSocket feed for live data.
2. models.py
- SlippageModel: Implements linear and quantile regression-based slippage estimation.
QuantReg fits the 95th percentile of slippage given liquidity.
3. simulator.py
- TradeSimulator: Core engine to simulate trades per tick.
4. utils.py
- fetch_okx_spot_assets(): Fetches tradable symbols from OKX.
- estimate_fees(): Calculates expected fees using a weighted average of maker and taker
rates based on predicted proportions.
- For each tick: computes metrics, updates UI placeholders, and optionally logs them.
- Tracks latency for full pipeline including WebSocket and UI render times.
6. dashboard.py
- run_dashboard(): Sets up and runs the main Streamlit interface.
7. layout.py
- render_sidebar(): Builds sidebar inputs (asset, order size, fee tier, etc.).
- Metrics include slippage, fees, impact, net cost, and latency breakdowns.
8. config.py
- Defines FEE_SCHEDULE: Maker and taker fees for different levels and user types (Regular,
VIP).
9. main.py
- Entry point for launching the Streamlit app by calling run_dashboard().
- Almgren-Chriss Model: Predicts temporary and permanent market impact from order size
and liquidity.
11. Execution Flow
1. User launches Streamlit and sets parameters.