Building an exchange — a walkthrough

Antisocial Extrovert
Blockchain thoughts
3 min readSep 13, 2020

--

Stock exchange ticker

In the world of trade, we have Marketplaces. A marketplace is an e-commerce platform that connects people looking for a product/service (buyer) with those who provide a product/service (seller).

An advanced type of marketplace is called an Exchange. it is used to trade financial instruments like stocks, bonds, futures contracts. This article will be talking about the technical requirements to building an exchange

My thoughts on building a basic high level asset exchange. (commodities, stock, currency etc)

To build an exchange, you would require a suite of tradable Assets. An asset is a financial instrument like a stock/bond.

it is characterised by the following:

  • Name
  • Symbol: This is used as an identifier to represent the stock on an exchange
  • Shares Outstanding (the total supply of that share)
  • Float (the total number of tradable shares in circulation)
  • Price

The buy and sell of this asset is called an Order. An order consists of the following, which will be explained

  • Asset (symbol)
  • Order type (market or limit)
  • Order side (buy or sell)
  • Amount
  • Price (optional, will be explained)

Every exchange keeps a list of orders for every asset, called an Order Book. It is used in the matching algorithm for each trade made on that asset.

There are different types of orders, for this article, we will be looking at the two most important (Market and Limit order).

A Market Order is an order placed on the exchange which must be fulfilled immediately. This order is placed when the buyer or seller of that asset is time sensitive but not bothered about the price.
This is because a market order is placed on the current price of that stock, after which, if the order is not completely fulfilled, it matches to the next price on the Order Book.

A Limit Order is an order placed on the exchange to be matched at a given price or a price better than the given price. e.g if the current price of ethereum is $200, when i place a limit buy order of $190, the exchange will match my order only when a corresponding sell order of $190 or less is available on the Order Book.
These order types are very important int the matching algorithm of an exchange.

We also have two sides of an order, A Buy (Bid) order and a Sell (Ask) order

A Bid, represents the maximum price that a buyer is willing to pay for a share of stock or other security. eg. If I place a bid order of $190 for ethereum, it means I am willing to pay a maximum of $190 for 1 ETH

An ASK, represents the minimum price that a seller is willing to accept for that same security. If I place a sell order of ethereum for $190, it means I am only going to accept a buy request of $190 and above for 1 ETH

A Trade can only happen when a bid and a corresponding ask have been matched by an exchange.

Scoping this out what this means is; If I were to place a market bid order, the price becomes the current market price.

The exchange gets all asks for that ticker in the order-book and sorts in ascending order by price. This is because I want to buy at the cheapest sell price for that amount.
In the same vein, If I place a market ask order, the exchange get’s all bids in the order book and sorts in descending order, by price. This is because I want to sell at the highest buy price for that amount.

This matching happens recursively in a market trade, meaning: If the first (Level 1) order in the order book has less amount of assets a user wants to trade, the matching algorithm, after purchasing that moves on to the next order, on the order book.
For a limit trade, after purchasing that amount of tokens, it creates a limit order into the order book for the remaining amount of assets it needs to purchase.

I wrote this out as an explanatory tutorial to anyone looking to write a basic exchange for trade, feel free to build upon this, and send a DM @bigbrutha_ if you have questions/solutions you think would work better.

--

--