-
Notifications
You must be signed in to change notification settings - Fork 72
1. Terminology
Order is an intent to buy or sell a specific quantity of stock at a desired price. The order includes information such as OrderId, UserId, Price, Quantity, Side(buy or sell), etc.
PriceLevel is a collection of orders at the same price. A PriceLevel can contain multiple orders at the same price of same side. All Orders of Pricelevels are stored and exceuted in FIFO manner. PriceLevels are maintained for Limit orders and Stop Orders. PriceLevel also contain aggregate information such as total quantity at and no. of orders for that price.
OrderBook maintains PriceLevels. One OrderBook contains 4 ordered collection of PriceLevels. PricelLevels are sorted based on price. For limit buy side, PriceLevels are sorted by price in descending order. for limit sell side, PriceLevels are sorted by price in ascending order. Order Book also maintains PriceLevels for stop orders. For stop buy side price levels are sorted by stop price in ascending order and stop sell side are sorted by descending order.
- Bid Side Price Levels
- Ask Side Price Levels
- Stop Bid Price Levels
- Stop Ask Price Levels
MatchingEngine is responsible for order execution. Each MatchingEngine Instance internally maintains OrderBook. it exposes method to add new order and cancel order. it also triggers events when order is accepted, matched, cancelled, or stop order is triggered.
MatchingEngine sends evets to ITradeListener instance. An instance of ITradeListener should be passed to MatchingEngine to listen to events emitted by MatchingEngine. MatchingEngine exceutes method on ITradeListner for all the events. ITradeListener can then forward this information to other component to update state of the order, fills and balance. Below events are available on ITradeListner.
- Order Accept
- Order Cancel
- Match
- Self Match
- Decrement Quantity
MatchingEngine calcualtes fee on the order. Fees can be provided by IFeeProvider instance. IFeeProvider is queried everytime when match occures. This allows configuring different fee for different tiers of users. MatchingEngine supports maker and take fee model. maker order provides liquidity while taker order takes liquidity. MatchingEngine calculates fee and passes that information to ITradeListener when order is filled completely or cancelled.
Serializers converts objects to byte array and vice versa. You could use any serializers(Json, Bson, Protobuf, msgapck) you like but serializers provided with order-matcher are fast and works with order matcher types only.