Skip to content

darthchudi/lob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Limit Order Book

Limit Order Book (LOB) is a minimal in-memory order book for matching bid and ask orders.

It uses price-time priority ordering to match orders by default, although the library is structured around a separable matching engine.

This library is intended as a research tool for experimenting with order matching behavior and order flow conditions.

Use cases

  • Experimentation bed for testing different matching algorithms
  • Reinforcement learning environments
  • Backtesting trading strategies
  • Building custom order books
  • Replaying historical order flow

Features

  • In-memory price-time priority matching: Uses sorted in-memory price levels with FIFO queues per level.
  • Extensible matching boundary: Structured around a separable matching engine, with the current execution layer tuned to the existing price-time-priority match plan.

Usage

use limit_order_book::core::limit_order_book::LimitOrderBook;
use limit_order_book::core::matching_engine::matching_engine::MatchingEngineStrategy;
use limit_order_book::core::utils;
use rust_decimal_macros::dec;

fn main() {
    // Initialize the orderbook with a price-time-priority matching engine
    let mut limit_order_book =
        LimitOrderBook::new(MatchingEngineStrategy::PriceTimePriority).unwrap();

    // Place a buy order and a sell order at the same price
    let buy_order = utils::new_buy_order("user_1", dec!(50), dec!(25)); // buy 50 units @ 25 per unit
    let sell_order = utils::new_sell_order("user_2", dec!(50), dec!(25)); // sell 50 units @ 25 per unit

    limit_order_book.add_order(buy_order).unwrap();
    limit_order_book.add_order(sell_order).unwrap();

    // Tick the order book to match orders
    let fills = limit_order_book.tick().unwrap();
    assert_eq!(fills.len(), 1);
    assert_eq!(fills[0].size, dec!(50))
}

Examples

1. Simple order flow

As shown in the examples/simple_order_flow.rs file, we can add orders to the order book and tick it to match orders.

This example uses synthetic orders to populate the orderbook and can be ran with cargo run --example simple_order_flow from the root directory.

cargo run --example simple_order_flow

2. Export the orderbook's depth snapshot for analysis and visualization.

As shown in the examples/depth_snapshot.rs file, the market depth for the order book can be exported for analysis and visualization.

This example uses synthetic orders to populate the orderbook and can be ran with cargo run --example depth_snapshot from the root directory. It exports a JSON file with the depth snapshot, which can be visualized using any tools that can parse JSON files and plot charts.

Depth Snapshot

cargo run --example depth_snapshot

Benchmarks

Benchmarks are located in the benches directory and can be run with cargo bench. The benchmarks are implemented using the criterion crate.

cargo bench

Current benchmark groups:

batch add orders

Time required to construct a fresh book, clone synthetic resting bids and asks, and add them to the book.

Batch Add Orders Timing

Batch Add Orders Throughput

Summary: 30.866ms median latency to construct a fresh book and add 10,000 resting orders. Median throughput is 323,990 input orders per second.

price time priority matching

Time required to fully match and drain a crossed-book with continuous_tick().

Match Orders Timing

Match Orders Throughput

Summary: 480.75ms median latency to fully match and drain a crossed-book containing 2,000 input orders. Median throughput is 4,160.2 input orders per second.

Limitations

  • The library currently only supports limit orders, this means that market orders, stop loss orders, and other similar order types are not yet supported.
  • The current execution engine assumes the existing price-time-priority style match plan, so swapping in a materially different matching algorithm will likely require execution-layer changes as well.
  • Calculating price-level volume requires walking the entire price level's order queue, which can be inefficient for orderbooks with large price levels.
  • Cancellation within a price level is still a linear scan over that level's resting orders.

About

Limit order book.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages