-
Notifications
You must be signed in to change notification settings - Fork 0
The Order Pipeline
This page describes how new orders are processed, as well as the life-cycle of orders.
We categorize the order as soon as its parsed.
struct Order {
action: String,
symbol: String,
quantity: i32,
filled: i32,
price: f64,
order_id: i32,
status: OrderStatus,
user_id: Option<i32>
}
The order can be either a BUY or SELL, otherwise we discard it. The status can be one of PENDING, CANCELLED, or COMPLETE.
When a new order is requested, we associate the order with the user that made the request, then call validate_order, a method that ensures that this new order cannot possibly fill a previous order placed by the same user (this prevents users from trading with themselves). If the Order is validated, we submit it to the market.
Now, the order isn't immediately added to the market, rather, we compare existing orders in the market with the new one to see if the new order will fill any pending orders. This is done in an extremely efficient manner, since we only need to compare against either the highest bid, or lowest sell, and we store pending orders as Binary Heaps, making accesses to these orders O(1)! We then enter a loop where we fill orders until either:
- the new order has been completely filled
- the new order cannot fill any more existing orders
If scenario 1 is met, the new order is not added to the market, since its been completed (i.e the order's life cycle is complete). If scenario 2 is met, we store the new order on the market, and its status (PENDING) remains unchanged. In addition to being added to the market, we add the new order to the users pending orders, and in either case, we add it to the Orders Buffer to be written to the database eventually.
Continuing with scenario 2 from above, the order is now a pending order, and it will remain that way until one of these situations occur:
- The order is cancelled by the account that originally placed it.
- The order is filled by a newly placed order some time in the future.