Building a Financial Exchange from first principles

proximuz1 pts0 comments

Building a Financial Exchange from first principles | Harsh Iyer

Speed and simplicity rarely come together. In pursuit of one we more often than not have to sacrifice the other. Yet,<br>all these stock exchanges somehow manage sub millisecond latency, for millions?!

This treasure hunt led me to make farzi.exchange.

farzi (फ़र्ज़ी ) translates to "fake" in Hindi, which is precisely what this is. The goal was to build a fake stock exchange, ground up from first principles,<br>while trying to retain a decent amount of performance without overengineering, or overspending for compute.

A couple of resources proved to be very useful while building this:

Trading & ExchangesLarry HarrisHow to Build an ExchangeJane Street

While both of these are skewed towards the US markets, the very core of a financial exchange never changes.

So then, that's enough introduction. Let's get into what an exchange truly is.

What makes a Financial Exchange?

the bombay stock exchange, mumbai

The core purpose of a financial exchange is to match orders , primarily buy and sell orders,<br>between buyers and sellers (who wouldve thought?!). Financial Exchanges work best when they are trade fungible instruments, i.e.<br>one unit is economically indistinguishable from all other units.

This is done via maintaining an Order Book , which is updated thousands to millions of times every second.

The core of an Order Book is a data structure that holds all the buy and sell orders in memory, usually partitioned<br>on an instrument level.

Then a Matching Algorithm refers to this Order Book, to match orders that go hand in hand and make trades out of them.

This algorithm has a list of rules it swears by, to make sure all matches are deterministic , reproducible and most importantly, fair .

Taking inspiration from NSE, farzi.exchange is a price-time exchange, which adheres to the rule of<br>Orders are matched based on price and time, with the best bid/ask that arrived the earliest, matched first.<br>For tie breaks, exchanges usually adopt different parameters to tip one order over the another, usually these are:

Disclosed Quantity (the more you disclose, the higher you shall stand)

Order Size (can go either way, with exchanges preferring larger orders over smaller ones and vice versa)

Now that we have valued one order over another, what different shapes can an order take?

Order Type Mayhem

An order is a trader's way of communicating with the exchange, their wants and more importantly, their terms.

Modern exchanges support a variety of order types, each serving a different purpose and scenario.

Limit Orders

Limit Orders are one of the most common order types, which is basically a traders way of telling the exchange<br>"I am willing to Buy/Sell XYZ, but only if its under/atleast this price."

This gives a trader the illusion of being in control.

Market Orders

Another common order type present in almost all exchanges are Market Orders, which are used to execute a trade at the current best market price , no strings attached.<br>Do note that between the time an order is placed on the client and it reaches the exchange, the price may have changed.

While usually this difference isnt much, not having a Limit Order can be a problem if the market price moves against you significantly.

Another con of Market Orders is they fill against the Best Bid/Ask opposite to you, thus making you eat the spread .

The spread is the difference between the Best Bid and Best Ask prices, basically the gap between the lowest someone is willing to sell<br>an instrument at and the highest someone is willing to buy it at.

Iceberg Orders

An iceberg order is a trading technique that splits a large buy or sell order into smaller, sequential limit orders to conceal the true trade size.

Only a small visible portion (the "tip of the iceberg" ) hits the order book, while the rest remains hidden. As each visible leg executes, the next automatically replenishes.

an example iceberg order

The reason large traders prefer iceberg orders is it hides a trader’s true market intentions from competitors, stopping front-running and other manipulative trading behaviors.

Exchanges such as NSE do have a restriction of a minimum of 10% of the total order value to be disclosed at the time of order placement.

That's a bunch of trading lingo, but it is essential to know them to better understand some key decisions. Moving on to the technical part of the Exchange, its design.

The Exchange Architecture

On a higher level, the exchange ended up looking something like this:

farzi.exchange architecture

That's a lot of blocks. Each one of them have their dedicated role as part of the exchange and we will shortly go over them in detail and as to what problem they solve.

The Exchange would then initialize everything in a back-to-front order, building them off of each other, to ensure each component has all its dependencies initialized prior to its initialization.

There's a bunch of key moving parts, let's go...

order exchange orders exchanges financial from

Related Articles