Matching Flow

Now that we know how the matching engine behaves we should take a look at how this works on a technical level. All possible events that happen on the Exchange can be categorized into trade events and status changes. Trade events are those that can result in the creation of a trade. Status changes also have an effect on the exchange but do not ever result in a trade. The events are triggered by actions. Adding orders and direct buys are actions that result in trading events while canceling an order results in a status change. 

The exchange collects and queues all actions that are submitted in one tick. One tick is the defined execution time frame of the exchange and it’s set to 1 second by default. The action consists of the action-type and its contents. The action-type can be either add order, add direct buy, or cancel the order. With every tick, all queued actions are executed one after the other. 

In case that a new order is added, it is inserted into the order book on either the ask or bid side. The order book is re-ordered by price every time that a new order is inserted. After the re-ordering, the matching engine is started and it is checked if two orders match. The matching works by comparing each existing ask and bid in the order book and see if they match based on EAC criteria and price. This creates the matching behavior explained above. Adding orders can result in the creation of a trade if the order matches with another one on the exchange. If not, the order is just added to the order book and stays there until matched or canceled. If a match is found, a trade event is created and collected by the matching engine. 

Similarly, if a direct buy is added, the system creates a bid with price and EAC criteria that exactly match the ask’s criteria. The buyer can choose the desired volume but the volume cannot exceed the ask volume. A trade is directly created with the chosen ask, without triggering the regular matching technique of comparing each bid and ask. This means that the bid is never really put on the exchange but the two orders are directly converted into a trade event. As a result, a direct buy is a separate action, compared to simple order creation, and is queued as a separate action type. A direct buy should always result in a trade event if no error occurs. If there is an error, a status change event is logged that expresses this problem with the execution. 

The main action that results in a status change event is canceling an order. An order is canceled by finding the order by id and creating a status change event. All status changes are collected by the matching engine. 

The trade and status change events that have been collected in the matching engine, trigger operations in other parts of the system. For every trade event, the order volume of the bid and ask is updated depending on the traded volume and a trade is created. Each trade contains corresponding volume and price information and a reference to the bid and ask. Orders with filled volumes disappear from the exchange completely while partially filled remain on the order book but with an updated volume. Created trades represent ownership changes in the exchange application. However, there is no ownership change on the blockchain until a user withdraws the EACs from the exchange. 

Orders with volumes that are not filled or only partially filled are active while orders that are filled or canceled are no longer active. For every status change event, the order status is updated. If an order is canceled, the order status is changed to canceled and is removed from the order book. Unexpected errors in the matching process that are collected through status change events e.g. in the case there has been a problem with the direct buy, trigger the order status to change to “Error” to reflect the problem in the matching engine. 

Requirements: 

  • All actions by users: add order, add direct buy and cancel order should be correctly collected by the matching engine

  • Every tick should trigger the execution of actions

  • Every action should be executed in the right order of appearance 

  • Add order should add the right side of the order book (either ask or bid)

  • Add order should correctly re-order the order book by price

  • Add order should trigger a comparison between all existing orders on the exchange and correctly create a trade event if matched

  • Orders should only match if all EACs criteria and price match

  • Every add direct buy should create a bid and automatically match it with the chosen ask, creating a trade event

  • Every error with direct buy should create an error status change event

  • Every cancel order should remove the order from the matching engine and create a cancel status change event

  • Every trade event should update order volumes and create a trade

  • Every status change event should change the order status correctly