Trading.OrderBook (trading v0.1.0)

Efficient limit order book implementation using ETS for O(log n) operations.

The order book maintains buy and sell sides with price-time priority.

Summary

Functions

Adds an order to the order book.

Gets the best bid and ask prices.

Cancels an order in the order book.

Gets the order book depth up to a certain level.

Gets all orders at a specific price level.

Gets market depth statistics.

Gets total volume at a specific price level.

Creates a new order book for a given symbol.

Gets the total number of orders in the book.

Types

t()

@type t() :: %Trading.OrderBook{
  ask_table: :ets.tid() | nil,
  bid_table: :ets.tid() | nil,
  last_trade: map() | nil,
  order_map: :ets.tid() | nil,
  symbol: String.t(),
  timestamp: integer()
}

Functions

add_order(book, order)

@spec add_order(t(), Trading.Order.t()) :: {:ok, t()} | {:error, String.t()}

Adds an order to the order book.

best_bid_ask(order_book)

@spec best_bid_ask(t()) :: {float() | nil, float() | nil}

Gets the best bid and ask prices.

cancel_order(book, order_id)

@spec cancel_order(t(), String.t()) :: {:ok, t()} | {:error, String.t()}

Cancels an order in the order book.

get_depth(order_book, levels)

@spec get_depth(t(), pos_integer()) :: %{bids: list(), asks: list()}

Gets the order book depth up to a certain level.

get_orders_at_price(book, price, side)

@spec get_orders_at_price(t(), float(), :buy | :sell) :: [Trading.Order.t()]

Gets all orders at a specific price level.

get_stats(book)

@spec get_stats(t()) :: map()

Gets market depth statistics.

get_volume_at_price(book, price, side)

@spec get_volume_at_price(t(), float(), :buy | :sell) :: non_neg_integer()

Gets total volume at a specific price level.

new(symbol)

@spec new(String.t()) :: t()

Creates a new order book for a given symbol.

order_count(order_book)

@spec order_count(t()) :: non_neg_integer()

Gets the total number of orders in the book.