| Crates.io | order_book_parser |
| lib.rs | order_book_parser |
| version | 0.1.1 |
| created_at | 2025-11-19 13:05:13.627224+00 |
| updated_at | 2025-11-19 18:42:07.603963+00 |
| description | A parser for order book data in financial markets, parsing bids and asks from a string format. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1940098 |
| size | 54,690 |
This project implements a parser for order book data, which is commonly used in financal markets to represet buy (bids) and sell (asks) orders for a security. The parser uses the Pest grammar to parse a string representation of an order book in the format "BIDS:level_list;ASKS:level_list".
The parsing results can be used in trading applications, market analysis tools, or simulations to process and analyze order book snapshots. In particular, this parser includes a Matching Engine that allows you to simulate Buy/Sell Market orders and predict the PnL (Profit and Loss) of open positions based on the actual market depth.
The parser offers the following capabilities:
The parser processes a string input representing an order book snapshot. The input format is:
The Pest grammar (grammar.pest) defines rules for:
The parsing process transforms a raw string into a structured financial object through several stages:
bids_side, asks_side, level).OrderBook struct using rust_decimal for high-precision arithmetic (avoiding floating-point errors).InstrumentConfig.Position object.The parsing logic is defined in grammar.pest using PEG rules. The parser processes the input format: "BIDS:price,qty|price,qty;ASKS:price,qty|price,qty"
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
ASCII_DIGIT = {'0'..'9'}
bids_identifier = { "BIDS" }
asks_identifier = { "ASKS" }
integer = @{ ASCII_DIGIT+ }
number = @{ integer ~ ("." ~ integer)? }
// A single price level: "100.5,10"
level = { number ~ "," ~ number }
// A list of levels separated by "|": "100.5,10|100.0,5"
level_list = { (level)? ~ ("|" ~ level)* }
bids_side = { bids_identifier ~ ":" ~ level_list }
asks_side = { asks_identifier ~ ":" ~ level_list }
// Root rule
order_book = { bids_side ~ ";" ~ asks_side }
The project includes a CLI built with clap. To ensure data integrity, instrument configuration arguments are mandatory for parsing.
cargo run -- credits
cargo run -- parse --file data/sample.txt --tick-size 0.5 --min-lot 1.0 --lot-step 1.0
cargo run -- parse --file data/sample.txt --tick-size 0.5 --min-lot 1.0 --lot-step 1.0 --action buy --amount 5.0
The template is located in data/sample.txt folder:
BIDS:100.0,10|99.5,20;ASKS:101.0,5|102.0,10
Our command:
cargo run -- parse --file data/sample.txt --tick-size 0.5 --min-lot 1.0 --lot-step 1.0 --action buy --amount 3.0
Result:
Reading file: "data/sample.txt"
Applying Config: Tick=0.5, MinLot=1, Step=1
✅ Successfully parsed and validated Order Book!
Order Book:
ASKS (Top): [Level { price: 101.0, quantity: 5 }, Level { price: 102.0, quantity: 10 }]
BIDS (Top): [Level { price: 100.0, quantity: 10 }, Level { price: 99.5, quantity: 20 }]
--- Executing Buy Market Order for 3 ---
Result: Order Filled!
- Quantity: 3
- Open price: 101.0
- PnL: -3.0
Updated Order Book State:
Order Book:
ASKS (Top): [Level { price: 101.0, quantity: 2 }, Level { price: 102.0, quantity: 10 }]
BIDS (Top): [Level { price: 100.0, quantity: 10 }, Level { price: 99.5, quantity: 20 }]
The project uses a makefile to automate routine tasks:
# Run full check cycle (Format + Lint + Test) - DO THIS BEFORE COMMIT
make check
# Run unit & integration tests
make test
# Display Help
make run
License: MIT. This project was developed as part of a coursework requirement Author: Mykhailo Pilat