Skip to content

[Snapshot][MVP] Implementing a basic order book with in-memory data structures #5

@HminiL

Description

@HminiL

1. Overview

This document provides an overview of the first MVP implementation of a basic order book using in-memory data structures in Java. The system is designed to handle limit orders using a price-priority + FIFO (First-In-First-Out) matching algorithm.

For performance optimization, Java's PriorityQueue has been used to manage orders efficiently, ensuring fast retrieval of the best available prices.

2. Key Features

Price-First Matching + FIFO:

Orders are matched based on price priority, with older orders executed first if prices are equal.

Buy and Sell Order Processing:

  • Buy orders (matchBuyOrder()) execute when BUY Price >= SELL Price.
  • Sell orders (matchSellOrder()) execute when BUY Price >= SELL Price.

Immediate Execution:

If the BUY and SELL prices match, the trade is executed instantly.

Unmatched Orders:

Orders that cannot be executed immediately are stored in the MatchingQueue.

3. Architecture

Data Structures:

Java's PriorityQueue is used to manage orders efficiently.

Matching Logic:

  • Orders match if the BUY price is greater than or equal to the SELL price.
  • If multiple orders have the same price, the earliest order (FIFO) is matched first.
  • If no match is found, the order is added to the queue.

4. Future Improvements

  • Implement order cancellation functionality.
  • Add support for market orders.
  • Optimize matching performance using multi-threading.
  • Persist executed orders to a database for historical tracking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    MVPsnapshotCode snapshot for future performance comparison.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions