ExchangeEngine

04 Feb 2019

I started ExchangeEngine on GitHub in 2015 to experiment with setting up continuous integration and testing on some different services. Back in 2015 I didn’t get far with the project after I had finished messing around with the continuous integration systems. I randomly picked it back up recently and got it to a minimal product.

At its most basic, ExchangeEngine is a Node.js library that allows you to create an exchange for any generic thing you wish. Currently, it is very minimal with no explicit architecture defined for users or authentication. In theory, you can use ExchangeEngine to back any type of exchange - be it stocks, Bitcoin, or Pokemon Cards.

The order types supported are Limit and Market orders with FIFO priority execution.

Example

Given the two orders coming in:

var order1 = {
    quantity: 1,
    price: 3,
    side: Order.SIDE.ASK,
    type: OrderTypes.LIMIT,
    owner: "user1",
    status: Order.STATUS.ACTIVE,
    time: new Date()
}

var order2 = {
    quantity: 1,
    price: null,
    side: Order.SIDE.BID,
    type: OrderTypes.MARKET,
    owner: "user2",
    status: Order.STATUS.ACTIVE,
    time: new Date()
}

The Exchange Engine will find a match and produce a trade:

[ Trade {
    fillPrice: 3,
    fillQuantity: 1,
    newOrders: [],
    childOrders: [[Object], [Object] ],
    rejectedOrders: [],
    executionTime: 2019-02-05T04:47:36.119Z } ]

Benchmark

Backing the order matching engine is a fibonacci heap. I picked it because it sounds cool (just kidding). This seems to produce satisfactory performance on my dying MacBook Pro retina. As expected with the heap, the performance depends on the book depth.

Book Depth - Transactions/Second
100   219,257
500   56,879
1,000   26,894
10,000   2,171

Given that the daily volume of SPY today was 60,673,524, if each trade was one share, that’d be around 2,592 trades per second in the 6.5 hour trading day. Not a bad start.

If your application is running on the cheapest MacBook, has books 10k orders deep, and transacting more volume than SPY, you can probably afford something better than this free GitHub project.

Although this performance is more than sufficient for most, I am thinking of implementing a more memory intensive but much faster matching engine. I also have some fun ideas for projects with this. More on this later…

finance software-engineering