How does a Stock Exchange actually work?

Karan Sharma
6 min readNov 12, 2017

--

This week I picked up an exciting book about the history of Indian Stock Market: Bulls Bears and other beasts. The book narrates an adventurous journey of an individual in the D-Street while describing every little nuance in the stock market world particularly in Indian context. It got me pretty curious about the way a stock exchange works.

What is an exchange?

An exchange is a marketplace for a buyer and seller to “exchange” an entity for a mutually agreeable price. As easy and simple this might sound now, there are a lot of interesting concepts behind how this price is determined. A stock exchange executes millions of orders every day and a lot goes behind building these mission-critical systems. Fun fact: BSE which is the oldest Asian stock exchange also is the world’s fastest exchange, executing an order in less than 6 ms.

Days of Yore

Back in the 90s, when computers were still making their way into mainstream offices, the giant exchange BSE was still stuck with the old school way of buying/selling i.e. trading on the floor, interacting with the other person face to face, and maintaining transactions on a sauda pad. Naturally, this was prone to errors and disputes would arise, which the exchange would raise an objection, also called vaanda kaapli. Every broker had few jobbers who would create the market for the scrips and buy/sell on behalf of the broker. BSE’s board was dominated by brokers and clearly they didn’t want technological advancements to happen as it would disrupt the way their business was operated. Jobbers would have been the most affected by this move. The introduction of NSE in 1994 was a disruptive moment in Indian stock exchange history. NSE had initially started only its debt market and 6–7 months later they also opened up screen trading for equities. This is an interesting video to watch if you want to go back in time!

To understand what happens behind the scenes when you place an order at the exchange(through a broker), one needs to be familiar with a few basic terms.

Orderbook

NSE and BSE follow limit order matching strategy for order execution. This basically drives down the transaction cost, makes the buyers and sellers anonymous and increases liquidity. Before the screen based trading was introduced, the exchanges were quote-driven. The jobber (aka market maker) would quote you the spread(bid/ask) and it was up to you to take the call. The bid price refers to the price a buyer is willing to “bid”(pay) to buy a security. Similarly, the ask price refers to the price a seller is willing to “ask”(receive) for selling a security. Market depth displays the list of bids and asks price for a security along with the quantity of shares for which this price is valid.

Bid-Ask Spread

The difference between the best buy price and the best sell price is called a spread. Best buy is the highest price in Bid price list and best sell is the lowest price in Ask price list. The order book is sorted by price first and then time.
This spread is exactly what transaction cost means. Higher the transaction cost means higher the spread and lesser the liquidity. Although the spread depends on the quantity of shares being transacted. This is to prevent a sudden surge in the price by an operator buying/selling in heavy quantities. The spread increases as the quantity increases.

Order Execution

For all practical purposes, let’s assume the exchange maintains only one kind of order book - the regular orders. (Although, in real-world scenario, exchange maintains different types of book for other special kinds of orders). A limit order is the fundamental building block of an order book. All other orders are a type of a limit order. A limit order is an order where you quote the price and indicate your intention (Buy/Sell). Market order is where you just indicate your intention, the price will be the current market price and whatever it is, you have to accept.
Remember exchange is just a marketplace to connect a buyer and seller? The order matching algorithm is based on the same principle, to act as an unbiased intermediary for both the parties. The exchange wants the buyer to get the best price for the security and give seller the best price to sell the security. How to achieve a win-win situation? It’s pretty simple, match the best bid with the best ask. The best bid is the one which is the highest price a buyer is willing to pay for the underlying security. Similarly, the best ask is the lowest price the seller is asking to pay for the underlying security.

Example of an orderbook at exchange

Now with the knowledge of bid-ask spread and order book, let’s see how the exchange would execute the above orders in real time. From the above example, the best bid is at ₹10 (highest buy price) and best ask is ₹11 (lowest sell price). The spread is the difference between ask and bid, which is 11–10 = ₹1.

Let’s say a person wants to buy 1500 quantity of this stock. A buy order is placed at the exchange for 1500 shares. The exchange matches 1000 shares at the best possible price available, i.e. at ₹11. The remaining 500 shares are executed at the next best price i.e. ₹11.50. Similarly, if a seller wants to offload 3000 shares of this stock, 1000 shares would first get executed at Rs 10, the next 500 at ₹9.70, the next 1500 at ₹9.40. The total amount he would receive is ₹28950 (1000*10 + 500 * 9.70 + 1500 * 9.40)

Did you notice an interesting thing? As your order size increases, the actual transaction cost of your order also increases. This is called Impact Cost.
In an ideal infinitely liquid market, there should be no difference between an ask and bid price of the same security. Let’s call this ideal price and this would be the average of the ask and bid price. In our case the ideal price is 10.50 (average(₹10(buy), ₹11(sell)). If a seller wants to offload 3000 shares, he would have received ₹31500(3000*10.50). In the real world, however, he received only ₹28950 (8.09% lesser!). The lower the spread the, lower this impact cost.

Back in the days before screen trading, the market was quote driven. That is, you would have to go to a jobber to execute your shares. For liquid scrips the spread would be less as the jobber is confident he can buy from you at lower price and sell at higher price easily. A part of the difference went in his pocket and other as the commission to the broker under whom the jobber was working for. There was also very limited quantity of shares that a jobber would risk at every trade. If the quantity was huge or the scrips were illiquid that means the jobber is taking more risk on your behalf and hence the spread would be really high.
The single biggest advantage of screen based trading is that now market is order driven. No longer could the jobber quote an outrageous spread, and you could also see the market depth in your screens and place order based on that.

Tip of the iceberg

The concept of market depth sounds really interesting, but don’t be misguided by the fact that you can see all the orders in the order book for the security. Most brokers provide L2 depth of data where you can only see the top 5 bids and asks. The quantity shown in the order book is also only discrepant quantity, that means a person is allowed to hide the real quantity of the shares she’s willing to buy/sell as long as she declares the minimum quantity which is set by the exchange.

This was just a tiny attempt at understanding how exchanges work. If you are interested in more details, do check out Reminiscences of a Stock Operator. It’s a thrilling narrative of how Jesse Livermore started as a pure speculator to one of the most celebrated traders on Wall St.

Did you enjoy this post? Recommend it, by giving it some claps. Thanks!

--

--

Karan Sharma

Developer| GSoC’16 student @coala_analyzer | Chicken, Beer, Floyd