How yanguangshaonian/order_book works
1. Repository purpose
- This repo is a Python behavioral model for SSE/SZSE order-book snapshot reconstruction, aimed at validating algorithm behavior and estimating FPGA resource needs.
- It is not packaged as a library; scripts are intended to be run from the repository root.
2. Main execution path
- The top-level runner is run_test_behave.py.
- It drives the reconstruction model against historical market data and is the main entry point for experiments and regression checks.
3. Behavior model subsystem
- The core algorithm lives under behave/, especially behave/axob.py.
- This is the “snapshot reconstruction” behavioral model described in the README: it maintains local order-book price levels, reconstructs snapshots from order/trade events, and compares reconstructed results with exchange snapshots.
4. Event/message layer
- The tool/ package contains the market-message and state abstractions used by the model:
- axsbe_order.py: order-message handling
- axsbe_exe.py: execution/trade-message handling
- axsbe_status.py: market status / trading-state handling
- axsbe_snap_stock.py: stock snapshot representation
- axsbe_base.py: shared base types or common parsing/dispatch logic
- These modules form the domain model for the reconstruction pipeline.
5. Data ingestion boundary
- tool/msg_util.py is the main data-access utility mentioned in the README.
- It reads the project’s historical Level-2 data files, such as the sample ZIP under data/20220426/AX_sbe_szse_300750.zip.
- This is the boundary between raw exchange data and the internal event model.
6. Processing / orchestration
- tool/pipeline.py likely coordinates ordered processing of incoming market messages into the reconstruction engine.
- The pipeline boundary is important: it separates parsing/normalization from the stateful order-book update logic.
7. State and buffering utilities
- tool/simpleStack.py suggests a lightweight stack structure used for temporary buffering or event correlation.
- This fits the README’s note that orders may be cached until corresponding executions arrive, because the exchange sends trades immediately after orders.
8. Behavioral model specifics
- The reconstruction approach is price-level based, not full order-queue based.
- That means the model maintains price ladders and quantities, then updates them using orders plus subsequent execution messages to produce a new snapshot.
- In architectural terms, the central state is a compact book representation rather than a per-order matching engine.
9. Testing subsystem
- behave/test/test_axob.py is the focused unit/regression test for the behavioral model.
- behave/test/__init__.py marks the test package, and run_test_behave.py likely acts as the orchestration script for broader test runs.
10. Support code
- tool/test_util.py and behave/mu.py appear to be helper modules for tests or mock/unified utilities.
- These are not core architecture pieces, but they support reproducibility and validation.
11. Outputs and artifacts
- The README indicates logs are written under a log/ directory at runtime, though that directory is not currently in the tree.
- The repo’s runtime artifacts are therefore separate from source and historical input data.
12. Key architectural boundary to diagram
- The clearest system boundary is:
historical SZSE L2 file(s) → message parsing/utilities → ordered event pipeline → snapshot reconstruction model → test/assertion/comparison output.
- That end-to-end flow is the main architecture of the repository.
Main entry points: axob.py AXOB · pipeline.py Pipeline · axsbe_base.py Base Types · test_axob.py Model Test
Components 15
Entry & Data
Script entry point and historical market data boundary.
- Test Runner · Top-level experiment and regression entry point.
run_test_behave.py - L2 Archive · Historical SZSE market data used by the model.
data/20220426/AX_sbe_szse_300750.zip
Message Toolkit
Parsing, message types, and pipeline coordination.
- Msg Util · Reads historical message files and adapts raw data for processing.
tool/msg_util.py - Pipeline · Coordinates ordered processing of incoming market messages.
tool/pipeline.py - Base Types · Shared message and parsing foundations.
tool/axsbe_base.py - Order Msg · Order-event handling and normalization.
tool/axsbe_order.py - Trade Msg · Execution/trade-event handling.
tool/axsbe_exe.py - Market Status · Trading-state and session-status handling.
tool/axsbe_status.py - Snapshot · Stock snapshot representation used as the exchange reference.
tool/axsbe_snap_stock.py
Behavior Model
Snapshot reconstruction logic and its test surface.
- AXOB · Price-level snapshot reconstruction and comparison logic.
behave/axob.py - Model Test · Focused test coverage for the behavioral model.
behave/test/test_axob.py - Test Package · Marks the behavioral test package.
behave/test/__init__.py
Support
Shared helpers used by the model and tests.
- Stack · Lightweight buffering or correlation structure for event sequencing.
tool/simpleStack.py - MU Helpers · Shared helper routines used by model and tests.
behave/mu.py - Test Utils · Helper functions for reproducible checks.
tool/test_util.py
Connections 22
- Test Runner → Msg Util: loads · Runner pulls historical data through message utilities.
- Test Runner → Pipeline: drives · Runner executes the ordered processing pipeline.
- Msg Util → L2 Archive: reads · Utility boundary from raw archive to parsed messages.
- Msg Util → Base Types: parses via · Raw exchange records are normalized through shared base types.
- Base Types → Order Msg: defines · Common foundations for order messages.
- Base Types → Trade Msg: defines · Common foundations for trade messages.
- Base Types → Market Status: defines · Common foundations for market-status messages.
- Base Types → Snapshot: defines · Common foundations for snapshot representation.
- Pipeline → Order Msg: routes · Pipeline dispatches order events.
- Pipeline → Trade Msg: routes · Pipeline dispatches execution events.
- Pipeline → Market Status: routes · Pipeline dispatches session-status events.
- Pipeline → Stack: buffers · Pipeline uses temporary buffering for sequencing or correlation.
- Pipeline → AXOB: feeds · Normalized event stream enters the reconstruction model.
- Order Msg → AXOB: updates · Order messages modify reconstructed book state.
- Trade Msg → AXOB: applies · Executions are used to reconcile quantities and snapshots.
- Market Status → AXOB: gates · Trading-state changes affect model processing.
- Snapshot → AXOB: compares · Exchange snapshots are used as the reference for validation.
- AXOB → Model Test: verified by · Regression tests exercise the reconstruction behavior.
- Model Test → Test Utils: uses · Tests rely on shared testing helpers.
- Model Test → MU Helpers: uses · Tests share utility routines with the model.
- AXOB → MU Helpers: shares · Model uses common helper utilities.
- Test Package → Model Test: packages · Test package groups the behavioral tests.