Skip to content

dhruvkjain/orderbook-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Under Construction


What is an orderbook ??

an orderbook is a store of buy(bid) and sell(ask) orders for a specific asset/financial instrument usually organized by price-time priority.

inshort: It shows who wants to buy or sell, how much, and at what price.

Order Book Levels

The different "levels" of order book data represent how much information is being shown or used.

  1. Level 1 (L1) - Top of the book Best Bid Offer (BBO), best ask => minimum ask somebody is willing to accept best bid => highest someone is willing to pay
  2. Level 2 (L2) – Market Depth Market by Price (MBP), Shows multiple prices, their quantity, number of asks/bids With L2, you can answer: “How much could I buy without moving the market too much?”
  3. Level 3 (L3) – Full Depth / Raw Orders Market by Order(MBO), Shows every individual order.

Order Matching Algorithms:

  • Price-Time Priority (FIFO): maintain a Priority Queue of asks/bids, FIFO as queue works on it. Match best price first. For equal price, oldest order wins.
  • Pro-Rata Matching: priority to high quantity at best price. Orders at the same price level are matched proportionally based on their sizes.
  • Size-Time Priority (Hybrid model): larger orders which came ahead of others get matched first if prices are equal. Orders are sorted first by size(like prorata), then by time within same size(like fifo).

Order Types:

  • GoodTillCancel: persist until filled or canceled.
  • FillAndKill (FAK): fill as much as possible immediately, any remaining qunatity is cancelled.
  • FillOrKill (FOK): completely filled immediately or cancel entire order.
  • GoodForDay: active for current trading day, automatically cancelled if not filled by end of day.
  • Market: whatever the best market price is just buy/sell.
  • CancelOnDisconnect (COD): open orders to be canceled if we lose connection to the market in order to reduce risk.

Rusty implementation:

  • Box<T> – Single Owner, Heap Allocation
  • Rc<T> – Multiple Owners (Single Thread) - Rc docs
  • Arc<T> – Multiple Owners (Multi-Thread) -

If you want mutability inside Rc or Arc, wrap your type:

  • Rc<RefCell<T>> for single-threaded mutable access
  • Arc<Mutex<T>> for multi-threaded mutable access

GitHub followers

Built with 🔨 & ☕️ | © 2025 Dhruv Jain

About

In-Memory Multi-Order Type Orderbook written in Rust from scratch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages