This project implements an order book and a matching engine for managing and matching buy (bid) and sell (ask) orders in a financial market.
order_book/
├── src/
│ ├── main.rs
│ ├── matching_engine/
│ │ ├── mod.rs
│ │ ├── orderbook.rs
│ │ └── engine.rs
├── Cargo.toml
└── README.md
- main.rs: The entry point of the application.
- matching_engine/mod.rs: The module declaration for the matching engine.
- matching_engine/orderbook.rs: Contains the
OrderBook
,Order
,Limit
,Price
, andBidOrAsk
enums. - matching_engine/engine.rs: Contains the
MatchingEngine
andTradingPair
structs.
Ensure you have Rust installed.
-
Clone the repository:
git clone https://github.com/yourusername/order_book.git cd order_book
-
Build and run the application:
cargo run
mod matching_engine;
use matching_engine::engine::{MatchingEngine, TradingPair};
use matching_engine::orderbook::{Order, OrderBook, BidOrAsk};
fn main() {
let buy_order = Order::new(BidOrAsk::Bid, 5.5);
let mut orderbook = OrderBook::new();
orderbook.add_order(4.4, buy_order);
//println!("{:?}", orderbook);
let mut engine = MatchingEngine::new();
let pair = TradingPair::new("BTC".to_string(), "USD".to_string());
engine.add_new_market(pair.clone());
let buy_order = Order::new(BidOrAsk::Bid, 5.5);
engine.place_limit_order(pair, 10.000, buy_order).unwrap();
}
This is the entry point of the application where:
- An
OrderBook
is created, and a buy order is added. - A
MatchingEngine
is initialized, and a new market (trading pair) is added. - A buy order is placed in the matching engine's order book for the specified trading pair.
Defines the OrderBook
and related structures:
- BidOrAsk: Enum to differentiate between bid (buy) and ask (sell) orders.
- OrderBook: Struct managing
bids
andasks
at various price levels. - Order: Struct representing an order with size and type (bid or ask).
- Limit: Struct grouping orders at a specific price.
- Price: Struct for precise price representation, split into integral and fractional parts.
Defines the MatchingEngine
and TradingPair
:
- TradingPair: Struct representing a trading pair (e.g., BTC/USD).
- MatchingEngine: Struct managing multiple order books for different trading pairs.
add_new_market
: Adds a new trading pair to the matching engine.place_limit_order
: Places a limit order in the order book for the specified trading pair.
Module declaration to include orderbook
and engine
modules.
pub mod orderbook;
pub mod engine;
fn main() {
let mut engine = MatchingEngine::new();
let pair = TradingPair::new("BTC".to_string(), "USD".to_string());
engine.add_new_market(pair.clone());
let buy_order = Order::new(BidOrAsk::Bid, 5.5);
engine.place_limit_order(pair, 10.000, buy_order).unwrap();
}
This example initializes the matching engine, adds a new market (BTC/USD), and places a buy order at a specific price.
Feel free to submit pull requests or open issues to improve the project.
This README provides an overview of the project, how to get started, and a brief explanation of the main components and their functionalities.