Exchange v0.1.0 Exchange.OrderBook View Source
Order Book Struct
The Order Book is the Exchange main data structure. It holds the Order Book in Memory where the MatchingEngine realizes the matches and register the Trades.
Example of and Order Book for Physical Gold (AUX) in London Market Orders with currency in Great British Pounds:
%Exchange.OrderBook{
name: :AUXLND,
currency: :GBP,
buy: %{},
sell: %{},
order_ids: MapSet.new(),
ask_min: 99_999,
bid_max: 0,
max_price: 99_999,
min_price: 0
}
Buy and Sell Sides, price points and Orders
The OrderBook has a buy and sell keys that represent each of the sides.
Each side is a map indexed by an integer representing the price_point
That contains a Queue of orders ordered by First In First Out (FIFO)
The queues of Exchange.Order are implemented using Qex (an Elixir wrapper for the erlang OTP :queue.
Example:
buy: %{
4001 => #Qex<[
%Exchange.Order{
acknowledged_at: ~U[2020-03-31 14:02:20.534364Z],
modified_at: ~U[2020-03-31 14:02:20.534374Z],
order_id: "e3fbecca-736d-11ea-b415-8c8590538575",
price: 3970,
side: :buy,
size: 750,
trader_id: "10995c7c-736e-11ea-a987-8c8590538575",
type: :limit
}
]>,
4000 => #Qex<[ Exchange.Order(1), ... Exchange.Order(n) ]>,
3980 => #Qex<[ Exchange.Order(1), ... Exchange.Order(n) ]>
Other Attributes
- name: Is the name of the ticker and the Id of the exchange. The exchange supports multiple matching engines identified by the different unique tickers.
- currency: The currency that is supported inside this OrderBook. All orders must have prices in cents in that currency.
- order_ids: Is an index of ids of the orders to see if and order is waiting for a match or not.
These are all attributes that represent prices in cents for currency:
- ask_min: is the current minimum ask price on the sell side.
- bid_max: is the current maximum bid price on the buy side.
- max_price: is the maximum price in cents that we accept a buy order
- min_price: is the minimum price in cents that we accept a sell order
Link to this section Summary
Functions
Updates the Order Book decrementing the bid_max by 1
Removes an %Order{} from the Order Book
Try to feth a matching buy/sell at the current bid_max/ask_min price
returns the highest asking volume
Updates the Order Book incrementing the ask_min by 1
Updates the Order Book incrementing the ask_min by 1 or decrementing the bid_max by 1 taking into account the order's side and price
Returns the list of open orders
Returns the list of open orders from a trader
This is the core of the Matching Engine. Our Matching Engine implements the Price-Time Matching Algorithm.
Queues an %Order{} in to the correct price_point in the order_book
Register Trade on Order Book completed_trades
Updates the Order Book setting ask_min
Updates the Order Book setting bid_max
returns spread for this exchange
Link to this section Types
Specs
order_book() :: %Exchange.OrderBook{ ask_min: price_in_cents(), bid_max: price_in_cents(), buy: %{optional(price_in_cents()) => queue_of_orders()}, completed_trades: list(), currency: atom(), expiration_list: term(), expired_orders: term(), max_price: price_in_cents(), min_price: price_in_cents(), name: atom(), order_ids: %{optional(String.t()) => {atom(), price_in_cents()}}, sell: %{optional(price_in_cents()) => queue_of_orders()} }
Specs
price_in_cents() :: integer()
Specs
queue_of_orders() :: %Qex{data: [Exchange.Order.order()]}
Specs
size_in_grams() :: integer()
Specs
ticker() :: atom()
Link to this section Functions
Specs
add_order_to_expirations(order_book(), Exchange.Order.order()) :: order_book()
Specs
add_order_to_index(order_book(), Exchange.Order.order()) :: order_book()
Specs
calculate_min_max_prices(order_book(), Exchange.Order.order()) :: order_book()
Specs
check_expired_orders!(order_book()) :: order_book()
Specs
completed_trades(order_book()) :: list()
Specs
decrement_bid_max(order_book(), number()) :: order_book()
Updates the Order Book decrementing the bid_max by 1
Specs
dequeue_order(order_book(), Exchange.Order.order()) :: order_book()
Removes an %Order{} from the Order Book
Specs
dequeue_order_by_id(order_book(), String.t()) :: order_book()
Specs
fetch_matching_order(order_book(), Exchange.Order.order()) :: atom() | {atom(), Exchange.Order.order()}
Try to feth a matching buy/sell at the current bid_max/ask_min price
Specs
fetch_order_by_id(order_book(), String.t()) :: Exchange.Order.order()
Specs
flush_expired_orders!(order_book()) :: order_book()
Specs
flush_trades!(order_book()) :: order_book()
Specs
highest_ask_volume(order_book()) :: number()
returns the highest asking volume
Specs
highest_bid_volume(order_book()) :: number()
Specs
increment_ask_min(order_book(), number()) :: order_book()
Updates the Order Book incrementing the ask_min by 1
Specs
increment_or_decrement(order_book(), Exchange.Order.order()) :: order_book()
Updates the Order Book incrementing the ask_min by 1 or decrementing the bid_max by 1 taking into account the order's side and price
Specs
insert_order_in_queue(order_book(), Exchange.Order.order()) :: order_book()
Specs
open_orders(Exchange.OrderBook) :: [Exchange.Order]
Returns the list of open orders
Specs
open_orders_by_trader(Exchange.OrderBook, String) :: [Exchange.Order]
Returns the list of open orders from a trader
Specs
order_exists?(order_book(), String.t()) :: boolean()
Specs
Specs
pop_order_from_expiration(order_book()) :: order_book()
Specs
price_time_match(order_book(), Exchange.Order.order()) :: order_book()
This is the core of the Matching Engine. Our Matching Engine implements the Price-Time Matching Algorithm.
The first Orders arriving at that price point have priority
Specs
queue_order(order_book(), Exchange.Order.order()) :: order_book()
Queues an %Order{} in to the correct price_point in the order_book
Specs
register_trade(order_book(), Exchange.Order.order(), Exchange.Order.order()) :: order_book()
Register Trade on Order Book completed_trades
Specs
remove_order_from_index(order_book(), Exchange.Order.order()) :: order_book()
Specs
set_ask_min(order_book(), Exchange.Order.order()) :: order_book()
Updates the Order Book setting ask_min
Specs
set_bid_max(order_book(), Exchange.Order.order()) :: order_book()
Updates the Order Book setting bid_max
Specs
spread(order_book()) :: number()
returns spread for this exchange
Specs
total_ask_orders(order_book()) :: number()
Specs
total_bid_orders(order_book()) :: number()
Specs
update_order(order_book(), Exchange.Order.order()) :: order_book()
Specs
update_queue(order_book(), atom(), price_in_cents(), queue_of_orders()) :: order_book()