0% found this document useful (0 votes)
30 views3 pages

OKX Trade Simulator Code Explanation

The OKX Trade Simulator provides real-time trading cost estimations using Level 2 order book data, including slippage, fees, and market impact. It is implemented with Streamlit and features various models for trade simulation, such as linear and logistic regression for slippage and maker/taker probabilities. The dashboard allows users to visualize metrics and monitor performance through a WebSocket connection for live data updates.

Uploaded by

soreko2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

OKX Trade Simulator Code Explanation

The OKX Trade Simulator provides real-time trading cost estimations using Level 2 order book data, including slippage, fees, and market impact. It is implemented with Streamlit and features various models for trade simulation, such as linear and logistic regression for slippage and maker/taker probabilities. The dashboard allows users to visualize metrics and monitor performance through a WebSocket connection for live data updates.

Uploaded by

soreko2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

OKX Trade Simulator - Full Code

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.

- MakerTakerModel: Uses logistic regression to estimate the probability of a trade being a


'maker' based on volatility and quantity.

3. simulator.py
- TradeSimulator: Core engine to simulate trades per tick.

- Computes live volatility using mid-price log returns.

- Calculates slippage using selected model.

- Estimates fees from the tiered schedule.

- Uses Almgren-Chriss model for market impact.

- Returns a summary of total trade cost and metrics.

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.

- almgren_chriss_impact(): Computes expected market impact based on theoretical


modeling of trade execution.
5. websocket_client.py
- listen_websocket(): Maintains connection to a WebSocket stream.

- Receives L2 order book updates in real time.

- 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.

- Initializes sidebar controls and output placeholders.

- Launches the WebSocket listener and simulation pipeline.

7. layout.py
- render_sidebar(): Builds sidebar inputs (asset, order size, fee tier, etc.).

- render_output_placeholders(): Creates UI sections to show metrics and plots.

- 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).

- ETA and GAMMA constants used in the Almgren-Chriss impact model.

9. main.py
- Entry point for launching the Streamlit app by calling run_dashboard().

10. Model Summary


- Linear Regression: Used for basic slippage as a function of quantity/liquidity.

- Quantile Regression: Captures the 95th percentile tail risk of slippage.

- Logistic Regression: Predicts probability of maker vs taker trade.

- Almgren-Chriss Model: Predicts temporary and permanent market impact from order size
and liquidity.
11. Execution Flow
1. User launches Streamlit and sets parameters.

2. WebSocket stream starts and delivers tick data.

3. Simulator calculates metrics for each tick.

4. Results displayed live in the dashboard and optionally logged.

5. Latency is measured and visualized to monitor performance.

You might also like